1

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:

where is 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))
Larissa Cury
  • 806
  • 2
  • 11
  • In your code, `offerFilt` is not defined and `df$var` has values such as `"VAR_A"`, so `as.integer(df$var)` unsurprisingly produces a column of `NA`s. Voting to close as not reproducible. – Limey Mar 07 '23 at 13:56
  • oh, sorry, that was the name of my original dataset. Now it's ok. Check it out. @Limey – Larissa Cury Mar 07 '23 at 13:57
  • This is also not reproducible: ! could not find function "fct_rev" – markhogue Mar 07 '23 at 13:57
  • @markhogue is from ```forcats``` https://forcats.tidyverse.org/reference/fct_rev.html I've added to make ```coord_flip()``` work. I've edited the post. – Larissa Cury Mar 07 '23 at 13:58
  • 1
    the midpoint of scale_fill_gradient defaults to 0. If you put midpoint = 4, green should show. – Wolfgang Arnold Mar 07 '23 at 14:01
  • Great! @WolfgangArnold , thank you! Now I can see green! but then why am I getting almost solid colors in the scale? I wish I could get gradients inside the bars, not only a gradient of solid colors... any ideas? Ex: https://d33wubrfki0l68.cloudfront.net/120e02325746f34849996f1ace77ae7d4467b8ea/f535b/images/color-gradient-text-overlay.png – Larissa Cury Mar 07 '23 at 14:05
  • 1
    If you want a gradient within a bar, check https://stackoverflow.com/questions/48210231/creating-a-vertical-color-gradient-for-a-geom-bar-plot – tjebo Mar 07 '23 at 14:18
  • You're welcome :-) - if you want the bars themselves being filled with gradient colours, then my only idea would be a workaround to "simulate" the gradient through stacked bars (which does not sound like a very elegant solution). – Wolfgang Arnold Mar 07 '23 at 14:45
  • Thanks! I was *almost* able to do so with @tjebo 's link: https://stackoverflow.com/questions/75664624/show-proportions-on-a-gradient-in-ggplot2 but I still need some help – Larissa Cury Mar 07 '23 at 16:26
  • if you consider this question here closed, maybe close it... ? – tjebo Mar 07 '23 at 16:31
  • indeed, Wolfgang Arnold's comment solved my problem, but since it's not an answer but a comment, how do I close it, then? The comment solved it – Larissa Cury Mar 07 '23 at 16:40

0 Answers0