Keep same scale in different graphs ggplot2

2019-07-21 22:51发布

I want to create 3 graphs in ggplot2 as follows:

ggplot(observbest,aes(x=factor(iteration),y=bottles,colour=Team ,group=Team)) + geom_line() + scale_colour_gradientn(colours=rainbow(16)) 
ggplot(observmedium,aes(x=factor(iteration),y=bottles,colour=Team ,group=Team)) + geom_line() + scale_colour_gradientn(colours=rainbow(16))
ggplot(observweak,aes(x=factor(iteration),y=bottles,colour=Team ,group=Team)) + geom_line() + scale_colour_gradientn(colours=rainbow(16))

That is, three graphs displaying the same thing but for difference dataset each time. I want to compare between them, therefore I want their y axis to be fixed to the same scale with the same margins on all graphs, something the currently doesn't happen automatically.

Any suggestion?

Thanks

标签: r ggplot2
3条回答
啃猪蹄的小仙女
2楼-- · 2019-07-21 23:15

It sounds like a facet_wrap on all the observations, combined into a single dataframe, might be what you're looking for. E.g.

library(plyr)
library(ggplot2)

observ <- rbind(
  mutate(observbest, category = "best"),
  mutate(observmedium, category = "medium"),
  mutate(observweak, category = "weak")
)

qplot(iteration, bottles, data = observ, geom = "line") + facet_wrap(~category)

enter image description here

查看更多
Anthone
3楼-- · 2019-07-21 23:16

Use scale_y_continuous to define the y axis for each graph and make them all easily comparable.

查看更多
我命由我不由天
4楼-- · 2019-07-21 23:25

Add + ylim(min_value,max_value) to each graph.

Another option would be to merge the three datasets with an id variable identifying which value is in which dataset, and then plot the three of them together, differentiating them by linetype for instance.

查看更多
登录 后发表回答