简体   繁体   中英

Filling bars with a color gradient in barplot ggplot2 R

I want to fill bars with collors accotrding to a color scale. The problem is that I can achieve that ggplot2 fill all bars with desired tones. I am doing:

p <- ggplot(data=base_c2, aes(x=c2_inicio, y=incid_usuar)) +
  geom_bar(stat="identity", width=0.8, fill=base_c2$incid_usuar) +
 scale_color_gradient2(midpoint=mean(base_c2$incid_usuar), 
                        low ="#91CF60",mid = "#FFFFBF", high = "#FC8D59",
                         space ="Lab")
p

but the result is: 在此处输入图片说明

how can achieve a smoooth grtadient? (green in low values, yellow in values close to the mean, red for high values)

this way:

p <- 
  ggplot(data=base_c2, aes(x=c2_inicio, y=incid_usuar, 
                            fill=base_c2$incid_usuar)) +
  geom_bar(stat="identity", width=0.8) +
  theme_void() +
  scale_fill_gradient2(midpoint=mean(base_c2$incid_usuar), 
                       low ="#91CF60",mid = "#FFFFBF", high = "#FC8D59",
                       space ="Lab") + 
  labs(title = "Numero de incidentes atendidos por usuario según C2")
p

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM