r - 如何使用 ggplot (R studio) 绘制其中项目由分类列分隔的绘图?
问题描述
从类似于以下的矩阵(但要大一点),在我的环境中保存为“vb”:
Food Counts se Category
Apple 3 1 Fruit
Cauliflower 4 0.5 Vegetable
Banana 1 3 Fruit
Pear 7 3.2 Fruit
Broccoli 4 1.4 Vegetable
Brussel's sprouts 7 7 Vegetable
我想制作一个图表,显示不同食物的计数+标准误差,按“类别”组织(即,水果和蔬菜聚集在不同的地块中)。我习惯于在 R 中使用以下代码:
ggplot(vb, aes(y=Counts, x=Food)) +
geom_point(color="#104E8B") +
geom_errorbar(aes(ymin=Counts-2*se, ymax=Counts+2*se), width=.2,color="#104E8B") +
facet_grid(Category ~ ., scales = "free", space = "free_y") +
ylim(0, 10) +
coord_flip()
但是,这仍然会在蔬菜旁边列出水果,反之亦然。它只省略了属于其他类别的食物的数据点, 参见图片以供参考。
如何让“水果”情节只列出水果,让“蔬菜”情节只列出蔬菜?
dput 格式的数据。
vb <-
structure(list(Food = c("Apple", "Cauliflower", "Banana",
"Pear", "Broccoli", "Brussel's sprouts"), Counts = c(3L,
4L, 1L, 7L, 4L, 7L), se = c(1, 0.5, 3, 3.2, 1.4, 7),
Category = structure(c(1L, 2L, 1L, 1L, 2L, 2L),
.Label = c("Fruit", "Vegetable"), class = "factor")),
row.names = c(NA, -6L), class = "data.frame")
解决方案
推荐阅读
- css - CSS:暂停前的完整动画
- r - R,如何改变 plotly 3d 表面的颜色?
- angular - 具有初始值的 Observable
- vagrant - vagrant 在 mac 上使用 Bento/ubuntu-18.04 的问题
- matlab - 创建离散时间模型
- r - 使用 nls 进行预测,总是返回训练数据集的预测值
- javascript - 当页面重新加载时,我如何调用下拉列表中的最新值?
- html - Plotly 不尊重 RMarkdown out.height,有解决方法吗?
- azure - 从现有 VHD 创建 Azure VM,无需 powershell
- ios - 仅选定行的 UITableView 复选标记