-1

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.

kwalk
  • 3
  • 2
  • Please specify the question and what did you already try? – eapo Oct 06 '20 at 01:29
  • Instead of `ggplot(d_long.....` try `ggplot(filter(d_long, Name == i), .......`. There is currently nothing in the loop that subsets your data, only the title and the file name have anything to do with `i`. – Axeman Oct 06 '20 at 01:52

1 Answers1

1

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)
}
cookesd
  • 1,296
  • 1
  • 5
  • 6
  • This produced “geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?” Fixed with adding `aes(date, n, group =1)` – kwalk Oct 06 '20 at 02:04