首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用geom_errorbar和geom_point向地块添加点的ggplot2

使用geom_errorbar和geom_point向地块添加点的ggplot2
EN

Stack Overflow用户
提问于 2013-06-22 04:03:34
回答 1查看 6.1K关注 0票数 2

我有一个使用ggplot的图,我想给它添加点和误差条。我正在使用geom_errorbar和geom_point,但我得到了一个错误:“离散值提供给连续的规模”,我不确定为什么。下图中的数据标签应保持不变。我只是想在现有的图中添加新的点。新图形应与下图类似,只是Y轴上的每个标签都有两个点/CI条。

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

最后两行代码是我添加点/置信度区间的尝试。我要出50美元的赏金。如果我说得不清楚,请告诉我。谢谢!

代码语言:javascript
运行
复制
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)
EN

Stack Overflow用户

回答已采纳

发布于 2013-06-24 13:06:16

首先,在数据框dfgeom_errorbar()中有两个不同的变量sd.iterc2sd.interc2。在df中也更改为sd.interc2

对于geom_point()的最后一行,您会得到错误,因为您的xy值的顺序错误。由于您使用的是coord_flip(),因此xy值的顺序应与原始绘图中coord_flip()之前的顺序相同,即lev.names作为xIntercepts2作为y。为了更好地说明,还将size=更改为5。

代码语言:javascript
运行
复制
+ geom_point(aes(lev.names,Intercepts2),size=5,pch=7)

更新-添加图例

要为截距类型的点添加图例,一种选择是将数据重塑为长格式,并添加具有截距类型的新列。对现有数据的其他选择是,首先,从ggplot()调用中删除shape=lev.names。然后在两个geom_point()调用中,在aes()中添加shape="somename"。然后使用scale_shape_manual()设置所需的形状值。

代码语言:javascript
运行
复制
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))

票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17243408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档