R ggplot2阴影和一个绘图中的多个数据点
我测量了两种R ggplot2阴影和一个绘图中的多个数据点,r,ggplot2,tidyverse,R,Ggplot2,Tidyverse,我测量了两种化合物丙酮和乙醛的排放量,共测量了116天九次。我画了一张这样的图: df%>% ggplot(aes(x=days, y=emission))+ geom_point(color="darkblue", size=3.5) + geom_errorbar(aes(ymin=emission, ymax=emission+se), width=.2, position=position_dodge(0.05)) +
化合物丙酮和乙醛的排放量,共测量了116天九次。我画了一张这样的图:
df%>%
ggplot(aes(x=days, y=emission))+
geom_point(color="darkblue", size=3.5) +
geom_errorbar(aes(ymin=emission, ymax=emission+se), width=.2,
position=position_dodge(0.05)) +
labs(color = "", size= "") +
labs(x = "Incubation time (days)", y = "Production (umol g-1 dw soil h-1)") +
theme_bw() +
facet_wrap(vars(compound), scales = "free_y")
测量分为三组:a
、b
和c
我试图在图表中绘制b组和c组的平均值(和标准误差)。这个符号必须是不同的
有什么想法吗
数据如下所示
df <- structure(list(compound = c("Acetaldehyde", "Acetaldehyde", "Acetaldehyde",
"Acetaldehyde", "Acetaldehyde", "Acetaldehyde", "Acetaldehyde",
"Acetaldehyde", "Acetaldehyde", "Acetone", "Acetone", "Acetone",
"Acetone", "Acetone", "Acetone", "Acetone", "Acetone", "Acetone"
), days = c(0, 4, 10, 17, 24, 66, 81, 94, 116, 0, 4, 10, 17,
24, 66, 81, 94, 116), emission = c(26.13, 59.09, 45, 11.41, 6.71,
0.07, 0, 0.53, 0.56, 28.47, 46.65, 44.03, 56.68, 29.08, 2.73,
7.72, 0.38, 0.21), se = c(5.88, 11.04, 11.4, 4.31, 3.54, 0.03,
0, 0.25, 0.31, 7.44, 8.98, 9.92, 22.79, 13.83, 1.2, 6.38, 0.09,
0.07), group = c("a", "b", "b", "b", "b", "b", "c", "c", "c",
"a", "b", "b", "b", "b", "b", "c", "c", "c")), row.names = c(NA,
-18L), class = "data.frame")
---------------------------------更新---------------------------------
我可以看出我在解释我要做的事情时做得很糟糕
数据中有一列名为组
。在此列中,测量值分为a
、b
和c
。我想在我显示的图的顶部绘制这些组的平均值(其中所有测量值都是。在a
中只有一个测量值,因此这对这个组没有意义。但是对于b
组和c
组,我希望绘制平均值(和标准误差)当然,这些符号需要与我已经绘制的不同
此外,我正在尝试更改每个组后面的背景颜色,使其具有不同的颜色,类似于我在谷歌上找到的这个图:
.我编辑了我的答案:
library(ggplot2)
library(magrittr)
library(plotrix)
library(knitr)
library(dplyr)
#creating group b acetaldehyde mean and standart error
b_mean_ald <- mean(df$emission[df$group == "b" & df$compound == "Acetaldehyde"])
b_se_ald <- std.error(df$emission[df$group == "b" & df$compound == "Acetaldehyde"])
#creating group b acetone mean and standart error
b_mean_acet <- mean(df$emission[df$group == "b" & df$compound == "Acetone"])
b_se_acet <- std.error(df$emission[df$group == "b" & df$compound == "Acetone"])
#creating group c acetaldehyde mean and standart error
c_mean_ald <- mean(df$emission[df$group == "c" & df$compound == "Acetaldehyde"])
c_se_ald <- std.error(df$emission[df$group == "c" & df$compound == "Acetaldehyde"])
#creating group c acetone mean and standart error
c_mean_acet <- mean(df$emission[df$group == "c" & df$compound == "Acetone"])
c_se_acet <- std.error(df$emission[df$group == "c" & df$compound == "Acetone"])
# making a dataframe which contains mean and se of each compounds per groups
mean_df <- data.frame(
compound = c("Acetaldehyde", "Acetaldehyde", "Acetone", "Acetone"),
days = c(33, 95, 33, 95),
emission = c(b_mean_ald, c_mean_ald, b_mean_acet, c_mean_acet),
se = c(b_se_ald, c_se_ald, b_se_acet, c_se_acet),
group = c("b", "c", "b", "c")
)
最终绘图:
如果您想在legend中包含组,您应该做一些技巧:
#final plot with groups and measurments legend
df_fin%>%
ggplot(aes(x=days, y=emission))+
labs(color = "", size= "") +
labs(x = "Incubation time (days)", y = "Production (umol g-1 dw soil h-1)") +
facet_wrap(vars(compound), scales = "free_y") +
geom_rect(xmin = -2, xmax = 2.2, ymin = 85, ymax = 100,
fill="green",alpha = 0.05)+
geom_rect(xmin = 2.2, xmax = 69, ymin = 85, ymax = 100,
fill = "yellow", alpha = 0.05) +
geom_rect(xmin = 69, xmax = 120, ymin = 85, ymax = 100,
fill="purple",alpha = 0.05) +
geom_rect(xmin = -2, xmax = 2.2, ymin = -20, ymax = 0,
fill="green",alpha = 0.05)+
geom_rect(xmin = 2.2, xmax = 69, ymin = -20, ymax = 0,
fill = "yellow", alpha = 0.05) +
geom_rect(xmin = 69, xmax = 120, ymin = -20, ymax = 0,
fill="purple",alpha = 0.05) +
geom_rect(aes(xmin = -2, xmax = 2.2, ymin = 0, ymax = 85,
fill="A", alpha = "D"))+
geom_rect(aes(xmin = 2.2, xmax = 69, ymin = 0, ymax = 85,
fill = "B", alpha = "E")) +
geom_rect(aes(xmin = 69, xmax = 120, ymin = 0, ymax = 85,
fill="C",alpha = "F"))+
geom_pointrange(aes(x = days, y = emission, ymin = emission, ymax = emission + se, color = category, shape = category, group = category),
size = 0.5) +
scale_color_manual(name = "Measurments", labels = c("Period Mean", "Emission"),
values = c("red", "darkblue")) +
scale_fill_manual(name = "Groups", labels = c("A", "B","C"), values = c("green", "yellow","purple"))+
scale_shape_manual(name = "Measurments", labels = c("Period Mean", "Emission"), values = c(17, 19))+
scale_alpha_manual(name = "Groups", labels = c("A", "B", "C"), values = c(0.05,0.05,0.05)) +
guides(fill = guide_legend(override.aes = list(alpha = 1)))
使用组和测量图例进行绘图:
在最后一个绘图中,由于带有alpha值的图例的小矩形显示不好,我们在绘图代码末尾使用guides()函数覆盖图例alpha而不影响主绘图alpha,因此组的图例的alpha值比主绘图的alpha值多。
在此绘图中,我特意将图例组的alpha设置为1,将绘图的alpha设置为0.05,以显示它们之间的差异。您可以为每个图例组设置任何alpha,以使它们更相似
下图显示了图例和绘图的类似alpha:
(通过将绘图alpha设置为0.05,将图例alpha设置为0.2)
---------
您还可以在最后一个绘图的末尾添加theme_bw()
以创建边框(我没有在最后一个绘图中包含它)您可以定义如何将测量分为三组吗?另外变量daysincubated4
似乎缺失。如果您可以使问题重现,那就太好了。您想将所有三个测量都绘制成图形吗(a、b、c)在一个地块(叠加)或者不同的绘图?你说的背景色是什么意思?谢谢你的更正@Peter。我不确定你说的可复制是什么意思?谢谢你的评论@Killbill!是的,所有内容都应该在一个绘图中。可复制:当其他人将问题中的数据和代码复制到他们的控制台时,代码会产生所述的问题在问题中。请阅读以获得真正有用的指南。@Tiptop,我编辑了我的答案,它有一个包含组和测量符号的图例。geom_rect(x=…)如果不使用aes,则在绘图上绘制一个不会影响轴比例的矩形,因此我们可以将矩形延伸到绘图的最顶部或最底部区域。我使用它们在绘图的上部和下部绘制矩形。但是如果您希望在绘图面板中具有图例的矩形,则应在几何矩形(aes)的辅助部分中添加颜色(x=…).Geom_-rect(aes())和Geom_-rect()在我的绘图中在面板区域有轻微重叠。那么“有点不对劲”是什么意思呢.如果你能发布你的情节,那就容易了fixed@Tiptop,现在您可以在我编辑的答案中看到最终绘图,图例alpha与绘图alpha不同。请完全复制新的最终绘图代码。您可以根据首选的alpha更改它。您可以在指南()中更改图例的alpha函数位于代码末尾,以及scale_alpha_manual()中绘图的alpha,代码最后一行前一行。
#final plot with groups and measurments legend
df_fin%>%
ggplot(aes(x=days, y=emission))+
labs(color = "", size= "") +
labs(x = "Incubation time (days)", y = "Production (umol g-1 dw soil h-1)") +
facet_wrap(vars(compound), scales = "free_y") +
geom_rect(xmin = -2, xmax = 2.2, ymin = 85, ymax = 100,
fill="green",alpha = 0.05)+
geom_rect(xmin = 2.2, xmax = 69, ymin = 85, ymax = 100,
fill = "yellow", alpha = 0.05) +
geom_rect(xmin = 69, xmax = 120, ymin = 85, ymax = 100,
fill="purple",alpha = 0.05) +
geom_rect(xmin = -2, xmax = 2.2, ymin = -20, ymax = 0,
fill="green",alpha = 0.05)+
geom_rect(xmin = 2.2, xmax = 69, ymin = -20, ymax = 0,
fill = "yellow", alpha = 0.05) +
geom_rect(xmin = 69, xmax = 120, ymin = -20, ymax = 0,
fill="purple",alpha = 0.05) +
geom_rect(aes(xmin = -2, xmax = 2.2, ymin = 0, ymax = 85,
fill="A", alpha = "D"))+
geom_rect(aes(xmin = 2.2, xmax = 69, ymin = 0, ymax = 85,
fill = "B", alpha = "E")) +
geom_rect(aes(xmin = 69, xmax = 120, ymin = 0, ymax = 85,
fill="C",alpha = "F"))+
geom_pointrange(aes(x = days, y = emission, ymin = emission, ymax = emission + se, color = category, shape = category, group = category),
size = 0.5) +
scale_color_manual(name = "Measurments", labels = c("Period Mean", "Emission"),
values = c("red", "darkblue")) +
scale_fill_manual(name = "Groups", labels = c("A", "B","C"), values = c("green", "yellow","purple"))+
scale_shape_manual(name = "Measurments", labels = c("Period Mean", "Emission"), values = c(17, 19))+
scale_alpha_manual(name = "Groups", labels = c("A", "B", "C"), values = c(0.05,0.05,0.05)) +
guides(fill = guide_legend(override.aes = list(alpha = 1)))