gpt4 book ai didi

r - 如何通过符号大小控制 geom_errorbar 宽度?

转载 作者:行者123 更新时间:2023-12-01 18:56:47 25 4
gpt4 key购买 nike

在下面的示例中,我有一个简单的平均值图,其中 X 轴和 Y 轴均带有标准差误差条。我想控制误差条宽度,以便两个轴始终绘制相同的尺寸。

理想情况下,我希望条形宽度/高度与符号大小相同(即在本例中 cex = 3),与最终绘图尺寸无关。有办法做到这一点吗?

# Load required packages:
library(ggplot2)
library(plyr)

# Create dataset:
DF <- data.frame(
group = rep(c("a", "b", "c", "d"),each=10),
Ydata = c(seq(1,10,1),seq(5,50,5),seq(20,11,-1),seq(0.3,3,0.3)),
Xdata = c(seq(1,10,1),seq(5,50,5),seq(20,11,-1),seq(0.3,3,0.3)))

# Summarise data:
subDF <- ddply(DF, .(group), summarise,
X = mean(Xdata), Y = mean(Ydata),
X_sd = sd(Xdata, na.rm = T), Y_sd = sd(Ydata))

# Plot data with error bars:
ggplot(subDF, aes(x = X, y = Y)) +
geom_errorbar(aes(x = X,
ymin = (Y-Y_sd),
ymax = (Y+Y_sd)),
width = 1, size = 0.5) +
geom_errorbarh(aes(x = X,
xmin = (X-X_sd),
xmax = (X+X_sd)),
height = 1, size = 0.5) +
geom_point(cex = 3)

以 1:1 比例 (500x500) 绘制时看起来不错: Plot1

但是当以 600x200 绘制时,误差条宽度/高度看起来有所不同 Plot2

最佳答案

我会使用大小变量,这样您就可以同时控制所有 3 个绘图元素

geom_size <- 3

# Plot data with error bars:
ggplot(subDF, aes(x = X, y = Y)) +
geom_errorbar(aes(x = X,
ymin = (Y-Y_sd),
ymax = (Y+Y_sd)),
width = 1, size = geom_size) +
geom_errorbarh(aes(x = X,
xmin = (X-X_sd),
xmax = (X+X_sd)),
height = 1, size = geom_size) +
geom_point(cex = geom_size)

enter image description here

关于r - 如何通过符号大小控制 geom_errorbar 宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47151127/

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