2017-06-20 67 views
2

如何添加一个传统的传说来使用ggalt::geom_dumbbellR创建的哑铃情节?在`R`的`ggalt :: geom_dumbbell`中添加一个传统的传说来哑铃情节

This question has an answer with in-chart legend。如何绘制美学图像以获得侧面/底部的点的单独图例?

library(ggalt) 

df <- data.frame(trt=LETTERS[1:5], l=c(20, 40, 10, 30, 50), r=c(70, 50, 30, 60, 80)) 

ggplot(df, aes(y=trt, x=l, xend=r)) + 
    geom_dumbbell(size=3, color="#e3e2e1", 
       colour_x = "red", colour_xend = "blue", 
       dot_guide=TRUE, dot_guide_size=0.25) + 
    theme_bw() 
得到一个图例

enter image description here

+3

它更容易帮助你,如果你提供一个[重复的例子(https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible样本)与样本输入数据。 – MrFlick

+0

@MrFlick这个例子已经结束了。 – Crops

+0

那么从这个例子中,你想在图例中做些什么? – MrFlick

回答

2

的一种方式是添加基于在长格式的数据集,映射到color分组变量a点层。

首先,通过gathertidyr制作长格式数据集。

df2 = tidyr::gather(df, group, value, -trt) 

然后,让情节,加入新的点层与长期数据集,并使用scale_color_manual来设置颜色。我把geom_dumbbell特定的美学移到了这一层。

ggplot(df, aes(y = trt)) + 
    geom_point(data = df2, aes(x = value, color = group), size = 3) + 
    geom_dumbbell(aes(x = l, xend = r), size=3, color="#e3e2e1", 
        colour_x = "red", colour_xend = "blue", 
        dot_guide=TRUE, dot_guide_size=0.25) + 
    theme_bw() + 
    scale_color_manual(name = "", values = c("red", "blue")) 

enter image description here