简体   繁体   中英

ggplot2 horizontal barplot with gradient color fill and Discrete value supplied to continuous scale

I am trying to create horizontal bar plot and would like to fill bars using a color gradient, white for lowest value and darkest for highest value.

First of all I created figure 1 through the follow script. The columns inform the frequency of each species):

Dataset <- read.csv(file = "dados.csv", header = TRUE, sep = ";")
attach(Dataset)
library(ggplot2)

ggplot(Dataset, aes(specie,M1_sava, fill = momento)) + 
  facet_wrap(~ momento, nrow=1) + # place the factors in separate facets
  geom_bar(stat="identity") +     # make the bars
  coord_flip() +                  # flip the axes so the test names can be horizontal  
  theme_bw(base_size=10)          # use a black-and-white theme with set font size

However I would like to leave the bars with gradient coloring, as in figure 2 and for this I wrote the next script, but the error message appeared:

Error: Discrete value supplied to continuous scale

ggplot(Dataset, aes(specie,M1_sava, fill = momento)) + 
  facet_wrap(~ momento, nrow=1) + # place the factors in separate facets
  geom_bar(stat="identity") +     # make the bars
  coord_flip() +                  # flip the axes so the test names can be horizontal  
  geom_col(aes(fill = M1_sava)) + 
  scale_fill_gradient2(low = "white",high = "green") +
  theme_bw(base_size=10)          # use a black-and-white theme with set font size

在此处输入图片说明

Data:

specie  momento M1_sava
S1  M1  1,00
S2  M1  0,86
S3  M1  1,00
S4  M1  1,00
S5  M1  1,00
S6  M1  0,74
S7  M1  0,39
S8  M1  0,83
S9  M1  0,83
S10 M1  0,00
S11 M1  0,70
S12 M1  0,11
S13 M1  1,00
S14 M1  0,00
S15 M1  0,00
S16 M1  0,00
S17 M1  0,00
S18 M1  0,83
S19 M1  0,00
S20 M1  0,00
S21 M1  0,00
S22 M1  0,00
S23 M1  0,00
S24 M1  0,04
S25 M1  0,00
S26 M1  0,00
S1  M2  0,33
S2  M2  0,86
S3  M2  0,39
S4  M2  0,02
S5  M2  0,07
S6  M2  0,02
S7  M2  0,87
S8  M2  0,06
S9  M2  0,63
S10 M2  0,33
S11 M2  0,91
S12 M2  0,67
S13 M2  0,18
S14 M2  0,08
S15 M2  0,00
S16 M2  0,00
S17 M2  0,00
S18 M2  0,00
S19 M2  0,08
S20 M2  0,00
S21 M2  0,04
S22 M2  0,00
S23 M2  0,00
S24 M2  0,00
S25 M2  0,00
S26 M2  0,00
S1  M3  0,04
S2  M3  0,32
S3  M3  0,02
S4  M3  0,00
S5  M3  0,00
S6  M3  0,00
S7  M3  0,96
S8  M3  0,06
S9  M3  0,18
S10 M3  0,33
S11 M3  0,63
S12 M3  1,00
S13 M3  0,00
S14 M3  0,94
S15 M3  0,17
S16 M3  0,00
S17 M3  0,41
S18 M3  0,04
S19 M3  0,44
S20 M3  0,17
S21 M3  0,02
S22 M3  0,00
S23 M3  0,00
S24 M3  0,00
S25 M3  0,00
S26 M3  0,00
S1  M4  0,00
S2  M4  0,00
S3  M4  0,00
S4  M4  0,00
S5  M4  0,00
S6  M4  0,00
S7  M4  0,89
S8  M4  0,00
S9  M4  0,03
S10 M4  0,22
S11 M4  0,41
S12 M4  0,46
S13 M4  0,00
S14 M4  0,81
S15 M4  0,39
S16 M4  0,70
S17 M4  0,70
S18 M4  0,00
S19 M4  0,87
S20 M4  0,91
S21 M4  0,33
S22 M4  0,37
S23 M4  0,24
S24 M4  0,15
S25 M4  0,00
S26 M4  0,00

As @Z.Lin wrote - you should remove geom_bar(stat="identity") + but keep fill = momento :

Dataset <- read.table(text = "specie  momento M1_sava
                  S1  M1  1.00
                  S2  M1  0.86
                  S3  M1  1.00
                  S4  M1  1.00
                  S5  M1  1.00
                  S6  M1  0.74
                  S7  M1  0.39
                  S8  M1  0.83
                  S9  M1  0.83
                  S10 M1  0.00
                  S11 M1  0.70
                  S12 M1  0.11
                  S13 M1  1.00
                  S14 M1  0.00
                  S15 M1  0.00
                  S16 M1  0.00
                  S17 M1  0.00
                  S18 M1  0.83
                  S19 M1  0.00
                  S20 M1  0.00
                  S21 M1  0.00
                  S22 M1  0.00
                  S23 M1  0.00
                  S24 M1  0.04
                  S25 M1  0.00
                  S26 M1  0.00
                  S1  M2  0.33
                  S2  M2  0.86
                  S3  M2  0.39
                  S4  M2  0.02
                  S5  M2  0.07
                  S6  M2  0.02
                  S7  M2  0.87
                  S8  M2  0.06
                  S9  M2  0.63
                  S10 M2  0.33
                  S11 M2  0.91
                  S12 M2  0.67
                  S13 M2  0.18
                  S14 M2  0.08
                  S15 M2  0.00
                  S16 M2  0.00
                  S17 M2  0.00
                  S18 M2  0.00
                  S19 M2  0.08
                  S20 M2  0.00
                  S21 M2  0.04
                  S22 M2  0.00
                  S23 M2  0.00
                  S24 M2  0.00
                  S25 M2  0.00
                  S26 M2  0.00
                  S1  M3  0.04
                  S2  M3  0.32
                  S3  M3  0.02
                  S4  M3  0.00
                  S5  M3  0.00
                  S6  M3  0.00
                  S7  M3  0.96
                  S8  M3  0.06
                  S9  M3  0.18
                  S10 M3  0.33
                  S11 M3  0.63
                  S12 M3  1.00
                  S13 M3  0.00
                  S14 M3  0.94
                  S15 M3  0.17
                  S16 M3  0.00
                  S17 M3  0.41
                  S18 M3  0.04
                  S19 M3  0.44
                  S20 M3  0.17
                  S21 M3  0.02
                  S22 M3  0.00
                  S23 M3  0.00
                  S24 M3  0.00
                  S25 M3  0.00
                  S26 M3  0.00
                  S1  M4  0.00
                  S2  M4  0.00
                  S3  M4  0.00
                  S4  M4  0.00
                  S5  M4  0.00
                  S6  M4  0.00
                  S7  M4  0.89
                  S8  M4  0.00
                  S9  M4  0.03
                  S10 M4  0.22
                  S11 M4  0.41
                  S12 M4  0.46
                  S13 M4  0.00
                  S14 M4  0.81
                  S15 M4  0.39
                  S16 M4  0.70
                  S17 M4  0.70
                  S18 M4  0.00
                  S19 M4  0.87
                  S20 M4  0.91
                  S21 M4  0.33
                  S22 M4  0.37
                  S23 M4  0.24
                  S24 M4  0.15
                  S25 M4  0.00
                  S26 M4  0.00", header = T)

library(ggplot2)

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

Is this desired output?

在此处输入图片说明

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