-1

I have asked the similar question an hour ago, got some nice answers, but not the one I was looking for, probibly because my question was not formulated in the correct way. That is way I post it again I have plotted different graphs in my R-Script with the help of ggplot. To compare them I need to integrate them into one graph.

this is my current code for the single graphs:

p1 <- ggplot(merch42, aes(x = day_code, y = avg_logistic_review_score, col = "red"))+   geom_smooth(method = "loess", span = 1/25, col = "red")

p2 <- ggplot(merch323, aes(x = day_code, y = avg_logistic_review_score, col = "blue"))+
  geom_smooth(method = "loess", span = 1/25, col = "blue")

p3 <- ggplot(merch24, aes(x = day_code, y = avg_logistic_review_score, col = "green"))+
  geom_smooth(method = "loess", span = 1/25, col = "green")

p4 <- ggplot(merch180, aes(x = day_code, y = avg_logistic_review_score, col = "yellow"))+
  geom_smooth(method = "loess", span = 1/25, col = "yellow")

p5 <- ggplot(merch505, aes(x = day_code, y = avg_logistic_review_score, col = "merch505"))+
  geom_smooth(method = "loess", span = 1/25, col = "black")

Has someone an idee how this works? thanks very much:) Phil

Here I have already compared them on one page. Now I need to integrate all in on systems of coordinates.
enter image description here

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
Phil R
  • 1
  • 1
    What you're looking for is called faceting, and it's built into `ggplot`. You want your data to be in a single, long shaped dataframe, and then you add `facet_wrap(~ type)`, where type is whatever column you use to differentiate between the chunks of data that go into each plot. If you post your data, I or someone else can give you an easy solution. Also take a look at the docs for `facet_wrap`. – camille Apr 25 '18 at 14:44
  • You should edit your previous question. – MLavoie Apr 26 '18 at 00:06

2 Answers2

3

Consider stacking (i.e., row-binding) all dataframes adding an indicator variable like type to each one and then plotting with color mapped to the indicator variable and even define manual colors:

final_df <- rbind(transform(merch42, type = "merch42"),
                  transform(merch323, type = "merch323"),
                  transform(merch24, type = "merch24"),
                  transform(merch180, type = "merch180"),
                  transform(merch505, type = "merch505"))

ggplot(final_df, aes(x = day_code, y = avg_logistic_review_score, color = type)) +
  geom_smooth(method = "loess", span = 1/25) +
  scale_color_manual(values = c("red", "blue", "green", "yellow", "black"))
Parfait
  • 104,375
  • 17
  • 94
  • 125
0

I am not sure if I understand well your problem, but if you want to have your five lines into a single plot, you can do the following:

p <- ggplot(merch42, aes(x = day_code, y = avg_logistic_review_score)) +
geom_smooth(method = "loess", span = 1/25, col = "red") + 
geom_smooth(merch323, aes(x = day_code, y = avg_logistic_review_score),
method = "loess", span = 1/25, col = "blue")+
geom_smooth(merch24, aes(x = day_code, y = avg_logistic_review_score),
method = "loess", span = 1/25, col = "green") + 
geom_smooth(merch180, aes(x = day_code, y = avg_logistic_review_score),
method = "loess", span = 1/25, col = "yellow") + 
geom_smooth(merch505, aes(x = day_code, y = avg_logistic_review_score), 
method = "loess", span = 1/25, col = "black")
Mel Sscn
  • 96
  • 3