Is there a generalized version of scale_fill_distiller and scale_colour_distiller?

stats.and.r

I do different plots with ggplot2 and want them all to have the same colour. The thing is that I use different geoms that sometimes work with fill and sometimes with col. See here:

df <- data.frame(x= 1:10, y= 1:10, col= c(1:5, 5:1))
library(ggplot2)
gg1 <- ggplot(df, aes(x, group= x, fill= col)) +
  geom_bar()
gg2 <- ggplot(df, aes(x, y, col= col)) +
  geom_point()

If I want both ggplot2 objects to have the same colour palette, say Spectral, I would need to do gg1 + scale_fill_distiller(palette = "Spectral") and gg2 + scale_colour_distiller(palette = "Spectral"). Pay attention to the difference "scale_fill_distiller" and "scale_colour_distiller". If we mix this up, the plot changes.

I have many plots and do not instantly know whether a col or fill was used, i.e. I have to manually print the plot before I can actually add the palette. So the question is whether there is a generalized function which adds a colour palette to any col, fill, whatever easthetic. So the function is suppost for gg1 and gg2 like this: gg1 + scale_generalized_distiller(palette = "Spectral") and gg2 + scale_generalized_distiller(palette = "Spectral")


What I do so far:

I simpy add all whats possible. Like this:

gg1  +
  scale_colour_distiller(palette = "Spectral") +
  scale_fill_distiller(palette = "Spectral")
gg2 +
  scale_colour_distiller(palette = "Spectral") +
  scale_fill_distiller(palette = "Spectral")

I hope there is a more elegant way.

Maël

You can use the aesthetics parameter, either with scale_color_distiller or scale_fill_distiller:

gg1  +
  scale_color_distiller(palette = "Spectral", aesthetics = c("colour", "fill"))

gg2 +
  scale_color_distiller(palette = "Spectral", aesthetics = c("colour", "fill"))

Checks

ggFill1 <- gg1  +
  scale_fill_distiller(palette = "Spectral", aesthetics = c("colour", "fill"))
ggColor1 <- gg1 +
  scale_color_distiller(palette = "Spectral", aesthetics = c("colour", "fill"))
all.equal(ggFill1,ggColor1)
# [1] TRUE

ggFill2 <- gg2  +
  scale_fill_distiller(palette = "Spectral", aesthetics = c("colour", "fill"))
ggColor2 <- gg2  +
  scale_color_distiller(palette = "Spectral", aesthetics = c("colour", "fill"))
all.equal(ggFill2,ggColor2)
# [1] TRUE

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Manually set scale of scale_fill_distiller()

Can I import a palette in scale_fill_distiller?

scale_fill_distiller ggplot legend missing for some data frames but not others

Adjusting output from scale_fill_distiller in stat_bin2d

How to rescale color mapping in scale_color_distiller (ggplot2)?

generalized find() with multiple occupation a vectorized version

Combining scale_colour and scale_fill in same function

Programmatically make colour slightly darker version of fill

D3.js -- Ordinal scale (version 3 to version 4)

R colour scale for logarithmic data?

Scale colour over multiple graphs

ggmap and scale_colour_brewer

Colour scale HP Bar colors?

Does C++ standard library provide more compact and generalized version of the erase–remove idiom?

Plotly: changing the colour scale (colour gradient) to a specific range of colour

`fill` scale is not shown in the legend

Manual colour scale in faceted ggplot waffle chart

AM Charts Defined Colour Scale on Heat Map

Converting colour image to grey scale (Matlab)

ggplot grouped plot scale colour gradient

Restrict viridis colour scale to between two values

ggplot legend not working with scale_colour_manual

chose colors for scale_colour_colorblind() in ggthemes

Set continuous colour scale in ggplot2

ggplot, scale_colour_manual issues

Excel Conditional Formatting - sum column and colour scale

ggplot2 scale_fill_gradient2 not showing mid colour

Fill or colour in continuous scale through gradient of more than 2 defined colours

When using scale_fill_manual() in a loop, only the colour values assigned in the last iteration are retained