【问题标题】:ggplot2 using geom_errorbar and geom_point to add points to a plotggplot2 使用 geom_errorbar 和 geom_point 将点添加到绘图
【发布时间】:2013-06-21 20:03:33
【问题描述】:

我有一个使用 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)

【问题讨论】:

  • 我认为你错了,事实上,geom_step 根本不是你想要的。你能用文字描述实际的预期结果吗?即描述应该绘制什么?
  • 对不起,如果我不清楚。我在帖子的开头更新了我正在寻找的内容的描述。而且我在使用 geom_step 时很可能是错误的,这可能是我的问题的一部分。如果我仍然不清楚,请告诉我。谢谢。
  • 好吧,要直接添加更多点,就像其他点一样,你应该只使用geom_point。但它们会重叠。请参阅here
  • 啊,谢谢!我确实希望它们重叠,所以这很好。让我通过那个链接,看看它是否有效。干杯
  • 好的,如果您不在乎它们是否重叠,只需对geom_errorbargeom_point 执行与第一组相同的过程,就可以了。跨度>

标签: r ggplot2


【解决方案1】:

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

对于geom_point() 的最后一行,您会收到错误消息,因为您的xy 值的顺序错误。当您使用 coord_flip() 时,xy 值应按照与原始图中相同的顺序放置在 coord_flip() 之前,即 lev.namesxIntercepts2 为 @987654339 @。为了更好的说明,还将size= 更改为 5。

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

更新 - 添加图例

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

【讨论】:

  • 谢谢!我会给你赏金,但只有一个跟进......我如何添加一个图例来标记这两种类型的点(一种用于 box-X 点,另一种用于黑点)?
  • @CaptainMurphy 更新了我的答案。
猜你喜欢
  • 2015-05-30
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多