4

这个 SO 答案中,用户 @Crops 展示了如何将图例添加到ggalt::geom_dumbbell绘图中。非常好。

library(ggalt)

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

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") )

在此处输入图像描述

我想trt按降序排序r。我尝试用 替换y = trty = reorder(trt, r)但我收到一个错误,即r找不到对象。

4

2 回答 2

3

这是我们重新排序trtindfdf2之前的因子水平的一种方式。

# reorder factor levels
df$trt <- reorder(df$trt, df$r)
df2$trt <- factor(df2$trt, levels = levels(df$trt))

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") )

在此处输入图像描述

于 2018-11-11T12:40:34.450 回答
0

使用哑铃包

##Reformat data
df3<-df  %>% arrange(r)

df2<-df%>% mutate("key"="trt")
df2$trt<-factor(df2$trt,df3$trt)

##plot
dumbbell::dumbbell(df2, id="trt", column1="l", column2="r",key="key", delt =1, textsize=3, lab1 = "l", lab2="r", pt_val = 1, pointsize = 3,pt_alpha = 0.6, arrow=1, leg = "Add legend title", pval=2) + xlim(8,85) + facet_wrap(key ~.)

添加了一些花里胡哨,您可以通过选项切换它们来删除它们。我没有足够的积分来嵌入这里是链接。希望有人觉得它有用。 哑铃 R 包

于 2021-02-26T04:47:45.393 回答