- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 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)
最佳答案
首先,在您的数据框中 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)
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))
关于r - ggplot2 使用 geom_errorbar 和 geom_point 将点添加到绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17243408/
我想在我的 geom_bar 图上绘制误差线。我知道您可以调整 aes(ymax, ymin) 以仅显示上条或下条。 但是,我有一些负平均值和正平均值,我想分别显示误差线。 这里有一些示例代码和数据可
我的数据看起来像这样: df1 <- structure( list( y = c(-0.19, 0.3,-0.05, 0.15,-0.05, 0.15), lb
在下面的示例中,我有一个简单的平均值图,其中 X 轴和 Y 轴均带有标准差误差条。我想控制误差条宽度,以便两个轴始终绘制相同的尺寸。 理想情况下,我希望条形宽度/高度与符号大小相同(即在本例中 cex
使用 ggplot 在 R 中绘制我的数据,不显示错误栏 mustache 。为什么不显示 mustache ?如何解决才能显示它们? (虽然没有必要在 geom_errorbar 中指定“data=
我是 R 新手,并且 ggplot 和以下数据集(从较大的集合中选择作为代表)存在问题,其中 geom_errorbar 条与平均点不对齐(使用 geom_point),并且在某些情况下,水平条不与垂
我似乎无法重新生成之前使用下面的代码生成的绘图(使用ggplot2)。我现在收到错误消息“没有名为 StatHline 的统计数据”。有替代品吗? data <- data.frame( Corr
我有以下示例图: test <- data.frame("Factor" = as.factor(c("O", "C", "A")), b = c(0.1
我如何将误差线放置在适当颜色条的中心? df1 <- data.frame( supp = c("OJ","OJ","OJ","VC","VC","VC"), dose = c(0.5,1,2
我正在使用 ggplot 创建一个图形,并希望使用箭头来指示我的误差线超出定义轴的位置。例如,我想最终得到一个如下所示的图形: 我希望 R 确定哪些下限超出了定义的图表范围,并添加一个漂亮的箭头(而不
我正在尝试制作带有误差线的分组条形图。但是,我无法让误差条看起来正确(即比主条更细)和正确定位(在条的中心)。 position选项和 position_dodge()似乎无法正常工作,我无法弄清楚为
我有一个使用 ggplot 的绘图,我想在其中添加点和误差线。我正在使用 geom_errorbar 和 geom_point,但我收到一个错误:“离散值提供给连续比例”,我不知道为什么。下图中的数据
我正在尝试使用 ggplot 和 geom_errorbar 创建一个分面图。但是,每个不同的方面可能具有截然不同的 x 范围,因此误差条的宽度看起来并不“好”。这是一个 MWE: library(g
我有一个数据集,其中在不同的日子对不同的群体进行测量。 我想要并排的条形表示不同组在不同日期的测量值,条形组根据测量日间隔开,误差条覆盖在它们上。 我在躲避 geom_bar 时遇到问题同意 geom
我有一些数据,其中一些估计有很大的误差线。我想通过对图表设置限制来以有意义的比例绘制。但是,我还想指出错误栏超出限制的位置。最好使用箭头,如果可能的话,不使用 geom_segment,因为它似乎不能
我正在使用 ggplot2 生成一个相对于整数值预测变量的相当简单的比例图。我正在使用 geom_errorbar 来显示每个点估计的不确定性。 例如 require(ggplot2) mydata
我是一名优秀的程序员,十分优秀!