I have a colorbar within ggplot graph ranging from white to red and the white border is not very good visible on the white background.
Is there a way to color the tick marks in the legend differently or adding a border around the gradient scale?
Here is a minimal example:
df <- data.frame(x <- rnorm(10),
y <- rnorm(10),
fill <- rnorm(10))
ggplot(df, aes(x, y, fill = fill)) +
geom_point() +
scale_fill_gradient(low = 'white', high = 'red')
On the Display tab, expand Background. For Symbol, click the down arrow to open the color palette. Choose a color to change the legend background.
You can place the legend literally anywhere. To put it around the chart, use the legend. position option and specify top , right , bottom , or left . To put it inside the plot area, specify a vector of length 2, both values going between 0 and 1 and giving the x and y coordinates.
You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))
In the latest ggplot2 (development version, to be released as 2.3), this now works:
# devtools::install_github("tidyverse/ggplot2") # do this once
library(ggplot2)
df <- data.frame(x <- rnorm(10),
y <- rnorm(10),
fill <- rnorm(10))
ggplot(df, aes(x, y, fill = fill)) +
geom_point() +
scale_fill_gradient(low = 'white', high = 'red',
guide = guide_colorbar(frame.colour = "black", ticks.colour = "black"))
Found a solution for my problem by using element_rect
and fill
argument.
df <- data.frame(x <- rnorm(10),
y <- rnorm(10),
fill <- rnorm(10))
ggplot(df, aes(x, y, fill = fill)) +
geom_point() +
scale_fill_gradient(low = 'white', high = 'red') +
theme(legend.background = element_rect(fill = "grey95"))
I would prefer a border just around the colorbar, but this seems not possible...
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