R ggplot2如何使用不同比例的轴使散点图上的水平和垂直误差条大小相同
我正试图绘制具有x轴和y轴误差条的汇总散点图。我希望错误条散列的大小相同。然而,因为我有不同规模的轴(例如海胆丰度与珊瑚生长率),所以我的误差条散列大小不同 我使用了R ggplot2如何使用不同比例的轴使散点图上的水平和垂直误差条大小相同,r,ggplot2,scatter-plot,errorbar,R,Ggplot2,Scatter Plot,Errorbar,我正试图绘制具有x轴和y轴误差条的汇总散点图。我希望错误条散列的大小相同。然而,因为我有不同规模的轴(例如海胆丰度与珊瑚生长率),所以我的误差条散列大小不同 我使用了geom\u errorbar()和geom\u errorbarh()的“宽度”和“高度”参数手动更改宽度和高度。但是,因为我有很多图形,我想看看是否有一种方法可以将错误条散列编码为相同的大小,这样我就可以在具有不同轴的其他图形上使用此代码 数据库 urchin_vs_growth_summary_data一种方法是创建绘图,提取
geom\u errorbar()
和geom\u errorbarh()
的“宽度”和“高度”参数手动更改宽度和高度。但是,因为我有很多图形,我想看看是否有一种方法可以将错误条散列编码为相同的大小,这样我就可以在具有不同轴的其他图形上使用此代码
数据库
urchin_vs_growth_summary_data一种方法是创建绘图,提取x和y范围(如上所述),并根据这些范围缩放错误条散列。这将产生与绘图的整体纵横比成比例的散列;只有当绘图本身完全为正方形时,水平散列和垂直散列才会完全相等。但是,如果您只是在寻找一种使散列看起来大致相似的方法,那么这可能就足够了
创建绘图(无错误条):
添加错误条:
urchin_vs_growth_plot <- ggplot(data = urchin_vs_growth_summary_data,
aes(x = urchin_mean, y = growth_mean,
fill = interaction(Site_long, Shelter),
shape = interaction(Site_long, Shelter))) +
geom_point(size = 5) +
ggtitle("Urchin Abundance vs. Coral Growth") +
scale_x_continuous(breaks = seq(0,24,3)) +
scale_y_continuous(breaks = seq(0, 3, 0.5)) +
scale_shape_manual(name = 'Site x Shelter', values = c(21, 24, 21, 24),
labels = c("Hanauma Bay - Low", "Waikiki - Low",
"Hanauma Bay - High", "Waikiki - High")) +
scale_fill_manual(name = "Site x Shelter", values = c(NA, NA, 1, 1),
labels = c("Hanauma Bay - Low", "Waikiki - Low",
"Hanauma Bay - High", "Waikiki - High")) +
geom_smooth(aes(group = 1), method ="lm", show.legend = FALSE) +
guides(size = FALSE, linetype = FALSE,
shape = guide_legend(override.aes = list(size = 4.5)),
color = guide_legend(override.aes = list(fill = NA))) +
theme(text = element_text(size = 15)) +
labs(x = "Mean urchin abundance ± SEM",
y = expression(paste("Mean coral growth (cm"^"2","/quarter) ± 95% SEM"))) +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
axis.title = element_text(size = rel(1.5)),
axis.title.y = element_text(size = rel(1)),
axis.text = element_text(size = rel(1.5)),
axis.text.y = element_text(angle = 90),
legend.text = element_text(size = rel(1)),
legend.title = element_text(size = rel(1), face = "bold"),
legend.position = "none",
plot.title = element_text(size = 20, hjust = 0.5, vjust = -1.5))
urchin_vs_growth_plot = urchin_vs_growth_plot +
geom_errorbar(aes(ymin = growth_lower, ymax = growth_upper), width = hash.width.x) +
geom_errorbarh(aes(xmin = urchin_lower, xmax = urchin_upper), height = hash.width.y)
结果是:
要对许多绘图执行此操作,可以创建一个小函数,用于获取第一个绘图(没有错误条),获取轴范围,并适当添加错误条;然后用该函数包装所有绘图。谢谢,好的。我会试试这个!看起来应该有用!
urchin_vs_growth_plot <- ggplot(data = urchin_vs_growth_summary_data,
aes(x = urchin_mean, y = growth_mean,
fill = interaction(Site_long, Shelter),
shape = interaction(Site_long, Shelter))) +
geom_point(size = 5) +
ggtitle("Urchin Abundance vs. Coral Growth") +
scale_x_continuous(breaks = seq(0,24,3)) +
scale_y_continuous(breaks = seq(0, 3, 0.5)) +
scale_shape_manual(name = 'Site x Shelter', values = c(21, 24, 21, 24),
labels = c("Hanauma Bay - Low", "Waikiki - Low",
"Hanauma Bay - High", "Waikiki - High")) +
scale_fill_manual(name = "Site x Shelter", values = c(NA, NA, 1, 1),
labels = c("Hanauma Bay - Low", "Waikiki - Low",
"Hanauma Bay - High", "Waikiki - High")) +
geom_smooth(aes(group = 1), method ="lm", show.legend = FALSE) +
guides(size = FALSE, linetype = FALSE,
shape = guide_legend(override.aes = list(size = 4.5)),
color = guide_legend(override.aes = list(fill = NA))) +
theme(text = element_text(size = 15)) +
labs(x = "Mean urchin abundance ± SEM",
y = expression(paste("Mean coral growth (cm"^"2","/quarter) ± 95% SEM"))) +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
axis.title = element_text(size = rel(1.5)),
axis.title.y = element_text(size = rel(1)),
axis.text = element_text(size = rel(1.5)),
axis.text.y = element_text(angle = 90),
legend.text = element_text(size = rel(1)),
legend.title = element_text(size = rel(1), face = "bold"),
legend.position = "none",
plot.title = element_text(size = 20, hjust = 0.5, vjust = -1.5))
hash.width.x = diff(layer_scales(urchin_vs_growth_plot)$x$range$range) / 20
hash.width.y = diff(layer_scales(urchin_vs_growth_plot)$y$range$range) / 20
urchin_vs_growth_plot = urchin_vs_growth_plot +
geom_errorbar(aes(ymin = growth_lower, ymax = growth_upper), width = hash.width.x) +
geom_errorbarh(aes(xmin = urchin_lower, xmax = urchin_upper), height = hash.width.y)