我有一个堆积图,我已经有两个变量了.我想在图表中添加第三个变量作为符号或对象.这是我想要的图表:

desired graph

我当前的代码是:

sur %>% 
  filter(!(species_broad %in% c("Monkey", "Cattle"))) %>%
  drop_na(circumstances_bite_broad_intended) %>%
  add_count(species_broad) %>%
  mutate(y_species_broad = 
           reorder(
             factor(paste0(species_broad, "\n", "(n = ", n, ")")),
             as.numeric(species_broad)
           )) %>% 
  ggplot(mapping=aes(y= y_species_broad, fill=circumstances_bite_broad_intended)) + 
  geom_bar(colour="black" , position = "fill")+
  scale_x_continuous (labels=c("0","25%","50%","75%","100%"))  +
  labs (x="Proportion of responses",y="Animal species" , fill="Circumstances")+  # "fill" to change the legend title 
  ggtitle ("Circumstances of bite in different animal species")+ 
  scale_fill_manual( values =c ("Threatening Engagement" = "darkred" ,
                                "Friendly Engagement" = "red" , 
                                "Practical Engagement" = "#EC7979" ,
                                "Unintentional Human Engagement" = "lightblue" ,
                                "Apparently unprovoked" = "darkblue"))

我的数据是:

structure(list(species = structure(c(1L, 1L, 3L, 5L, 1L, 2L, 
7L, 2L, 2L, 1L, 3L, 1L, 2L, 3L, 1L, 2L, 1L, 4L, 4L, 7L, 2L, 1L, 
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 3L, 2L, 1L, 1L, 1L, 6L, 7L, 9L), levels = c("Pet dog", 
"Unowned dog", "Dog (unknown status)", "Pet cat", "Unowned cat", 
"Cat (unknown status)", "Rat/rodent", "Monkey", "Cattle"), class = "factor"), 
    circumstances_bite_avoidability = structure(c(1L, 2L, 1L, 
    1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 
    1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 
    1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 
    2L, NA), levels = c("Avoidable", "Unavoidable"), class = "factor"), 
    circumstances_bite_broad_intended = structure(c(2L, 4L, 1L, 
    3L, 4L, 4L, 4L, 5L, 5L, 3L, 5L, 1L, 4L, 5L, 5L, 1L, 5L, 5L, 
    2L, 5L, 4L, 3L, 5L, 2L, 2L, 5L, 4L, 3L, 3L, 5L, 1L, 5L, 1L, 
    1L, 5L, 5L, 4L, 4L, 5L, 1L, 5L, 5L, 1L, 1L, 5L, 5L, 2L, 3L, 
    5L, NA), levels = c("Threatening Engagement", "Friendly Engagement", 
    "Practical Engagement", "Unintentional Human Engagement", 
    "Apparently unprovoked"), class = "factor")), row.names = c(NA, 
50L), class = "data.frame")

推荐答案

有了提供的数据,我们可以做类似这样的事情(主要功能是使用ggpattern包中的gemo_bar_pattern.在图例修改中有一些技巧:

library(tidyverse)
library(ggpattern)

sur <- sur %>% 
  mutate(species = case_when(str_detect(species, "D|dog") ~ "Dog",
                               str_detect(species, "C|cat") ~ "Cat",
                               .default = "Rat/rodent")
         ) %>% 
  filter(!(species %in% c("Monkey", "Cattle"))) %>%
  drop_na(circumstances_bite_broad_intended) %>%
  add_count(species)

sur$y_species <- with(sur, reorder(
  factor(paste0(species, "\n", "(n = ", n, ")")),
  as.numeric(species)))

ggplot(sur, aes(y= y_species, fill=circumstances_bite_broad_intended, pattern = circumstances_bite_avoidability)) + 
  geom_bar_pattern(
    colour = "black", 
    position = "fill",
    pattern_density = 0.1,
    pattern_spacing = 0.015,
    pattern_key_scale_factor = 0.9
  ) +
  scale_x_continuous(labels=c("0","25%","50%","75%","100%")) +
  labs(x="Proportion of responses", y="Animal species", fill="Circumstances", pattern="Avoidability") +
  ggtitle("Circumstances of bite in different animal species") + 
  scale_fill_manual(values =c("Threatening Engagement" = "darkred",
                              "Friendly Engagement" = "red",
                              "Practical Engagement" = "#EC7979",
                              "Unintentional Human Engagement" = "lightblue",
                              "Apparently unprovoked" = "darkblue")) + 
  scale_pattern_manual(values=c("Avoidable" = "stripe", 
                                "Unavoidable" = "none")) +
  theme(legend.key = element_rect(fill = "white")) +
  guides(pattern = guide_legend(override.aes = list(fill = "white")),
         fill = guide_legend(override.aes = list(pattern = "none")))

enter image description here

R相关问答推荐

按列A中的值进行子集化,并获得列C中对应于R中列B的最大值行的值?使用循环自动化此操作

R:如何自动化变量创建过程,其中我需要基于ifelse()为现有变量的每个级别创建一个单独的变量

如何在弹性表中为类别值的背景上色

通过绘图 Select 线串几何体并为其着色

使用rlang s arg_match判断函数输入列表

MCMC和零事件二元逻辑回归

如何对数据集进行逆向工程?

如何计算前一行的值,直到达到标准?

如何优化向量的以下条件赋值?

R函数‘paste`正在颠倒其参数的顺序

未识别时区

将饼图插入条形图

在使用bslb和bootstrap5时,有没有办法更改特定dt行的 colored颜色 ?

从服务器在Shiny中一起渲染图标和文本

如何在使用箭头R包(箭头::OPEN_DATASSET)和dplyr谓词时编写具有整齐计算的函数?

正则表达式在第二个管道和第二个T之后拆分R中的列

如何删除设置大小的曲线图并添加条形图顶部数字的百分比

如何计算每12行的平均数?

R基于变量组合创建新的指标列

我需要使用ggplot2制作堆叠条形图