horizontal barplot with color gradient from top to bottom of the graphic

99封情书 提交于 2019-12-24 10:47:04

问题


I would like to fill the bars in a gradient of color from the top to the bottom of the graphic. That is, regardless of the bar lenght, the color should be getting lighter trhough the top to the bottom direction. I created 2 SIMULATIONS in the photoshop. The first one (Fig. 1) is that ploted through the follow script (long bars are dark and short bars are light). However the seconde one (Fig. 2) is that I would like to elaborate (the top bars are dark and get lighter in the bottom direction).

specie	momento	M1_sava
zr	M1	1
yr	M1	0.86
xr	M1	1
wr	M1	1
vr	M1	1
ur	M1	0.74
tr	M1	0.39
sr	M1	0.83
rr	M1	0.83
qr	M1	0
pr	M1	0.7
or	M1	0.11
nr	M1	1
mr	M1	0
lr	M1	0
kr	M1	0
jr	M1	0
Ir	M1	0.83
hr	M1	0
Gr	M1	0
fr	M1	0
er	M1	0
dr	M1	0
cr	M1	0.04
br	M1	0
ar	M1	0
zr	M2	0.33
yr	M2	0.86
xr	M2	0.39
wr	M2	0.02
vr	M2	0.07
ur	M2	0.02
tr	M2	0.87
sr	M2	0.06
rr	M2	0.63
qr	M2	0.33
pr	M2	0.91
or	M2	0.67
nr	M2	0.18
mr	M2	0.08
lr	M2	0
kr	M2	0
jr	M2	0
Ir	M2	0
hr	M2	0.08
Gr	M2	0
fr	M2	0.04
er	M2	0
dr	M2	0
cr	M2	0
br	M2	0
ar	M2	0
zr	M3	0.04
yr	M3	0.32
xr	M3	0.02
wr	M3	0
vr	M3	0
ur	M3	0
tr	M3	0.96
sr	M3	0.06
rr	M3	0.18
qr	M3	0.33
pr	M3	0.63
or	M3	1
nr	M3	0
mr	M3	0.94
lr	M3	0.17
kr	M3	0
jr	M3	0.41
Ir	M3	0.04
hr	M3	0.44
Gr	M3	0.17
fr	M3	0.02
er	M3	0
dr	M3	0
cr	M3	0
br	M3	0
ar	M3	0
zr	M4	0
yr	M4	0
xr	M4	0
wr	M4	0
vr	M4	0
ur	M4	0
tr	M4	0.89
sr	M4	0
rr	M4	0.03
qr	M4	0.22
pr	M4	0.41
or	M4	0.46
nr	M4	0
mr	M4	0.81
lr	M4	0.39
kr	M4	0.7
jr	M4	0.7
Ir	M4	0
hr	M4	0.87
Gr	M4	0.91
fr	M4	0.33
er	M4	0.37
dr	M4	0.24
cr	M4	0.15
br	M4	0
ar	M4	0
Dataset <- read.csv(file = "data.csv", header = TRUE, sep = ";")
attach(Dataset)
library(ggplot2)
ggplot(Dataset, aes(specie, M1_sava)) + 
  facet_wrap(~ momento, nrow = 1) + 
  coord_flip() + 
  geom_col(aes(fill = M1_sava)) + 
  scale_fill_gradient2(low = "white", high = "red") + 
  theme_bw(base_size = 10) 

回答1:


Your code is almost there, you just got the color variable wrong.
(Also, don't attach data, please.)

library(ggplot2)

ggplot(Dataset, aes(specie, M1_sava)) + 
  facet_wrap(~ momento, nrow = 1) + 
  coord_flip() + 
  geom_col(aes(fill = as.numeric(specie))) + 
  scale_fill_gradient2(low = "white", high = "red") + 
  theme_bw(base_size = 10) 

Data in dput format.

Dataset <-
structure(list(specie = structure(c(26L, 25L, 24L, 23L, 22L, 
21L, 20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 
8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 26L, 25L, 24L, 23L, 22L, 21L, 
20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 
7L, 6L, 5L, 4L, 3L, 2L, 1L, 26L, 25L, 24L, 23L, 22L, 21L, 20L, 
19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 
6L, 5L, 4L, 3L, 2L, 1L, 26L, 25L, 24L, 23L, 22L, 21L, 20L, 19L, 
18L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 
5L, 4L, 3L, 2L, 1L), .Label = c("ar", "br", "cr", "dr", "er", 
"fr", "Gr", "hr", "Ir", "jr", "kr", "lr", "mr", "nr", "or", "pr", 
"qr", "rr", "sr", "tr", "ur", "vr", "wr", "xr", "yr", "zr"), class = "factor"), 
    momento = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
    4L, 4L, 4L, 4L, 4L), .Label = c("M1", "M2", "M3", "M4"), class = "factor"), 
    M1_sava = c(1, 0.86, 1, 1, 1, 0.74, 0.39, 0.83, 0.83, 0, 
    0.7, 0.11, 1, 0, 0, 0, 0, 0.83, 0, 0, 0, 0, 0, 0.04, 0, 0, 
    0.33, 0.86, 0.39, 0.02, 0.07, 0.02, 0.87, 0.06, 0.63, 0.33, 
    0.91, 0.67, 0.18, 0.08, 0, 0, 0, 0, 0.08, 0, 0.04, 0, 0, 
    0, 0, 0, 0.04, 0.32, 0.02, 0, 0, 0, 0.96, 0.06, 0.18, 0.33, 
    0.63, 1, 0, 0.94, 0.17, 0, 0.41, 0.04, 0.44, 0.17, 0.02, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.89, 0, 0.03, 0.22, 0.41, 
    0.46, 0, 0.81, 0.39, 0.7, 0.7, 0, 0.87, 0.91, 0.33, 0.37, 
    0.24, 0.15, 0, 0)), class = "data.frame", row.names = c(NA, 
-104L))


来源:https://stackoverflow.com/questions/52026353/horizontal-barplot-with-color-gradient-from-top-to-bottom-of-the-graphic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!