I am trying to make a series of plots for comparison. Say for example, I wanted to use the iris
data set to make a plot like this where I have filtered to view only the setosa species:
library(ggplot2)
library(dplyr)
iris %>%
filter(Species=="setosa") %>%
ggplot(aes(y = Sepal.Width, x = Sepal.Length, fill = Petal.Length)) +
geom_tile(stat = "identity") +
scale_fill_distiller(palette = "Spectral")
Now if I want to plot the versicolor species in the same way I can do it like this:
iris %>%
filter(Species=="versicolor") %>%
ggplot(aes(y = Sepal.Width, x = Sepal.Length, fill = Petal.Length)) +
geom_tile(stat = "identity") +
scale_fill_distiller(palette = "Spectral")
My issue is that the fill scales are different for these plot. I know I could facet_wrap
these to solve this but let's just say I need individual plots. What I would like is to apply the same fill scale to both plots. Ideally I'd like to keep using scale_fill_distiller
but I wasn't able to get very far as the values argument is a bit different for that function, so I tried scale_fill_gradientn
. First I established a fill_range
like so:
fill_range <- seq(min(iris$Petal.Length), max(iris$Petal.Length), by=0.2)
Then I tried inputting the seq into the values argument in scale_fill_gradientn
like this:
iris %>%
filter(Species=="versicolor") %>%
ggplot(aes(y = Sepal.Width, x = Sepal.Length, fill = Petal.Length)) +
geom_tile(stat = "identity") +
scale_fill_gradientn(colours = terrain.colors(length(fill_range)),
values=fill_range)
iris %>%
filter(Species=="setosa") %>%
ggplot(aes(y = Sepal.Width, x = Sepal.Length, fill = Petal.Length)) +
geom_tile(stat = "identity") +
scale_fill_gradientn(colours = terrain.colors(length(fill_range)),
values=fill_range)
Neither of these produced a desired result, plotting a bunch of grey tiles and each with one green one.
Does anyone have an idea where I can manually specify the scale for scale_fill_distiller"? If that is not possible, can anyone see where I have gone wrong with
scale_fill_gradientn`?
You can set the limits
to be the same in both plots within scale_fill_distiller
.
Using the min
and max
of Petal.Length
as the limits:
iris %>%
filter(Species=="setosa") %>%
ggplot(aes(y = Sepal.Width, x = Sepal.Length, fill = Petal.Length)) +
geom_tile(stat = "identity") +
scale_fill_distiller(palette = "Spectral",
limits = c(min(iris$Petal.Length), max(iris$Petal.Length)))
iris %>%
filter(Species=="versicolor") %>%
ggplot(aes(y = Sepal.Width, x = Sepal.Length, fill = Petal.Length)) +
geom_tile(stat = "identity") +
scale_fill_distiller(palette = "Spectral",
limits = c(min(iris$Petal.Length), max(iris$Petal.Length)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With