尝试将数据重新整形为长。这里是使用ggplot2 和tidyverse 函数的解决方案代码:
library(ggplot2)
library(dplyr)
library(tidyr)
#Data
df <- data.frame(Cause=c('Cellulitis','Convulsions and epilepsy',
'Dental conditions','Ear, nose and throat infections'),
under=c(3.7,2,1.2,1.2),
over=c(5.4,3.9,1.8,2.5),stringsAsFactors = F)
#Plot
df %>% pivot_longer(-Cause) %>%
ggplot(aes(x=Cause,y=value,fill=name))+
geom_col(color='black')+
theme_bw()+
theme(legend.position = 'top')+
scale_fill_manual('',values=c('tomato','cornflowerblue'))
输出:
对于进一步的定制,你可以使用这个:
#Plot 2
df %>% pivot_longer(-Cause) %>%
mutate(name=paste(name,'65')) %>%
ggplot(aes(x=Cause,y=value,fill=name))+
geom_col(color='black')+
theme_bw()+
theme(legend.position = 'top')+
scale_fill_manual('',values=c('tomato','cornflowerblue'))
输出: