-1

So I want to plot multiple plots at the same time for each place with Num of Visits on y-axis and Day on x-axis but I'm not sure if there is a function to do this?

data frame

So I was able to make a plot for place A by subsetting place A :

placeA <- subset(df$place=="A")

ggplot(data=placeA, aes(x=Day, y=Num_OfVisits, group=1)) +
  geom_line(color="#00AFBB", size=0.5) +
  theme(axis.text.x=element_text(angle=90,hjust=1, size=5))

But now I want to generate a plot for the other places and I am hoping that I could do it all in one shot because there are around 1000 places on my dataset and subsetting is taking some time. Any help will be appreciated. Thank you!

Tung
  • 26,371
  • 7
  • 91
  • 115
potatopainting
  • 103
  • 1
  • 9
  • 1
    Please do not post an image of code/data/errors, just the text itself. Several reasons are immediate: I cannot copy code or data from your image into my R console and try it out, and I choose to not transcribe it manually. Some reasons are slightly less obvious but still important, including: it breaks screen readers *hard*; search engines don't read them, so searches will not find it; mobile device screen size might be a limiting factor. Ref: https://meta.stackoverflow.com/a/285557/3358272 – r2evans Oct 20 '18 at 04:30
  • I'm sorry. I was trying to do that but the format was not good so I decided to upload a photo. – potatopainting Oct 20 '18 at 04:32
  • 1
    `dput(x)` works really well. `dput(head(x,n=10))` if the data is "large-ish". – r2evans Oct 20 '18 at 04:34

1 Answers1

1

You can write a function that takes the data frame and Place as inputs then loop through all the values in Place column to create the corresponding plots.

library(tidyverse)

df <- data_frame(
  Place = c(rep(c("A", "B", "C"), each = 3)),
  Num_of_Visits = seq(1:9),
  Day = rep(c("Sunday", "Monday", "Tuesday"), 3)
)

df <- df %>% 
  mutate(Day = factor(Day, levels = c("Sunday", "Monday", "Tuesday")))

my_plot <- function(df, myvar) {      
  ggplot(data = df %>% filter(Place == myvar), 
         aes(x = Day, y = Num_of_Visits, group = 1)) +
    geom_line(color = "#00AFBB", size = 0.5) +
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5))      
}

# test
my_plot(df, 'A')

Loop through Place var, create plots & store them in a list using purrr::map()

plot_list <- unique(df$Place) %>% 
  purrr::set_names() %>% 
  purrr::map( ~ my_plot(df, .x))
str(plot_list, max.level = 1)
#> List of 3
#>  $ A:List of 9
#>   ..- attr(*, "class")= chr [1:2] "gg" "ggplot"
#>  $ B:List of 9
#>   ..- attr(*, "class")= chr [1:2] "gg" "ggplot"
#>  $ C:List of 9
#>   ..- attr(*, "class")= chr [1:2] "gg" "ggplot"

Display all plots with purrr::walk()

purrr::walk(plot_list, print)

Save all plots to PNG files using purrr::iwalk()

purrr::iwalk(plot_list,
             ~ ggsave(plot = .x,
                      filename = paste0("Plot_", .y, ".png"),
                      type = 'cairo', width = 6, height = 6, dpi = 150)
)

Combine all plot together if needed using cowplot::plot_grid()

library(cowplot)
do.call(plot_grid, c(plot_list, 
                     align = "h",
                     axis = 'tb',
                     ncol = 3))

Created on 2018-10-19 by the reprex package (v0.2.1.9000)

Tung
  • 26,371
  • 7
  • 91
  • 115
  • thanks! just one place worked right away, but multiple places seemed to made R to stop responding in a sense that it took 45 min for R to have the Stop sign on and when the Stop sign was gone it did not say an error message but there was no plot that was generated either. – potatopainting Oct 20 '18 at 07:27
  • This worked on R 3.5.1 and latest `tidyverse` package. You might need to update yours – Tung Oct 20 '18 at 09:39
  • I have the latest R software and tidyverse package. Im getting this error after the "walk(plot_list, print)". The error Im getting is geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic? Error in grid.Call.graphics(C_setviewport, vp, TRUE) : non-finite location and/or size for viewport – potatopainting Oct 20 '18 at 19:36
  • Make sure you have the same variable names as I did (`Num_of_visits`). It's difficult to help when you don't provide a sample of your data. You should add the output of `dput(df)` to your question. See more here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example & https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5 – Tung Oct 20 '18 at 19:57
  • @yiyle: try restarting R/RStudio before running the code to make sure there isn't any conflict with other packages – Tung Oct 20 '18 at 21:27