Home > Software design >  Problem with "fill" argument data visualisation R, ggplot2
Problem with "fill" argument data visualisation R, ggplot2

Time:12-28

I currently have an issue regarding how the "fill" argument colors the different bars in my plot. I wish to have a predetermined ranking of the colors such that each value in the table that I'm plotting, corresponds to a color instead of in my case, where it varies. The way it works in my code is that the highest value in my table corresponds to the lightest color (in the default ggplot palette), and the lowest value corresponds to the darkest color. My wish specifically is that 100 should be what corresponds to the lightest color and 0 corresponding to the darkest color (as I'm plotting quantiles) and of course, everything in-between.

Here is my code.

library(tidyverse)

test <- tibble(factors=c("fact1","fact2","fact3","fact4"),
               percent=c(18.3,42.6,81.1,62.8))

ggplot(test)  
  geom_hline(
    aes(yintercept = y), 
    data.frame(y = c(0:4) * 25),
    color = "lightgrey"
  )   
  
  geom_col(
    aes(
      x = reorder(str_wrap((factors), 5), percent),
      y = percent,
      fill = percent
    ),
    position = "dodge2",
    show.legend = TRUE,
    alpha = .9,
    width=0.8
    
  )  
  geom_segment(
    aes(
      x = reorder(str_wrap(factors, 5), percent),
      y = 0,
      xend = reorder(str_wrap(factors, 5), percent),
      yend = 100
    ),
    linetype = "dashed",
    color = "gray12"
  )  
  
  scale_y_continuous(
    limits = c(-50, 100),
    expand = c(0, 0),
    breaks = c(0, 25, 50, 75,100)
  ) 
coord_polar() 

If you change the values in the "percent" part of the "test" tibble, to something like percent=c(80.1,80.5,80.1,79.9) you'll quickly understand what I am talking about, as in my ideal case, It should only be showing four pretty much identical colors instead of the range of colors being determined from the highest to lowest of the "percent" column, showing dark to light colors.

Thanks in advance!

CodePudding user response:

First you should create a palette:

my_palette <- colorRampPalette((RColorBrewer::brewer.pal(9, "Blues")))

Then you can add a scale_fill_gradientn to your ggplot:

test <- tibble(factors=c("fact1","fact2","fact3","fact4"),
               # percent=c(18.3,42.6,81.1,62.8))
               percent = c(80.1,80.5,80.1,79.9))


ggplot(test)  
  geom_hline(
    aes(yintercept = y), 
    data.frame(y = c(0:4) * 25),
    color = "lightgrey"
  )   
  
  geom_col(
    aes(
      x = reorder(str_wrap((factors), 5), percent),
      y = percent,
      fill = percent
    ),
    position = "dodge2",
    show.legend = TRUE,
    alpha = .9,
    width=0.8
    
  )  
  geom_segment(
    aes(
      x = reorder(str_wrap(factors, 5), percent),
      y = 0,
      xend = reorder(str_wrap(factors, 5), percent),
      yend = 100
    ),
    linetype = "dashed",
    color = "gray12"
  )  
  
  scale_y_continuous(
    limits = c(-50, 100),
    expand = c(0, 0),
    breaks = c(0, 25, 50, 75,100)
  ) 
  
  scale_fill_gradientn(colours = my_palette(100), 
                         limits=c(0,100)) 

  coord_polar()

enter image description here

  • Related