Home > Blockchain >  Change ggplot bar chart fill colors
Change ggplot bar chart fill colors

Time:02-11

With this data:

df <- data.frame(value =c(20, 50, 90), 
                 group = c(1, 2,3))

I can get a bar chart:

df %>% ggplot(aes(x = group, y = value, fill = value))  
  geom_col()  
  coord_flip()  
  scale_fill_viridis_c(option = "C")  
  theme(legend.position = "none")

enter image description here

But I would like to have the colors of those bars to vary according to their corresponding values in value.

I have managed to change them using geom_raster:

ggplot()   
  geom_raster(aes(x = c(0:20), y = .9, fill = c(0:20)),  
              interpolate = TRUE)  
  geom_raster(aes(x = c(0:50), y = 2, fill = c(0:50)), 
              interpolate = TRUE)  
  geom_raster(aes(x = c(0:90), y = 3.1, fill = c(0:90)),               
              interpolate = TRUE)  
  scale_fill_viridis_c(option = "C")  
  theme(legend.position = "none")

enter image description here

This approach is not efficient when I have many groups in real data. Any suggestions to get it done more efficiently would be appreciated.

I found enter image description here

If this is too pixilated you can increase the number of rows generated by replacing list(0:value) with seq(0, value, by = 0.1).

enter image description here

CodePudding user response:

This is a real hack using ggforce. This package has a geom that can take color gradients but it is for a line segment. I've just increased the size to make the line segment look like a bar. I made all the bars the same length to get the correct gradient, then covered a portion of each bar over with the same color as the background color to make them appear to be the correct length. Had to hide the grid lines, however. :-)

df %>% 
    ggplot()   
    geom_link(aes(x = 0, xend = max(value), y = group, yend = group, color = stat(index)), size = 30)  
    geom_link(aes(x = value, xend = max(value), y = group, yend = group), color = "grey", size = 31)  
    scale_color_viridis_c(option = "C")  
    theme(legend.position = "none", panel.background = element_rect(fill = "grey"), 
          panel.grid = element_blank())  
    ylim(0.5, max(df$group) 0.5 )

enter image description here

  • Related