gpt4 book ai didi

r - 向 ggalt::geom_dumbbell 图添加图例并对 y 轴进行排序

转载 作者:行者123 更新时间:2023-12-03 23:53:42 24 4
gpt4 key购买 nike

this SO answer , 用户@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") )

enter image description here

我要排序 trt下降在 r .我尝试更换 y = trty = reorder(trt, r) ,但我收到一个错误,对象 r没有找到。

最佳答案

这是我们重新排序 trt 的因子水平的方法在 dfdf2在我们绘图之前。

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

enter image description here

关于r - 向 ggalt::geom_dumbbell 图添加图例并对 y 轴进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53248812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com