我一直在try 创建一个自定义条形图,在这里我可以更改每组的 colored颜色 和图案(条纹或无条纹).

structure(list(Level= c(0.2, 0.3, 0.25, 0.35, 0.4, 0.5, 0.5, 0.6, 0.15, 0.35), Group= c("A", "A", 
"B", "B", "C", "C", "D", "D", 
"E", "E"), Condition = c("no", "yes", "no", "yes", "no", "yes", "no", 
"yes", "no", "yes")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

I have tried the following which creates the graph below. Example

I would however like to have both groups in white color, and if the bars where the condition == yes have black stripes in them as often seen in ggpattern plots.

使用ggpattern,我找不到可行的解决方案[Error in seq.default(from,to,by):invalid'(to-from)/by']是我遇到最多的错误之一.

p<- ggplot(z, aes(fill=Condition, y=Level, x=Group, pattern = Condition)) +
  geom_bar(position="dodge", stat="identity") +
  ggpubr::theme_pubr() +
  theme(legend.position = "top") +
  theme( panel.background = element_rect(colour = "black", size=0.5)) +
  labs(x = "Group", y = "Level")+
  scale_y_continuous(breaks=seq(0, 0.8, 0.1))+
  scale_fill_discrete(name = "", labels = c("No", "Yes")) 

推荐答案

你在找这样的东西吗?

ggplot(z, aes(fill=Condition, y=Level, x=Group, pattern = Condition, 
              pattern_type = Condition)) +
  geom_bar_pattern(position="dodge", stat="identity", pattern_fill = "black",
                   fill = "white", colour = "black", pattern_spacing = 0.01,
                   pattern_frequency = 5, pattern_angle = 45) +
  ggpubr::theme_pubr() +
  theme(legend.position = "top") +
  theme( panel.background = element_rect(colour = "black", size=0.5)) +
  labs(x = "Group", y = "Level")+
  scale_y_continuous(breaks=seq(0, 0.8, 0.1), limits = c(0, 0.8)) +
  scale_pattern_manual(values=c('stripe', 'none')) +
  scale_pattern_type_manual(values=c(NA, NA))

enter image description here

R相关问答推荐

R中的枢轴/转置

如何修复R码的置换部分?

次级y轴R gggplot2

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

将非重复序列高效转换为长格式

在R中为马赛克图中的每个字段着色

当我们有多个特殊字符时,使用gsub删除名称和代码'

根据日期从参考帧中创建不同的帧

我如何才能找到FAMILY=POISSON(LINK=&Q;LOG&Q;)中的模型预测指定值的日期?

如何同时从多个列表中获取名字?

计算满足R中条件的连续列

我将工作代码重构为一个函数--现在我想不出如何传递轴列参数

如何将这个小列表转换为数据帧?

解析嵌套程度极高的地理数据

ggplot R:X,Y,Z使用固定/等距的X,Y坐标绘制六边形热图

变异以按组从其他列创建具有最大和最小值的新列

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

我正在try 创建一个接近cos(X)的值的While循环,以便它在-或+1-E10范围内

无法保存gglot的所有pdf元素

从单个html段落中提取键-值对