r - 如何在绘图的 Y 轴上的每个点上添加三个条形
问题描述
我想绘制一个重要的三向交互。这三个因素是收集、品种和灌溉,响应变量是 meanGlucCVI。我目前的想法(对其他建议持开放态度)是制作一个条形图,其中 Y 轴上的 meanGlucCVI 响应和 X 轴上的灌溉。在 X 轴上的每个灌溉处理处,每个收集处理都会有一个条形图。最后,我会为每个品种制作这些图表之一。
我的问题是我不知道如何将三个收集栏添加到我的情节中。我已经多次看到类似这样的图表,但我对 R 的理解还不够扎实,无法实现。
这似乎是显示这些数据的明智方式吗?如果是这样,我该如何为它编写代码?我认为 usingggplot
及其facetwrap
功能可能是有意义的,或者可能只是ggplot
用于单个图形并将它们与gridExtra
base R 结合起来(如果可能的话。)
这是我当前的数据集:
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 <- structure(your_dput_output)
。
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)
推荐阅读
- r - 将字符转换为分钟数
- android - 向下滚动时从多个来源附加到适配器列表
- c++ - 结构/类包装器中的 C++ 重载自动运算符
- c# - 如何将 MassTransit 演示分成生产者和消费者应用程序
- django - 在 Django 中,预选表单选项标签的正确方法是什么?
- python - 如果将大量使用计算,我应该将计算存储在变量中吗?
- linux - 在 CentOS 中配置 Nginx 以加载 Laravel 5.8 站点
- javascript - 如何使用 Sinon 存根“包装器”函数?
- c# - 如何使用 OnRender 中的 drawingContext 绘制精确的 1 个监视器像素线以获得 120 DPI 分辨率?
- node.js - 如何存储 SSE 连接?