R语言 ggplot2中分面分组条形图的不同色调

xlpyo6sf  于 2023-04-27  发布在  其他
关注(0)|答案(2)|浏览(250)

我试图给同样是分面的分组条形图赋予相同颜色的不同阴影。
下面是我已经拥有的代码和输出:

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"))

然而,我在这里找不到一个答案,为不同的条形组给予不同的颜色。我最终想出了scale_alpha_manual()函数,但它不能手动提供颜色,它应该是定义的“填充”的alpha刻度。因此,这个问题也不是重复的问题。
下面是所需的输出应该是什么样子(最好是带有图例):

先谢谢你。

pb3skfrl

pb3skfrl1#

您可以使用ggnewscale包。下面,我分别为物种A和B制作了数据的子集。然后,我用一组颜色制作了物种A的条形图,然后调用new_scale_fill()来初始化一个新的填充比例。然后,我用不同的颜色制作了物种B的条形图。两个条形图都有一组相关的颜色和一个可以用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

vlf7wbxs

vlf7wbxs2#

我们可以根据一列设置颜色,然后根据另一列添加阴影:

# create custom colours per Species
cols <- ifelse(temp.data$Species == "A", "red", "blue")

# change to lighter shade for An
ix <- which(temp.data$Status == "An")
cols[ ix ] <- sapply(cols[ ix ], function(i) colorRampPalette(c("white", i))(10)[ 5 ])
cols <- setNames(cols, temp.data$grp)

# fill group
temp.data$grp = paste(temp.data$Species, temp.data$Status)

# plot with fill manual
ggplot(temp.data, aes(x = Species, y = Proportion, fill = grp)) +
  geom_bar(stat = "identity",
           position = position_dodge(width = 0.73), width = 0.67) +
  facet_grid(Sex ~ .) +
  scale_fill_manual(values = cols)

相关问题