Create and save a plot for different groups using ggplot for loop from one .csv file

435 views Asked by At

How do I create a loop that will produce and save 45 different line plots based on "Name" in the .csv file?

I have one large .csv file that contains data for 45 different buildings by date. I need to create a line plot for each building that shows the numbers on the y-axis and date on the x-axis.

I tried using a list() for the buildings to use within the loop, but real building names are too complicated for R.

The code below produces 45 identical plots with different titles. Each plot contains all the data, instead of just the data for one building.


# Read in .csv file
d <- read_csv("res_data_ABC.csv")

# Reshape the data to make it longer and omit NA 
d_long <- na.omit(pivot_longer(d, cols = -Name, names_to = "date", values_to = "n"))
print(d_long)
# A tibble: 114 x 3
   Name       date         n
   <chr>      <chr>    <dbl>
 1 Building 1 09/14/20  2030
 2 Building 1 09/21/20 17900
 3 Building 1 09/30/20  1380
 4 Building 2 09/14/20    57
 5 Building 2 09/21/20     0
 6 Building 2 09/28/20   301
 7 Building 3 09/14/20 79200
 8 Building 3 09/21/20 23700
 9 Building 3 09/30/20 21800
10 Building 4 09/16/20   496
# … with 104 more rows

# Begin for loop for each building's plot
for (i in d_long$Name) {
  ggplot(d_long, aes(date, n)) + geom_line() + ggtitle(i) + theme(axis.text.x = element_text(angle = 90)) + ylab("viral density/L") + theme(plot.title = element_text(size = 14, face = "bold", hjust=0.5), axis.title.x = element_text(size = 12, face = "bold"), axis.title.y = element_text(size = 12, face = "bold"))
  ggsave(paste0("~/Desktop/plots/", i, ".png"), width = 6, height = 5, units = "cm", dpi=300)
}

These are plots produced: https://i.stack.imgur.com/5HWI3.png

I can't figure out how to limit the data in each plot by individual buildings.

1

There are 1 answers

1
cookesd On BEST ANSWER

In the for loop, you're using the whole dataframe. You should filter it in your for loop

for (i in d_long$Name) {
  ggplot(d_long[d_long$Name == i,], aes(date, n)) + geom_line() + ggtitle(i) + theme(axis.text.x = element_text(angle = 90)) + ylab("viral density/L") + theme(plot.title = element_text(size = 14, face = "bold", hjust=0.5), axis.title.x = element_text(size = 12, face = "bold"), axis.title.y = element_text(size = 12, face = "bold"))
  ggsave(paste0("~/Desktop/plots/", i, ".png"), width = 6, height = 5, units = "cm", dpi=300)
}