Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually set scale of scale_fill_distiller()

Tags:

r

ggplot2

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")

enter image description here

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")

enter image description here

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 withscale_fill_gradientn`?

like image 558
boshek Avatar asked Jul 20 '16 22:07

boshek


1 Answers

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)))

enter image description here

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)))

enter image description here

like image 109
aosmith Avatar answered Nov 16 '22 02:11

aosmith