0

I have a dataset:

dd <- data.frame(name = c(1,2,3,4,5,6,7,8,9,10,11,12), value = c(2,4,5,1,2,3,4, 7,8,10,14,20),type = c('a', 'a', 'a', 'b', 'b', 'b', 'b','c','c','c', 'c', 'c'))

I want to make plots:

library(ggplot2)
ggplot(dd, aes(x=name, y = value, color = type)) +
  geom_smooth()

They look like this: enter image description here

How could i put this plots on three separate graphs, so they will be compactly located one after one? What should i add or change in ggplot function?

P.S. It's just an example, im looking for some solution for case when there is not three but 14 types

2 Answers2

0

Maybe you're after facetting, with scales set to be "free".

ggplot(dd, aes(x=name, y = value, color = type)) +
  geom_smooth() +
  facet_wrap(~type, scales="free")

You can determine how many rows and columns you need with the nrow and ncolarguments.

Edward
  • 10,360
  • 2
  • 11
  • 26
  • thanks, and how to change sizes of facets? make each of them wider or higher? –  May 02 '20 at 00:40
  • Why do you keep deleting this question? You just stretch using the mouse. Or you can try the solutions from here for more complicated ways: https://stackoverflow.com/questions/49110877/how-to-adjust-facet-size-manually – Edward May 02 '20 at 00:46
0

I think you meant to put subplots graphs in the same picture. You can do it adding a +facet_grid() subsetting with the variable "type". In this case I modified your ggplot like this:

ggplot(dd, aes(x=name, y = value, color = type)) +
  geom_smooth()+facet_wrap(type~.)

which gives you this graphfirst graph

But if you see the rest of subplots too warped because of the c type, as Edward said, you can add a scales="free" so every subplot is shown according to its scale. You can also decide to put a free scale in only one axis if you see the distortion only affects to one dimension, like in this graph, where only the y scale is free, but that's up to you:

ggplot(dd, aes(x=name, y = value, color = type)) +
  geom_smooth()+facet_wrap(type~., scales="free_y")

second graph

In any case in cookbook-r.com they explain it quite well. In my case I got that trick from the R for Data Analysis book.

Hope this helped you!

Enrique
  • 23
  • 5