我正在try 给分组的条形图赋予相同 colored颜色 的不同深浅,这些条形图也是多面的.

以下是我已经拥有的代码和输出:

temp.data = data.frame (Species  = rep(c("A","B"),each=2, times=2),
                        Status = rep(c("An","Bac"), times=4),
                        Sex = rep(c("Male","Female"), each=4, times=1),
                        Proportion = c(6.86, 7.65, 30.13, 35.71, 7.13, 10.33, 29.24, 31.09))

ggplot(temp.data, aes(x = Species, y = Proportion, fill = Status, alpha = Species)) +
  geom_bar(stat='identity', position = position_dodge(width = 0.73), width=.67) +
  facet_grid(Sex ~ .) + xlab("X title") + 
  ylab("Y title") + 
  scale_fill_manual(name = "Status", labels = c("An","Bac"),
                    values = c("#86a681","#0a3e03")) +
  scale_alpha_manual(values = c(0.6, 1)) +
  theme_light() + theme(legend.title= element_text(size = 8),
                        legend.title.align=0.5,
                        legend.position = c(),
                        legend.background=element_blank(),
                        legend.text=element_text(size=7),
                        legend.key.size = unit(0.6, 'cm'),
                        axis.title.x = element_text(size = 9),
                        axis.title.y = element_text(size = 9),
                        axis.title.y.right = element_text("Sex of groups"),
                        strip.background = element_rect(
                          color="lightgrey", fill="white", size=0.5, linetype="solid"),
                        strip.text.y = element_text(color = "darkgrey", face = "bold"))

enter image description here

然而,我在这里找不到一个答案来为不同的wine 吧群体分配不同的 colored颜色 .我终于想出了scale_alpha_manual()的函数,但它不能手动赋予 colored颜色 ,它应该是定义的"填充"的阿尔法刻度.因此,这个问题在这里也不是重复的问题.

Here's how the desired output should seem like (preferably with the legends): enter image description here

提前谢谢你了.

推荐答案

你可以使用ggnewscale套餐.下面,我分别制作了物种A和B的数据子集.然后,我用一组 colored颜色 制作了物种A条,然后调用new_scale_fill()来初始化一个新的填充比例.然后,我制作了不同 colored颜色 的物种B条.这两个栏都将有一组关联的 colored颜色 和一个可以用scale_fill_manual()设置的图例.

library(ggnewscale)
library(ggplot2)
library(dplyr)
temp.data = data.frame (Species  = rep(c("A","B"),each=2, times=2),
                        Status = rep(c("An","Bac"), times=4),
                        Sex = rep(c("Male","Female"), each=4, times=1),
                        Proportion = c(6.86, 7.65, 30.13, 35.71, 7.13, 10.33, 29.24, 31.09))

tempA <- temp.data %>% filter(Species == "A")
tempB <- temp.data %>% filter(Species == "B")


ggplot() +
  geom_bar(data = tempB, 
           mapping = aes(x = Species, y = Proportion, fill = Status), 
           stat='identity', 
           position = position_dodge(width = 0.73), 
           width=.67) +
  scale_fill_manual(name = "Species", 
                    labels = c("An","Bac"),
                    values = c("#86a681","#0a3e03"),
                    guide = guide_legend(override.aes = list(fill=c("gray75", "gray25")))) + 
  new_scale_fill() + 
  geom_bar(data = tempA, 
           mapping = aes(x = Species, y = Proportion, fill = Status), 
           stat='identity', 
           position = position_dodge(width = 0.73), 
           width=.67, show.legend=FALSE) +
  scale_fill_manual(name = "Species A: Status", 
                    labels = c("An","Bac"),
                    values = c("brown2","brown4")) +
  facet_grid(Sex ~ .) + 
  xlab("X title") + 
  ylab("Y title") + 
  theme_light() + theme(legend.title= element_text(size = 8),
                        legend.title.align=0.5,
                        legend.position = c(),
                        legend.background=element_blank(),
                        legend.text=element_text(size=7),
                        legend.key.size = unit(0.6, 'cm'),
                        axis.title.x = element_text(size = 9),
                        axis.title.y = element_text(size = 9),
                        axis.title.y.right = element_text("Sex of groups"),
                        strip.background = element_rect(
                          color="lightgrey", fill="white", linewidth=0.5, linetype="solid"),
                        strip.text.y = element_text(color = "darkgrey", face = "bold"))

创建于2023-04-26,共reprex v2.0.2

R相关问答推荐

次级y轴R gggplot2

如何在RMarkdown LaTex PDF输出中包含英语和阿拉伯语?

R中的子集文件—读取文件名索引为4位数字序列,例如0001到4000,而不是1到4000)

如何写一个R函数来旋转最后n分钟?

如何在ggplot中标记qqplot上的点?

制作等距离的线串副本

如何得到每四个元素向量R?

根据类别合并(汇总)某些行

在R函数中使用加号

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

如何从嵌套数据中自动创建命名对象?在R中

按镜像列值自定义行顺序

使用ggplot2绘制具有边缘分布的坡度图

生存时间序列的逻辑检验

具有自定义仓位限制和计数的GGPLATE直方图

策略表单连接两个非常大的箭头数据集,而不会 destruct 内存使用

R dplyr::带有名称注入(LHS of:=)的函数,稍后在:=的RHS上引用

将R中对象的CSV数组转换为JSON数组

打印的.txt文件,将值显示为&Quot;Num&Quot;而不是值

从data.table列表中提取特定组值,并在R中作为向量返回