如何根据geom_point做出geom_linerange闪避位置
How to make geom_linerange dodge position according to geom_point
我正在尝试用我在 R 中生成的理论数据创建一个点图。在 x 轴上,我有四个类别 (1,2,3,4),在 y 轴上有一些预测每个 x 类别。这就是我创建数据的方式:
x<-c(1,2,3,4,1,2,3,4)
conf.low<- c(0.65, 0.65, 0.15, 0.15, 0.65, 0.15, 0.65, 0.40)
predicted<-c(0.70, 0.70, 0.20, 0.20, 0.70, 0.20, 0.70,0.45)
conf.high<-c(0.75, 0.75, 0.25, 0.25, 0.75, 0.25, 0.75, 0.50)
group2<- c("Day","Day","Day","Day","Night","Night","Night","Night")
tot.risk<-cbind.data.frame(x, predicted, conf.low, conf.high, group2) #Combine all the variables
tot.risk$group2<-as.factor(tot.risk$group2)
tot.risk$x<-factor(tot.risk$x, levels = c(1:4), labels = c("Double low", "Low hunt/high wolf",
"High hunt/low wolf", "Double high"))
#Plot it
ggplot(tot.risk, aes(x = x, y = predicted)) +
scale_fill_manual(values=c("white", "black"))+
geom_point(pch=21, size=6, aes(fill=group2), position = position_dodge(width = 0.8))+
scale_y_continuous(limits = c(0,0.8))+
geom_linerange(aes(ymin=conf.low, ymax=conf.high), position = position_dodge(width = 0.8))+
theme_classic()+
theme(axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(size=12),
axis.title.x = element_text(size=14),
axis.title.y = element_text(size=14),
legend.text = element_text(size=14),
legend.title = element_blank())+
xlab("Type of risk")+
ylab("Probability of selection") ````
这是我在 运行 这段代码时得到的情节:
正如你所看到的,geom_linerange
并没有和那个点一起闪避。我不知道如何解决这个问题。有谁知道 position_dodge
在这种情况下不起作用?
您可以使用 position_dodge2()
来获得正确的“闪避”。
position_dodge() requires the grouping variable to be be specified in the global or geom_* layer. Unlike position_dodge(), position_dodge2() works without a grouping variable in a layer.
因此如果你想使用position_dodge
,把fill = group2
放在ggplot(aes())
中或者直接在geom_linerange
中使用position_dodge2
。
library(ggplot2)
ggplot(tot.risk, aes(x = x, y = predicted)) +
scale_fill_manual(values=c("white", "black"))+
geom_point(pch=21, size=6, aes(fill=group2), position = position_dodge(width = 0.8))+
scale_y_continuous(limits = c(0,0.8))+
geom_linerange(aes(ymin=conf.low, ymax=conf.high), position = position_dodge2(width = 0.8))+
theme_classic()+
theme(axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(size=12),
axis.title.x = element_text(size=14),
axis.title.y = element_text(size=14),
legend.text = element_text(size=14),
legend.title = element_blank())+
xlab("Type of risk")+
ylab("Probability of selection")
我正在尝试用我在 R 中生成的理论数据创建一个点图。在 x 轴上,我有四个类别 (1,2,3,4),在 y 轴上有一些预测每个 x 类别。这就是我创建数据的方式:
x<-c(1,2,3,4,1,2,3,4)
conf.low<- c(0.65, 0.65, 0.15, 0.15, 0.65, 0.15, 0.65, 0.40)
predicted<-c(0.70, 0.70, 0.20, 0.20, 0.70, 0.20, 0.70,0.45)
conf.high<-c(0.75, 0.75, 0.25, 0.25, 0.75, 0.25, 0.75, 0.50)
group2<- c("Day","Day","Day","Day","Night","Night","Night","Night")
tot.risk<-cbind.data.frame(x, predicted, conf.low, conf.high, group2) #Combine all the variables
tot.risk$group2<-as.factor(tot.risk$group2)
tot.risk$x<-factor(tot.risk$x, levels = c(1:4), labels = c("Double low", "Low hunt/high wolf",
"High hunt/low wolf", "Double high"))
#Plot it
ggplot(tot.risk, aes(x = x, y = predicted)) +
scale_fill_manual(values=c("white", "black"))+
geom_point(pch=21, size=6, aes(fill=group2), position = position_dodge(width = 0.8))+
scale_y_continuous(limits = c(0,0.8))+
geom_linerange(aes(ymin=conf.low, ymax=conf.high), position = position_dodge(width = 0.8))+
theme_classic()+
theme(axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(size=12),
axis.title.x = element_text(size=14),
axis.title.y = element_text(size=14),
legend.text = element_text(size=14),
legend.title = element_blank())+
xlab("Type of risk")+
ylab("Probability of selection") ````
这是我在 运行 这段代码时得到的情节:geom_linerange
并没有和那个点一起闪避。我不知道如何解决这个问题。有谁知道 position_dodge
在这种情况下不起作用?
您可以使用 position_dodge2()
来获得正确的“闪避”。
position_dodge() requires the grouping variable to be be specified in the global or geom_* layer. Unlike position_dodge(), position_dodge2() works without a grouping variable in a layer.
因此如果你想使用position_dodge
,把fill = group2
放在ggplot(aes())
中或者直接在geom_linerange
中使用position_dodge2
。
library(ggplot2)
ggplot(tot.risk, aes(x = x, y = predicted)) +
scale_fill_manual(values=c("white", "black"))+
geom_point(pch=21, size=6, aes(fill=group2), position = position_dodge(width = 0.8))+
scale_y_continuous(limits = c(0,0.8))+
geom_linerange(aes(ymin=conf.low, ymax=conf.high), position = position_dodge2(width = 0.8))+
theme_classic()+
theme(axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.text.x = element_text(size=12),
axis.title.x = element_text(size=14),
axis.title.y = element_text(size=14),
legend.text = element_text(size=14),
legend.title = element_blank())+
xlab("Type of risk")+
ylab("Probability of selection")