gpt4 book ai didi

r - ggplot2:geom_errorbarh 末尾两条垂直线的大小

转载 作者:行者123 更新时间:2023-12-04 10:25:34 26 4
gpt4 key购买 nike

我目前有如下图所示:

enter image description here

但是,(水平)线末尾的两条垂直线对我来说太长了。
我试图更改“高度”和“宽度”的数字,但更改数字不起作用。

任何人都可以使垂直线更短吗?

提前谢谢了!

*** 这是可以重现情节的代码。

my_data <- data.frame(mean = c(0.04, 0.015, -0.04),
stdev = c(0.019, 0.019, 0.02),
Type = factor(c("A",
"B",
"C")))
# points at which to evaluate the Gaussian densities
x <- seq(-0.1, 0.1, by = 0.001)

# build list of Gaussian density vectors based on means and standard deviations
pdfs <- mapply(dnorm, mean = my_data$mean, sd = my_data$stdev, MoreArgs = list(x = x),
SIMPLIFY = FALSE)

# add group names
names(pdfs) <- my_data$Type

# convert list to dataframe
pdfs <- do.call(cbind.data.frame, pdfs)

# Summary stat
x.com <- data.frame(pdfs, x)
sum_stat <- matrix(NA, ncol = 4, nrow = 3)
for (i in 1:3){
sum_stat[i,1] <- qnorm(0.025 , mean=my_data[i,1], sd=my_data[i,2])
sum_stat[i,2] <- x.com[,4][x.com[,i]==max(x.com[,i])]
sum_stat[i,3] <- qnorm(0.975, mean=my_data[i,1], sd=my_data[i,2])
}
sum_stat <- data.frame(sum_stat)
sum_stat[,4] <- colnames(x.com)[1:3]
sum_stat[,4] <- as.factor(sum_stat[,4])
colnames(sum_stat) <- c("left", "max", "right", "Type")
colnames(sum_stat)[4] <- "Type"

# convert dataframe to tall format
# library(tidyr)
pdfs$x <- x
tall_df <- gather(pdfs, Type, density, -x)
tall_df$Type

tt <- transform(tall_df,
Type=factor(Type,levels=c("A",
"B",
"C")))

ggplot(tt) +
# geom_line(aes(x = x, y = density, color = Type)) +
geom_point(data = sum_stat,
aes(x = max, y = 0, alpha=0.4),
show.legend = FALSE, color = "black") +

geom_errorbarh(data = sum_stat,
aes(xmin = left, xmax = right, y = 0, alpha=0.4),
height = 0.01, show.legend = FALSE, color = "black") +

facet_wrap(~ Type, ncol = 1) +
theme(strip.background = element_blank(),
strip.text.x = element_blank())

更新:

我正在更新帖子,因为我无法向您展示我的控制台所说的内容。我清除了全局环境,然后运行我在这里附加的代码。我运行它没有任何问题。
就我而言,tt 的头部和尾部如下所示:
> tt <- transform(tall_df,
+ Type=factor(Type,levels=c("A",
+ "B",
+ "C")))
> head(tt)
x Type density
1 -0.100 A 3.407570e-11
2 -0.099 A 5.014963e-11
3 -0.098 A 7.360166e-11
4 -0.097 A 1.077220e-10
5 -0.096 A 1.572238e-10
6 -0.095 A 2.288385e-10
> tail(tt)
x Type density
598 0.095 C 2.547469e-09
599 0.096 C 1.815481e-09
600 0.097 C 1.290591e-09
601 0.098 C 9.151661e-10
602 0.099 C 6.473296e-10
603 0.100 C 4.567360e-10

我想提的一件事是:我想保持 y 的范围,但只是让 errorbar 末尾的垂直线更短。

最佳答案

修复 y 轴的比例,例如添加 + scale_y_continuous(limits = c(-.2, .2)) .然后你可以用 height 调整垂直线的长度.否则 ggplot2始终将 y 轴的比例重新调整为所选 height所以它看起来好像 height完全没有影响。

ggplot(tt) + 
# geom_line(aes(x = x, y = density, color = Type)) +
geom_point(data = sum_stat,
aes(x = max, y = 0, alpha=0.4),
show.legend = FALSE, color = "black") +
geom_errorbarh(data = sum_stat,
aes(xmin = left, xmax = right, y = 0, alpha=0.4),
height = 0.01, show.legend = FALSE, color = "black") +
scale_y_continuous(limits = c(-.2, .2)) +
facet_wrap(~ Type, ncol = 1) +
theme(strip.background = element_blank(),
strip.text.x = element_blank())



创建于 2020-03-11 由 reprex package (v0.3.0)

关于r - ggplot2:geom_errorbarh 末尾两条垂直线的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60643875/

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