Reputation: 566
lets say I have this data:
d1 <- data.frame(x = letters[1:3], y=LETTERS[24:26], num = 1:3)
d2 <- data.frame(x = letters[1:3], y=LETTERS[24:26], num = c(1,2,30))
library(gridExtra)
library(ggplot2)
ggd1 <- ggplot(d1, aes(x=x,y=y)) +
geom_tile(aes(fill=num)) +
scale_fill_gradient(low = "green", high = "blue")
ggd2 <- ggplot(d2, aes(x=x,y=y)) +
geom_tile(aes(fill=num)) +
scale_fill_gradient(low = "green", high = "blue")
grid.arrange(ggd1,ggd2)
My question is how can I standardise the fill gradient so that even though the extent of the data in d1 and d2 differ, colours for X-a
,Y-b
shoud match for both plots but Z-c
should differ by an order of magnitude. i.e. I want to keep the same scale for both plots.
Upvotes: 2
Views: 1962
Reputation: 67788
If you wish the scale of the plots to be the same, it may suffice with one scale. Then, instead of using grid.arrange
, you may bind the two data sets together, and add a grouping variable which can be used in facet_wrap
:
d3 <- rbind(d1, d2)
d3$grp <- rep(1:2, each = 3)
# You may also create a group variable before binding the df:s together, e.g.
d1$grp <- 1
d2$grp <- 2
rbind(d1, d2)
ggplot(d3, aes(x = x, y = y)) +
geom_tile(aes(fill = num)) +
scale_fill_gradient(low = "green", high = "blue") +
facet_wrap(~ grp, ncol = 1)
Upvotes: 4
Reputation: 52677
Add limits to each plot:
ggd1 <- ggplot(d1, aes(x=x,y=y)) +
geom_tile(aes(fill=num)) +
scale_fill_gradient(low = "green", high = "blue", limits=c(1, 30))
ggd2 <- ggplot(d2, aes(x=x,y=y)) +
geom_tile(aes(fill=num)) +
scale_fill_gradient(low = "green", high = "blue", limits=c(1, 30))
grid.arrange(ggd1,ggd2)
Upvotes: 7