Home > database >  How can I customize certain labels, but keep default values, in ggplot guide_colorsteps?
How can I customize certain labels, but keep default values, in ggplot guide_colorsteps?

Time:07-18

I am trying to selectively label a subset of a color bar using geom_contour_filled, while continuing to use the default ggplot colour palette. I've figured out how to do the first part by adapting the function from this post: desired label format

However, my goal is to produce a plot with the default color palette used by geom_contour_filled, like the one below.

ggplot(data=faithfuld, aes(x=waiting, y=eruptions))  
  geom_contour_filled(aes(z=100*density),show.legend=T)  
  theme(legend.position="top")

desired color palette

CodePudding user response:

Thanks for commenting and clarifying your question; the default palette used for geom_contour_filled() is the viridis palette. One potential solution to your problem is to use the scale_fill_viridis_d() function, e.g.

library(tidyverse)

fun_lab <- function(x) {
  x[!(x %in% c(1, 2, 3))]<- "" # selected values to label
  return(x)
}

ggplot(data=faithfuld, aes(x=waiting, y=eruptions))  
  geom_contour_filled(aes(z=100*density),show.legend=T)  
  scale_fill_viridis_d(labels = fun_lab,
                       guide = guide_colorsteps(direction="horizontal",
                                               title.position="top"))  
  theme(legend.position="top")

Created on 2022-07-18 by the reprex package (v2.0.1)

  • Related