gpt4 book ai didi

r - ggplot2 使用 geom_errorbar 和 geom_point 将点添加到绘图

转载 作者:行者123 更新时间:2023-12-04 04:52:27 27 4
gpt4 key购买 nike

我有一个使用 ggplot 的绘图,我想在其中添加点和误差线。我正在使用 geom_errorbar 和 geom_point,但我收到一个错误:“离散值提供给连续比例”,我不知道为什么。下图中的数据标签应保持不变。我只是想在现有图表中添加新点。新图表应如下图所示,除了 Y 轴上的每个标签有两个点/CI 条。

以下示例来自 lme4 包,它使用下面的 ggplot 生成一个带有置信区间的图(除了最后两行 borken 代码外,所有都可以复制)。我的数据不同之处仅在于它包含大约 15 个截距而不是下面的 6 个(这就是我使用 scale_shape_manual 的原因)。

最后两行代码是我尝试添加点/置信区间。我将为此提供 50 赏金。如果我不清楚,请告诉我。谢谢!

library("lme4")
data(package = "lme4")

# Dyestuff
# a balanced one-way classiï¬cation of Yield
# from samples produced from six Batches

summary(Dyestuff)

# Batch is an example of a random effect
# Fit 1-way random effects linear model
fit1 <- lmer(Yield ~ 1 + (1|Batch), Dyestuff)
summary(fit1)
coef(fit1) #intercept for each level in Batch


randoms<-ranef(fit1, postVar = TRUE)
qq <- attr(ranef(fit1, postVar = TRUE)[[1]], "postVar")

rand.interc<-randoms$Batch

#THESE ARE THE ADDITIONAL POINTS TO BE ADDED TO THE PLOT
Inter <- c(-25,-45,20,30,23,67)
SE2 <- c(20,20,20,20,20,20)

df<-data.frame(Intercepts=randoms$Batch[,1],
sd.interc=2*sqrt(qq[,,1:length(qq)]), Intercepts2=Inter, sd.iterc2=SE2,
lev.names=rownames(rand.interc))

df$lev.names<-factor(df$lev.names,levels=df$lev.names[order(df$Intercepts)])

library(ggplot2)
p <- ggplot(df,aes(lev.names,Intercepts,shape=lev.names))

#Added horizontal line at y=0
#Includes first set of points/confidence intervals. This works without error
p <- p + geom_hline(yintercept=0) +geom_errorbar(aes(ymin=Intercepts-sd.interc, ymax=Intercepts+sd.interc), width=0,color="black") + geom_point(aes(size=2))

#Removed legends and with scale_shape_manual point shapes set to 1 and 16
p <- p + guides(size=FALSE,shape=FALSE) + scale_shape_manual(values=c(16,16,16,16,16,16))

#Changed appearance of plot (black and white theme) and x and y axis labels
p <- p + theme_bw() + xlab("Levels") + ylab("")

#Final adjustments of plot
p <- p + theme(axis.text.x=element_text(size=rel(1.2)),
axis.title.x=element_text(size=rel(1.3)),
axis.text.y=element_text(size=rel(1.2)),
panel.grid.minor=element_blank(),
panel.grid.major.x=element_blank())

#To put levels on y axis you just need to use coord_flip()
p <- p+ coord_flip()
print(p)

#####
# code for adding more plots, NOT working yet
p <- p +geom_errorbar(aes(ymin=Intercepts2-sd.interc2, ymax=Intercepts2+sd.interc2),
width=0,color="gray40", lty=1, size=1)

p <- p + geom_point(aes(Intercepts2, lev.names),size=0,pch=7)

最佳答案

首先,在您的数据框中 dfgeom_errorbar()有两个不同的变量sd.iterc2sd.interc2 .在 df 中也进行了更改至sd.interc2 .

对于 geom_point() 的最后一行您收到错误是因为您的 xy值的顺序错误。当您使用 coord_flip()然后 xy值应按照与 coord_flip() 之前的原始图中相同的顺序放置,即lev.namesx , 和 Intercepts2y .也改变了size=到 5 以获得更好的说明。

+ geom_point(aes(lev.names,Intercepts2),size=5,pch=7)

enter image description here

更新 - 添加图例

要为截距类型的点添加图例,一种选择是将数据 reshape 为长格式并添加具有截距类型的新列。您现有数据的其他选项是,首先,删除 shape=lev.names来自 ggplot()称呼。然后在两个 geom_point()来电添加 shape="somename"里面 aes() .然后用 scale_shape_manual()设置您需要的形状值。
ggplot(df,aes(lev.names,Intercepts))+
geom_hline(yintercept=0) +
geom_errorbar(aes(ymin=Intercepts-sd.interc, ymax=Intercepts+sd.interc), width=0,color="black")+
geom_point(aes(shape="Intercepts"),size=5)+
theme_bw() + xlab("Levels") + ylab("")+
theme(axis.text.x=element_text(size=rel(1.2)),
axis.title.x=element_text(size=rel(1.3)),
axis.text.y=element_text(size=rel(1.2)),
panel.grid.minor=element_blank(),
panel.grid.major.x=element_blank())+
coord_flip()+
geom_errorbar(aes(ymin=Intercepts2-sd.interc2, ymax=Intercepts2+sd.interc2),
width=0,color="gray40", lty=1, size=1) +
geom_point(aes(lev.names,Intercepts2,shape="Intercepts2"),size=5)+
scale_shape_manual(values=c(16,7))

enter image description here

关于r - ggplot2 使用 geom_errorbar 和 geom_point 将点添加到绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17243408/

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