I want to make three graphs of which each is based on a subset of the same dataset. I have a continuous covariate I want to illustrate using a gradient. The issue is that for each of my subsets, the ranges of the covariate are vastly different, so it doesn't turn out right when they are plotted together with one mutual legend. When I plot all together using ggarrange, the legend for the first plot in the order is used.
In the example below, the ranges for sepal length vary vastly between species; the highest value for sepal length for Setosa is the lowest for Versicolor. It looks alright when each plot is plotted separately (note the min and max values in the gradient), but when they are plotted together, the legend is misleading, since red color (max sepal length) means different things for each species.
What is needed is that the gradient stretches from the lowest to highest value in the whole dataset. Probably this needs to be done in some other way, not as separate graphs perhaps. Any advice?
library(ggpubr)
library(ggplot2)
data(iris)
se <-subset(iris, iris$Species == "setosa")
ve <-subset(iris, iris$Species == "versicolor")
vi <-subset(iris, iris$Species == "virginica")
a<-ggplot(se, aes(y=Petal.Length, x=Petal.Width))
geom_point(aes(colour = Sepal.Length), size =2)
scale_color_gradient(low = "blue", high = "red")
a
b<-ggplot(ve, aes(y=Petal.Length, x=Petal.Width))
geom_point(aes(colour = Sepal.Length), size =2)
scale_color_gradient(low = "blue", high = "red")
b
c<-ggplot(vi, aes(y=Petal.Length, x=Petal.Width))
geom_point(aes(colour = Sepal.Length), size =2)
scale_color_gradient(low = "blue", high = "red")
c
ggarrange(a,b,c, ncol=3, nrow=1, common.legend = TRUE,label.y = 0, legend="right")
CodePudding user response:
You need to specify the same limits
and breaks
for the color scale in each plot:
a <- ggplot(se, aes(y=Petal.Length, x=Petal.Width))
geom_point(aes(colour = Sepal.Length), size =2)
scale_color_gradient(low = "blue", high = "red", breaks = 4:8,
limits = c(4, 8))
b <- ggplot(ve, aes(y=Petal.Length, x=Petal.Width))
geom_point(aes(colour = Sepal.Length), size =2)
scale_color_gradient(low = "blue", high = "red", breaks = 4:8,
limits = c(4, 8))
c <- ggplot(vi, aes(y=Petal.Length, x=Petal.Width))
geom_point(aes(colour = Sepal.Length), size =2)
scale_color_gradient(low = "blue", high = "red", breaks = 4:8,
limits = c(4, 8))
ggarrange(a,b,c, ncol=3, nrow=1, common.legend = TRUE,label.y = 0,
legend="right")