0

Consider

a1<-data.frame(a=c(1,2,3),b=c(45,34,67),c=c(35,56,89),d=c("first"))
a2<-data.frame(a=c(1,2,3),b=c(35,40,60),c=c(30,59,92),d=c("second"))
a3<-data.frame(a=c(1,2,3),b=c(45,38,57),c=c(35,52,91),d=c("third"))

I want to use ggplot to make three charts/plots corresponding to a1, a2, and a3. Each chart/plot should have x=a, and y=b,c. So there should be two lines in each chart. The charts should be named first, second, and third. How do I do this?

I tried

overall<-list(a1,a2,a3)
plots<-lapply(overall,function(category){o<-melt(category, id = "a", measure = c("b", "c"));
  ggplot(o, aes(a, value, colour = variable)) + geom_line()})

but it produces only one plot. Ideally, I would like to see three plots with each plot containing two lines corresponding to b and c in data frames a1, a2, and a3.

user17144
  • 428
  • 3
  • 18

3 Answers3

2

If you want to do it with ggplot2, this is an option:

a1<-data.frame(a=c(1,2,3),b=c(45,34,67),c=c(35,56,89))
a2<-data.frame(a=c(1,2,3),b=c(35,40,60),c=c(30,59,92))
a3<-data.frame(a=c(1,2,3),b=c(45,38,57),c=c(35,52,91))

library(tidyverse)
a <- list(a1,a2,a3)
for (i in seq_along(a)){
  a[[i]] <- as.data.frame(a[[i]]) %>% 
    mutate(plot_name = paste0("a", i))
}

a <- bind_rows(a)
a %>%  pivot_longer(cols =c(b,c)) %>% 
  ggplot(aes(x=a, y=value, group=name)) +
  geom_line(aes(colour = name)) +
  facet_wrap(.~plot_name, nrow =  3)

Created on 2020-02-27 by the reprex package (v0.3.0)

MKR
  • 1,620
  • 7
  • 20
  • How does it understand what y=value is and group=name are? Also, in ggplot(aes(x=a..) what is a - the column name or the consolidated data frame? – user17144 Feb 27 '20 at 08:43
  • that comes from the functon `pivot_longer()`. You can check the output of `a %>% pivot_longer(cols =c(b,c))` and you will see what I mean – MKR Feb 27 '20 at 08:46
1

Maybe a bit easier, also using ggplot:

library(tidyverse)

df <- bind_rows(a1,a2,a3, .id = "id") %>% 
  gather(`b`, `c`, key = "values", value = "value")

  ggplot(df, aes(a, value, colour = name)) + 
  geom_line() +
  facet_wrap(~id, labeller = labeller(id = c(`1` = "first", `2` = "second", `3` = "third")))

enter image description here

user213544
  • 2,046
  • 3
  • 22
  • 52
1

You can use the following code modified from @user213544 to color b and c

library(tidyverse)

a1<-data.frame(a=c(1,2,3),b=c(45,34,67),c=c(35,56,89))
a2<-data.frame(a=c(1,2,3),b=c(35,40,60),c=c(30,59,92))
a3<-data.frame(a=c(1,2,3),b=c(45,38,57),c=c(35,52,91))

df <- bind_rows(a1,a2,a3, .id = "id")

df %>% pivot_longer(-c(a,id)) %>% 
  ggplot(aes(x = a, y = value, colour = name)) + geom_line()+ 
  facet_wrap(~id, labeller = labeller(id = c(`1` = "a1", `2` = "a2", `3` = "a2")))+
  labs(title="Plot title",x="a", y = "b & c")

enter image description here

Update

a1<-data.frame(a=c(1,2,3),b=c(45,34,67),c=c(35,56,89),d=c("first"))
a2<-data.frame(a=c(1,2,3),b=c(35,40,60),c=c(30,59,92),d=c("second"))
a3<-data.frame(a=c(1,2,3),b=c(45,38,57),c=c(35,52,91),d=c("third"))

df <- bind_rows(a1,a2,a3)

df %>% pivot_longer(-c(a, d)) %>% 
  ggplot(aes(x = a, y = value, colour = name)) + geom_line()+ 
  facet_wrap(~d)+
  labs(title="Plot title",x="a", y = "b & c")

enter image description here

Community
  • 1
  • 1
UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • The code works. But not for my actual example. I will try to see what the issue is. By the way, what is colour=name? – user17144 Feb 27 '20 at 09:25
  • If you run `df %>% pivot_longer(-c(a, d))` this part of the code it will make your b and c as name column. You just run that part, you will able to get your answer on your own. Don't forget to accept it as answer. – UseR10085 Feb 27 '20 at 09:29