我在下面创建了一个哑铃图,我想根据显示x
和的点的颜色添加一个图例xend
。哑铃可以吗?
library(ggalt)
health <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/health.csv")
health$Area <- factor(health$Area, levels=as.character(health$Area))
ggplot(health, aes(x=pct_2013, xend=pct_2014, y=Area)) +
#create a thick line between x and xend instead of using defaut
#provided by geom_dubbell
geom_segment(aes(x=pct_2013,
xend=pct_2014,
y=Area,
yend=Area),
color="#b2b2b2", size=1.5)+
geom_dumbbell(color="light blue",
size_x=3.5,
size_xend = 3.5,
#Note: there is no US:'color' for UK:'colour'
# in geom_dumbbel unlike standard geoms in ggplot()
colour_x="#edae52",
colour_xend = "#9fb059")+
labs(x=NULL, y=NULL,
title="Dumbbell Chart",
subtitle="Pct Change: 2013 vs 2014")+
geom_text(color="black", size=2, hjust=-0.5,
aes(x=pct_2013, label=pct_2013))+
geom_text(aes(x=pct_2014, label=pct_2014),
color="black", size=2, hjust=1.5)