使用ggplot堆积条形图中的多个色阶

问题描述:

我有一个数据集,其中单个样本属于一个大组和一个较小的子组。每个组有几个子组,但每个子组只能属于一个较大的组。同样,每个样本只能属于一个小组,因此属于一个较大的小组。使用ggplot堆积条形图中的多个色阶

我希望做一个真/假堆叠条形图两种颜色的含义:

  • 纲要(颜色)是大组
  • 填充是真/假数据,但 是两种色调的较大的组轮廓颜色。

这是接近我想要的,但不是浅灰色和深灰色,我喜欢红色水果的明亮和深红色,绿色水果的明亮和深绿色以及浅绿色和深蓝色蓝色水果。

fruit <- data.frame(Sample=1:20, 
       Fruit=c(rep("Apple", 3), rep("Strawberry", 2), rep("Grape", 4), 
         rep("Watermelon", 4), rep("Lime", 3), rep("Blueberry", 2), 
         rep("Plum", 2)), 
       Color=c(rep("Red", 9), rep("Green", 7), 
         rep("Blue", 4)), 
       Ripe=c(rep(c(T, F), 10))) 

fruit$Fruit <- factor(fruit$Fruit, unique(fruit$Fruit)) 
fruit$Color <- factor(fruit$Color, unique(fruit$Color)) 

ggplot(fruit, aes(Fruit)) + 
    theme_bw() + 
    geom_bar(stat="count", position="fill", 
      aes(fill=Ripe, color=Color)) + 
    scale_fill_manual(values=c("grey65", "grey85")) + 
    scale_y_continuous(labels=scales::percent) 

example

这可能吗?还是有更好的方法,我可以直观地将较大群体成员与真/假值区分开来吗? 感谢

+0

使用alpha审美:https://*.com/a/33222028/471093 – baptiste

编辑:这可能是做了,用interaction分配独特的颜色各因素对的比较正确的做法:

ggplot(fruit, aes(Fruit)) + 
    geom_bar(aes(fill=interaction(Color,Ripe), color=Color), stat="count", position="fill")+ 
    scale_y_continuous(labels=scales::percent)+ 
    scale_fill_manual(values=c("pink","lightgreen","lightblue","red", "green","blue"))+ 
    theme_bw() 

enter image description here

地图颜色到fill,和成熟为alpha

ggplot(fruit, aes(Fruit)) + 
    geom_bar(stat="count", position="fill", 
      aes(fill=Color, color=Color,alpha=Ripe)) + 
    scale_y_continuous(labels=scales::percent)+ 
    scale_alpha_discrete(range=c(.5,1))+ 
    theme_bw() 

enter image description here

+0

谢谢!我更喜欢使用alpha的外观。 – hmg

+0

在第二个例子中,如果我想要y轴是“计数”水果而不是百分比,会怎样?另外,成熟为“计数”... – guidebortoli

+1

@guidebortoli我会用'dplyr'首先汇总:'fruit2 %group_by(Fruit,Ripe)%>%summarize(count = n(),Color = first(Color ))'then'ggplot(fruit2,aes(Fruit,count))+ geom_bar(stat =“identity”,aes(fill = interaction(Color,Ripe),color = Color))+ scale_fill_manual(values = c(“pink “,”lightgreen“,”lightblue“,”red“,”green“,”blue“))+ theme_bw()' – Mako212