我正在try 制作一个条形图,对于包括杀虫剂在内的治疗,我希望有交叉舱口,但我目前的代码不起作用,而且我无法想出如何修复它.

这是我数据的一个子集:

dput(peak.hillmeans1[1:12,])
structure(list(Subplot_Descriptions = c("ambient", "ambient", 
"ambient", "ambient", "drought", "drought", "drought", "drought", 
"drought_insecticide", "drought_insecticide", "drought_insecticide", 
"drought_insecticide"), hill.number.type = c("H0", "H1", "H2", 
"Hinf", "H0", "H1", "H2", "Hinf", "H0", "H1", "H2", "Hinf"), 
    hill.number.mean = c(203.425, 41.2441606562857, 12.737168631955, 
    4.18742498977372, 243.933333333333, 47.6826726791051, 14.0547025275759, 
    4.50664860697016, 259.016666666667, 47.6003166273996, 13.7637529655749, 
    4.55027488751543), hill.number.sd = c(60.6538518974682, 6.51934912505994, 
    1.47899803604672, 0.379117399794485, 56.4631708874614, 9.62482790860152, 
    2.24482412930567, 0.537764198394058, 48.9080225184649, 8.0756230742672, 
    1.26719037679015, 0.478045892509355), insecticide = c("ambient_i", 
    "ambient_i", "ambient_i", "ambient_i", "ambient_i", "ambient_i", 
    "ambient_i", "ambient_i", "insecticide", "insecticide", "insecticide", 
    "insecticide")), row.names = c(NA, 12L), class = "data.frame")

这是我现在拥有的代码:

library(ggplot2)
library(ggpattern)

ggplot(peak.hillmeans1, aes(x = hill.number.type, 
                            y = hill.number.mean, 
                            fill = Subplot_Descriptions)) + 
  geom_bar(stat = "identity", position = position_dodge()) +
  geom_errorbar(aes(ymin = hill.number.mean - hill.number.sd, 
                    ymax = hill.number.mean + hill.number.sd), 
                width = 0.2, position = position_dodge(0.9)) + 
  geom_bar_pattern(aes(pattern = insecticide),
                   position = position_dodge(preserve = "single"),
                   pattern = "stripe",
                   pattern_density = 0.1,
                   pattern_spacing = 0.025,
                   pattern_key_scale_factor = 0.6,
                   color = "black") +
  scale_pattern_manual(values = c(insecticide = "stripe", ambient_i = "none")) +
  scale_fill_manual(values = c("ambient" = "darkblue",
                               "drought" = "gray58",
                               "warmed" = "red2",
                               "warmed_drought" = "darkred",
                               "drought_insecticide" = "dodgerblue4", 
                               "insecticide" = "sienna1", 
                               'warmed_drought_insecticide' = "orangered", 
                               'warmed_insecticide' = "salmon")) +
  labs(title = "Mean Hill Numbers for Peak Drought", 
       x = "Hill Number", y = "Mean # of Species", fill = "Treatment") +
  theme_minimal()

推荐答案

library(ggplot2)
library(ggpattern)

ggplot(peak.hillmeans1, aes(x = hill.number.type, 
                            y = hill.number.mean, 
                            fill = Subplot_Descriptions)) + 
  geom_bar_pattern(aes(pattern = insecticide),
                   stat = "identity",
                   position = position_dodge(preserve = "single"),
                   pattern_density = 0.1,
                   pattern_spacing = 0.025,
                   pattern_key_scale_factor = 0.6,
                   color = "black") +
  geom_errorbar(aes(ymin = hill.number.mean - hill.number.sd, 
                    ymax = hill.number.mean + hill.number.sd), 
                width = 0.2, position = position_dodge(0.9)) + 
  scale_pattern_manual(values=c(insecticide = 'stripe', ambient_i = 'none'),
                       guide = guide_legend(override.aes=list(fill=NA))) +
  scale_fill_manual(values = c("ambient" = "darkblue",
                               "drought" = "gray58",
                               "warmed" = "red2",
                               "warmed_drought" = "darkred",
                               "drought_insecticide" = "dodgerblue4", 
                               "insecticide" = "sienna1", 
                               'warmed_drought_insecticide' = "orangered", 
                               'warmed_insecticide' = "salmon"),
                    guide = guide_legend(override.aes = list(pattern = "none"))) +
  labs(title = "Mean Hill Numbers for Peak Drought", 
       x = "Hill Number", y = "Mean # of Species", 
       fill = "Treatment", pattern = "Insecticide") +
  theme_minimal() 

创建于2024年4月17日,共有reprex v2.0.2

R相关问答推荐

从R中的另一个包扩展S3类的正确方法是什么

如何替换R中数据集列中的各种字符串

r中的stat_difference函数不起作用

判断字符串中数字的连续性

如何根据组大小应用条件过滤?

如何通过Docker部署我的shiny 应用程序(多个文件)

从外部文件读取多个值作为字符向量

以更少间隔的较小表中的聚合离散频率表

提取一个列表中单个列的重复观察结果R

SHINY:使用JS函数应用的CSS样式显示HTML表格

从R中的对数正态分布生成随机数的正确方法

随机森林的带Shap值的蜂群图

在使用具有Bray-Curtis相似性的pvCluust时计算p值

网络抓取新闻标题和时间

长/纬点继续在堪萨斯-SF结束,整齐的人口普查

R-找出存在其他变量的各种大小的所有组合

如何合并不同列表中的数据文件,包括基于名称的部分匹配,而不是一对一等价

Ggplot2:添加更多特定 colored颜色 的线条

我有2011-2022年的年度数据.如何计算最低年份和最高年份之间的差额?

在直方图中显示两个变量