My question is the same as this one, but I'm seeing the mid
color, but not the low
color in scale_fill_gradient2()
. How can I display all low, mid and high colours in scale_fill_gradient2()
?
- No sign of green:
- code:
library(tidyverse)
library(forcats)
df %>%
ggplot(aes(y = prop, x = fct_rev(var),
fill = as.integer(df$var))) + ## HACK to convert discrete to cont
geom_bar(stat = "identity", width = 0.3) +
scale_fill_gradient2(name = "category",
low = "green",
mid= "orange",
high = "purple",
space = "Lab",
# midpoint = 0,
guide= 'legend',
# limits = c(-7, 7), ## tried the other post's solution: not working
) +
coord_flip(clip = "off") +
Question : Why is green nowhere to be found?
data:
structure(list(var = structure(1:7, .Label = c("VAR_A", "VAR_B",
"VAR_C", "VAR_D", "VAR_E", "VAR_F", "VAR_G"), class = "factor"),
x = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("no",
"yes"), class = "factor"), n = c(7L, 30L, 49L, 48L, 47L,
39L, 21L), prop = c(13.73, 58.82, 96.08, 94.12, 92.16, 76.47,
41.18)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -7L), groups = structure(list(var = structure(1:7, .Label = c("VAR_A",
"VAR_B", "VAR_C", "VAR_D", "VAR_E", "VAR_F", "VAR_G"), class = "factor"),
.rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -7L), .drop = TRUE))