Binding multiple csvs in R to output to a graph in ggplot2

Viewed 45

This is an example of my data:

                      HUC8 YEAR RO_MM
       bcc1_45Fall_1020004 1961 112.0
       bcc1_45Fall_1020004 1962 243.7
       bcc1_45Fall_1020004 1963 233.3
       bcc1_45Fall_1020004 1964 190.3
     bcc1_M_45Fall_1020004 1961 100.9
     bcc1_M_45Fall_1020004 1962 132.3
     bcc1_M_45Fall_1020004 1963 255.1
     bcc1_M_45Fall_1020004 1964 281.9
     bnuesm_45Fall_1020004 1961  89.0
     bnuesm_45Fall_1020004 1962  89.5
     bnuesm_45Fall_1020004 1963 126.8
     bnuesm_45Fall_1020004 1964 194.3
    canesm2_45Fall_1020004 1961 186.6
    canesm2_45Fall_1020004 1962 197.4
    canesm2_45Fall_1020004 1963 229.1
    canesm2_45Fall_1020004 1964 141.8

Each of the similar prefixes represents (a segment of) a single csv. I have called them into a list and used rbind to link them. My goal is to have each csv represent a line of data, which would look like this:

Name               
                        1961   1962   1963   1964   ...
   bcc1_45Fall_1020004  112.0  243.7  233.3  190.3
 bcc1_M_45Fall_1020004  100.9  132.3  255.1  281.9
 bnuesm_45Fall_1020004   89.0   89.5  126.8  194.3
canesm2_45Fall_1020004  186.6  197.4  229.1  141.8

I would then like to plot these lines in a line graph using ggplot2where each Name becomes a line of "RO_MM" data over 140 years. Remember, this is only a tiny sample. There are actually hundreds of files. I know that hundreds is too many for a graph and plan to do them in smaller groups, but I DO NOT need to grid them together. I have so far used this code which has provided the initial datalist above:

library(rio)
library(tidyverse)
library(data.table)

file_names <- list.files("~/Desktop/Rproj/splitByHUCs45/a01020004/splFall")
data_list <- lapply(file_names, read.csv , header=TRUE, sep=",")
finalTable <- do.call(rbind, data_list)

I have found this code (below). It is not what I need because I don't need the mean of anything, but I saw that it used more than one csv for input, so I'm trying to make sense of it, but don't know how to make it work for me.

   #some pseudo data for testing
   my_other_data <- myData 
my_other_data$Data <- my_other_data$Data * 0.5 

pplot <- ggplot(data=myData, aes(x=Group, y=Data)) + 
   stat_summary(fun = mean, geom = "line", color='red') +
   stat_summary(data=my_other_data, aes(x=Group, y=Data), 
                fun = mean, geom = "line", color='green') +
   xlab("Group") +
   ylab("Data")
pplot

That said, the page on creating a reprex said that I should provide you with this:

head(finalTable, 3) %>%
+    deparse()
 [1] "structure(list(HUC8 = structure(c(1L, 1L, 1L), .Label = c(\"bcc1_45Fall_1020004\", "            
 [2] "\"bcc1_M_45Fall_1020004\", \"bnuesm_45Fall_1020004\", \"canesm2_45Fall_1020004\", "             
 [3] "\"ccsm4_45Fall_1020004\", \"cnrmcm5_45Fall_1020004\", \"csiromk360_45Fall_1020004\", "          
 [4] "\"gfdlesm2g_45Fall_1020004\", \"gfdlesm2m_45Fall_1020004\", \"hadgem2cc_45Fall_1020004\", "     
 [5] "\"hadgem2es_45Fall_1020004\", \"hist_Fall_1020004\", \"inmcm4_45Fall_1020004\", "               
 [6] "\"ipslcm5_alr_45Fall_1020004\", \"ipslcm5_blr_45Fall_1020004\", \"ipslcm5amr_45Fall_1020004\", "
 [7] "\"miroc5_45Fall_1020004\", \"mirocesm_45Fall_1020004\", \"mirocesmchem_45Fall_1020004\", "      
 [8] "\"mricgcm3_45Fall_1020004\", \"noresm1m_45Fall_1020004\"), class = \"factor\"), "               
 [9] "    YEAR = 1961:1963, RO_MM = c(112, 243.7, 233.3)), row.names = c(NA, "                        
[10] "3L), class = \"data.frame\")"  

I would appreciate getting help structuring the data so that I can bring it into ggplot2 and how to make a graph with ggplot2, and explanations would be especially helpful. Thanks.

1 Answers

I wasn't able to use your example data (please use dput(head(finalTable)) instead of deparse), but here is one potential solution using the data at the beginning of your question:

# Load libraries and data
library(tidyverse)
dat1 <- read.table(text = "HUC8 YEAR RO_MM
       bcc1_45Fall_1020004 1961 112.0
       bcc1_45Fall_1020004 1962 243.7
       bcc1_45Fall_1020004 1963 233.3
       bcc1_45Fall_1020004 1964 190.3
     bcc1_M_45Fall_1020004 1961 100.9
     bcc1_M_45Fall_1020004 1962 132.3
     bcc1_M_45Fall_1020004 1963 255.1
     bcc1_M_45Fall_1020004 1964 281.9
     bnuesm_45Fall_1020004 1961  89.0
     bnuesm_45Fall_1020004 1962  89.5
     bnuesm_45Fall_1020004 1963 126.8
     bnuesm_45Fall_1020004 1964 194.3
    canesm2_45Fall_1020004 1961 186.6
    canesm2_45Fall_1020004 1962 197.4
    canesm2_45Fall_1020004 1963 229.1
    canesm2_45Fall_1020004 1964 141.8",
           header = TRUE)

# Create your table
dat1 %>% 
  pivot_wider(names_from = YEAR, values_from = RO_MM)
# A tibble: 4 x 5
#  HUC8                   `1961` `1962` `1963` `1964`
#  <chr>                   <dbl>  <dbl>  <dbl>  <dbl>
#1 bcc1_45Fall_1020004      112   244.    233.   190.
#2 bcc1_M_45Fall_1020004    101.  132.    255.   282.
#3 bnuesm_45Fall_1020004     89    89.5   127.   194.
#4 canesm2_45Fall_1020004   187.  197.    229.   142.

# Create a line plot (don't need to use the table for this)
dat1 %>%
  ggplot(aes(x = YEAR, y = RO_MM, group = HUC8, color = HUC8)) +
  geom_line()

example_1.png

And you can 'group' your results however you like, e.g.

dat1 %>%
  mutate(group = ifelse(str_detect(string = HUC8, pattern = "bcc"),
                        "group_bcc", "group_others")) %>%
  ggplot(aes(x = YEAR, y = RO_MM, group = HUC8, color = HUC8)) +
  geom_line() +
  facet_grid(rows = vars(group))

example_2.png

And, if you don't want a grid (like you said in your question):

dat1 %>%
  mutate(group = ifelse(str_detect(string = HUC8, pattern = "bcc"),
                        "group_bcc", "group_others")) %>%
  filter(group == "group_bcc") %>% 
  ggplot(aes(x = YEAR, y = RO_MM, group = HUC8, color = HUC8)) +
  geom_line() +
  ggtitle("bcc csv files only")

example_3.png

You can "highlight" one specific line using a package (e.g. gghighlight) or just tell ggplot which colours you want to use, e.g.

dat1 %>%
  ggplot(aes(x = YEAR, y = RO_MM, group = HUC8, color = HUC8)) +
  geom_line() +
  scale_color_manual(values = c("black", viridis::viridis(3, alpha = 0.33)))

example_4.png

Related