如何将模式添加到ggplot中的一个类别

问题描述 投票:0回答:1

我有以下数据:

data <- structure(list(individual = c("47%", "41%", "12%", "5%", NA, 
"39%", "29%", "12%", "5%", NA, "44%", "39%", "28%", "31%", NA, 
"60%", "59%", "26%", "36%", NA, "73%", "48%", "52%", "36%", NA, 
"49%", "43%", "NA%", "3%", NA, "32%", "34%", "NA%", "2%", NA), 
    group = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
    3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 
    6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L), levels = c("SNs", 
    "SMNs", "NMSC", "Breast cancer", "Thyroid cancer", "Meningioma", 
    "Sarcoma"), class = "factor"), group2 = structure(c(3L, 1L, 
    4L, 2L, NA, 3L, 1L, 4L, 2L, NA, 3L, 1L, 4L, 2L, NA, 3L, 1L, 
    4L, 2L, NA, 3L, 1L, 4L, 2L, NA, 3L, 1L, 4L, 2L, NA, 3L, 1L, 
    4L, 2L, NA), levels = c("CCSS-All treatments", "CCSS-PRS", 
    "SJLIFE-All treatments", "SJLIFE-PRS"), class = "factor"), 
    value = c(47, 41, 12, 5, NA, 39, 29, 12, 5, NA, 44, 39, 28, 
    31, NA, 60, 59, 26, 36, NA, 73, 48, 52, 36, NA, 49, 43, NA, 
    3, NA, 32, 34, NA, 2, NA), id = 1:35, group3 = c("SJLIFE", 
    "CCSS", "SJLIFE", "CCSS", NA, "SJLIFE", "CCSS", "SJLIFE", 
    "CCSS", NA, "SJLIFE", "CCSS", "SJLIFE", "CCSS", NA, "SJLIFE", 
    "CCSS", "SJLIFE", "CCSS", NA, "SJLIFE", "CCSS", "SJLIFE", 
    "CCSS", NA, "SJLIFE", "CCSS", "SJLIFE", "CCSS", NA, "SJLIFE", 
    "CCSS", "SJLIFE", "CCSS", NA), group4 = c("All treatments", 
    "All treatments", "PRS", "PRS", NA, "All treatments", "All treatments", 
    "PRS", "PRS", NA, "All treatments", "All treatments", "PRS", 
    "PRS", NA, "All treatments", "All treatments", "PRS", "PRS", 
    NA, "All treatments", "All treatments", "PRS", "PRS", NA, 
    "All treatments", "All treatments", "PRS", "PRS", NA, "All treatments", 
    "All treatments", "PRS", "PRS", NA)), row.names = c(NA, -35L
), class = "data.frame")

我想绘制条形图,并将条纹添加到第 3 组中的“CCSS”类别,并在第 4 组中添加颜色,所以我想要两个图例。我该怎么做?

这是我当前的代码:

ggplot(data, aes(x=as.factor(id), y=value, fill=group4)) +      
  geom_bar(aes(x=as.factor(id), y=value, fill=group4), stat="identity", alpha=1, width=0.95) +
  geom_col_pattern(
    aes(pattern=group3), 
    fill = 'white',
    colour = 'black', 
    pattern_density = 0.5
  ) 
r ggplot2
1个回答
0
投票

这将实现您想要的输出。您可能需要调整一些参数才能完全达到您想要的效果。我提供了一些用于调整模式的非详尽选项,以帮助您顺利完成任务。

请注意,除非您排除 NA 值,否则

geom_bar_pattern()
会引发错误,因此我已使用
dplyr::filter()
排除了这些值。

library(dplyr) # 1.1.4 
library(ggplot2) # 3.5.1
library(ggpattern) # 1.0.1

ggplot() +      
  geom_bar(data = filter(data, !is.na(group4)),
           aes(x = as.factor(id), y = value, fill = group4), 
           stat = "identity", 
           alpha = 1, 
           width = 0.95) +
  geom_bar_pattern(data = filter(data, !is.na(group3)),
                   aes(x = as.factor(id), y = value, pattern = group3),
                   stat = "identity",
                   fill = NA,
                   colour = 'black',
                   pattern_density = 0.1,
                   pattern_spacing = .03,
                   pattern_fill = NA,
                   pattern_colour = "black",
                   pattern_key_scale_factor = 0.6,
                   inherit.aes = FALSE) +
  scale_pattern_manual(values = c("stripe", "circle")) 

# Warning messages:
# 1: Removed 2 rows containing missing values or values outside the scale range
# (`geom_bar()`). 
# 2: Removed 2 rows containing missing values or values outside the scale range
# (`geom_bar_pattern()`).

result

© www.soinside.com 2019 - 2024. All rights reserved.