-1

So i created a ggplot as below;

enter image description here

using this code:

ggplot(dataset1, aes(x = y, y = x)) + geom_smooth(span=0.2) + ylim(0,5) + xlim(0,23) + ylab("Count") 
  labs(x="Hours") +
  theme_classic()

i then wanted to add an additonal 3 lines to this graph and so tried this code:

ggplot(rbind(dataset1,dataset2,dataset3,dataset4), aes(x = y, y = x)) + geom_smooth(span=0.2) + ylim(0,5) + xlim(0,23) + ylab("count") +
  labs(x="Hours") +
  theme_classic()

however the graph i was then given was as seen below: enter image description here

which is no where near what I'm trying to do.

I also got an error message after i did this code such as; Warning message: Removed 1 rows containing non-finite values (stat_smooth).

I know i'm going majorly wrong with the second code and probably missing out a part of it but this code isnt something I've used before so just trying my hand at trying to get around it.

Thanks for any help!

Marc
  • 19,394
  • 6
  • 47
  • 51
nickie_5
  • 1
  • 2
  • You need to do three separate calls, the data call can only process one data frame. For example https://stackoverflow.com/questions/9109156/ggplot-combining-two-plots-from-different-data-frames or https://stackoverflow.com/questions/27003562/plotting-two-different-data-frames-on-same-figure-in-ggplot2 – Michelle May 26 '20 at 19:41

1 Answers1

0

If you need one line per dataset you should add some sort of category/grouping variable. By combining your data you just create one big dataset so ggplot has no way of knowing it should plot them separately.

dataset1$category <- 1
dataset2$category <- 2
...

Now you can create your new data and then add for example color = category to your aesthetics.

ek-g
  • 681
  • 3
  • 7