我有一个使用ggplot的图,我想给它添加点和误差条。我正在使用geom_errorbar和geom_point,但我得到了一个错误:“离散值提供给连续的规模”,我不确定为什么。下图中的数据标签应保持不变。我只是想在现有的图中添加新的点。新图形应与下图类似,只是Y轴上的每个标签都有两个点/CI条。
下面的示例来自lme4包,它使用下面的ggplot生成了一个带有置信区间的图(除了博肯代码的最后两行之外,所有的都可以复制)。我的数据唯一的不同之处在于,它包括大约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)发布于 2013-06-24 13:06:16
首先,在数据框df和geom_errorbar()中有两个不同的变量sd.iterc2和sd.interc2。在df中也更改为sd.interc2。
对于geom_point()的最后一行,您会得到错误,因为您的x和y值的顺序错误。由于您使用的是coord_flip(),因此x和y值的顺序应与原始绘图中coord_flip()之前的顺序相同,即lev.names作为x,Intercepts2作为y。为了更好地说明,还将size=更改为5。
+ geom_point(aes(lev.names,Intercepts2),size=5,pch=7)

更新-添加图例
要为截距类型的点添加图例,一种选择是将数据重塑为长格式,并添加具有截距类型的新列。对现有数据的其他选择是,首先,从ggplot()调用中删除shape=lev.names。然后在两个geom_point()调用中,在aes()中添加shape="somename"。然后使用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))

https://stackoverflow.com/questions/17243408
复制相似问题