R 如何向绘图Y轴上的每个点添加三个条形图

R 如何向绘图Y轴上的每个点添加三个条形图,r,ggplot2,statistics,R,Ggplot2,Statistics,我有一个重要的三方互动,我想策划。这三个因素是采集、品种和灌溉,响应变量为MeanGlucVi。我目前的想法(接受其他建议)是在Y轴上绘制一个带有meanGlucCVI响应的条形图,在X轴上绘制灌溉。在X轴上的每个灌溉处理处,每个收集处理将有一个条形。最后,我将为每种类型制作一张这些图 我的问题是,我不知道如何将三个收集栏添加到我的绘图中。我已经看过很多次这样的图表,但是我没有足够的理由用R来实现它 这似乎是显示这些数据的合理方式吗?如果是这样,我如何为它编写代码?我认为使用ggplot及其fa

我有一个重要的三方互动,我想策划。这三个因素是采集、品种和灌溉,响应变量为MeanGlucVi。我目前的想法(接受其他建议)是在Y轴上绘制一个带有meanGlucCVI响应的条形图,在X轴上绘制灌溉。在X轴上的每个灌溉处理处,每个收集处理将有一个条形。最后,我将为每种类型制作一张这些图

我的问题是,我不知道如何将三个收集栏添加到我的绘图中。我已经看过很多次这样的图表,但是我没有足够的理由用R来实现它

这似乎是显示这些数据的合理方式吗?如果是这样,我如何为它编写代码?我认为使用
ggplot
及其
facetwrap
函数可能是有意义的,或者可能只是对单个图形使用
ggplot
,并将它们与基于R的
gridExtra
组合(如果可能的话)

以下是我当前的数据集:

dput(head(dataAvgGlucCVI))


structure(list(Collection = structure(c(1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("1", "2", "3"), class = "factor"), Variety = structure(c(1L, 
1L, 1L, 1L, 1L, 2L), .Label = c("Hodag", "Lamoka", "Snowden"), class = "factor"), 
    Irrigation = structure(c(1L, 2L, 3L, 4L, 5L, 1L), .Label = c("Rate1", 
    "Rate2", "Rate3", "Rate4", "Rate5"), class = "factor"), meanGlucCVI = c(0.03475, 
    0.03475, 0.0455, 0.047, 0.061, 0.04275)), row.names = c(NA, 
-6L), groups = structure(list(Collection = structure(c(1L, 1L
), .Label = c("1", "2", "3"), class = "factor"), Variety = structure(1:2, .Label = c("Hodag", 
"Lamoka", "Snowden"), class = "factor"), .rows = list(1:5, 6L)), row.names = c(NA, 
-2L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame")) 

我不完全理解什么变量应该映射到什么,但这里是第一步,实际上是得到一个绘图。如果您发现自己编写代码有困难,您可以对希望更改的内容提供反馈。假设
df
是由
df生成的,非常感谢您的分享!这似乎是理想的图形。然而,当我运行这个程序时,我得到了错误代码“errorin-FUN(X[[I]],…):object'meanglucvi'notfound”。你知道这意味着什么,或者我能做些什么来解决它吗?这可能意味着你正在使用的data.frame不包含名为“meanglucvi”的列。您必须检查data.frame中是否存在此列,或者将
y=meanGlucCVI
重命名为
y=your\u other\u columname
。谢谢!我能看出哪里出了问题。这对我的数据非常有效,再次感谢您的大力帮助
library(ggplot2)

# I'm including a second factor for illustration purposes.
df2 <- df
df2$Collection <- as.factor(2)
# I'm reversing the order of the response to visually distinguish them
df2$meanGlucCVI <- rev(df2$meanGlucCVI)

# Now I'll combine them
df <- rbind(df, df2)

# You give ggplot the data.frame, and map inside aes() what 
# variable you want to map to what aesthetic.
ggplot(df, aes(x = Irrigation, y = meanGlucCVI, fill = Collection)) +
  # We'll dodge the groups (determined by fill) so that they are not stacked
  geom_col(position = position_dodge(width = 0.7), width = 0.6) +
  # You can facet on a variable, for example Variety
  facet_wrap(~ Variety)