我正在尝试在R中构建一个哑铃形图表,其中包括一个图例以及按y轴以外的值进行排序的能力。
我可以在两个单独的图中分别获得这些要求,但无法在一个图表中同时使用这两个功能。
下面的脚本显示了带有图例但没有自定义排序的绘图:
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") )
下面的脚本显示了没有图例和自定义排序的绘图:
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=reorder(trt,-l), x=l, xend=r)) +
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") )
当我尝试将这两者加在一起时,我收到一个错误"Error in tapply(X = X,INDEX = x,FUN = FUN,...):参数必须具有相同的长度“,下面是我正在使用的脚本:
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=reorder(trt,-l), x=l, xend=r)) +
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") )
有什么想法可以纠正这个问题吗?谢谢
转载请注明出处:http://www.xjzhisheng.com/article/20230320/2138105.html