我有下面的数据:

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组中添加 colored颜色 ,所以我想要两个传奇.我该怎么做?

这是我当前的代码:

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
  ) 

推荐答案

这将实现您想要的输出.您可能需要调整一些参数才能完全达到您想要的效果.我提供了大量调整模式的选项,以帮助您继续前进.

我已经使用dplyr::filter()过滤了"CCSS"值.鉴于NA值没有显示在图中,我使用scale_fill_discrete(na.translate = FALSE)从说明中省略了它们,如果不适合,则删除这一行.

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

ggplot() +      
  geom_bar(data = data,
           aes(x = as.factor(id), y = value, fill = group4), 
           stat = "identity", 
           alpha = 1, 
           width = 0.95) +
  geom_bar_pattern(data = filter(data, group3 == "CCSS"),
                   aes(x = as.factor(id), y = value, pattern = group3),
                   stat = "identity",
                   fill = NA,
                   width = 1,
                   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 = "stripe") +
  scale_fill_discrete(na.translate = FALSE)

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

result

R相关问答推荐

feature_weights参数没有影响Xgboost

保存shiny 的代码嗅探器:避免$ Symbol问题

如何根据包含相同值的某些列获取总额

R Highcharts与两个位置关联的注释

gt()从gt为相同内容的单元格 colored颜色 不同?

有没有一个R函数允许你从一个数字变量中提取一个数字,而不考虑它的位置(不仅仅是第一个或最后一个数字?

如何从R中的字符串元素中减go 一个数字?

整数成随机顺序与约束R?

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

如何改变时间图R中的悬停信息?

如何计算多个日期是否在一个日期范围内

使用rest从header(h2,h3,table)提取分层信息

解析R函数中的变量时出现的问题

如何将网站图像添加到带有极坐标的面包裹条形图?

如何在R中使用hmm TMB提前一步预测观察到的状态?

删除数据帧中特定行号之间的每第三行和第四行

数据集上的R循环和存储模型系数

在使用SliderInput In Shiny(R)设置输入数据的子集时,保留一些情节痕迹

计算多变量的加权和

Data.table::Shift type=允许扩展数据(&Q;LAG&Q;)