Keep same scale in different graphs ggplot2

2019-07-21 22:22发布

问题:

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

回答1:

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)



回答2:

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.



回答3:

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



标签: r ggplot2