使用ggplot绘制堆叠条形图,并按照另一个变量进行排序

3

我正在尝试创建一个“订单”堆叠条形图,每个堆叠由一种变量着色并按另一种变量排序,请参见下面的示例:

library(ggplot2)
library(dplyr)

data(iris)

chart.df.st00 <- iris %>%
  as_tibble %>%
  mutate(`Sepal.Length`=round(`Sepal.Length`)) %>%
  count(Species,`Sepal.Length`) %>%
  mutate(`Sepal.Length`=as.character(`Sepal.Length`)) %>%
  group_by(Species) %>%
  mutate(percent=n/sum(n)*100) %>%
  arrange(desc(n)) %>%
  mutate(rank=1:n()) %>%
  ungroup %>%
  mutate(rank=paste(Species,rank,sep='-')) 

chart.df.st01 <- chart.df.st00 %>%
  left_join(chart.df.st00 %>%
              distinct(`Sepal.Length`) %>%
              mutate(color=colorRampPalette(
                RColorBrewer::brewer.pal(length(unique(chart.df.st00$`Sepal.Length`)),'Set1'))(length(unique(chart.df.st00$`Sepal.Length`)))))

chart.color1.st00 <- chart.df.st01 %>%
  distinct(rank,color) %>%
  arrange(rank)

chart.color1.st01 <- chart.color1.st00$color
names(chart.color1.st01) <- chart.color1.st00$rank

chart1 <- ggplot(data=chart.df.st01,
       aes(x=1,y=percent)) +
  geom_bar(aes(fill=rank),stat='identity') +
  scale_fill_manual(values=chart.color1.st01) +
  facet_wrap(.~Species,ncol = 1) +
  scale_y_reverse(breaks=c(0,25,50,75,100),labels=c(100,75,50,25,0)) +
  coord_flip()

chart.color2.st00 <- chart.df.st01 %>%
  distinct(color,Sepal.Length) %>%
  arrange(Sepal.Length)

chart.color2.st01 <- chart.color2.st00$color
names(chart.color2.st01) <- chart.color2.st00$`Sepal.Length`

chart2 <- ggplot(data=chart.df,
       aes(x=1,y=percent)) +
  geom_bar(aes(fill=`Sepal.Length`),stat='identity') +
  scale_fill_manual(values=chart.color2.st01) +
  facet_wrap(.~Species,ncol = 1) +
  coord_flip()

在我的示例中,每个堆栈都由Sepal.Length填充,并按rank排序,chart1具有我想要的堆栈顺序,但没有图例,而chart2具有我想要的图例,但不具有堆栈的顺序。
是否有一种方法可以在单个图表中具有chart1的堆积条和chart2的图例?
谢谢!
1个回答

5
使用第二个图表的代码,可以通过在`group` aes上另外映射`rank`来实现:
library(ggplot2)
library(dplyr)

data(iris)

chart.df.st00 <- iris %>%
  as_tibble %>%
  mutate(`Sepal.Length`=round(`Sepal.Length`)) %>%
  count(Species,`Sepal.Length`) %>%
  mutate(`Sepal.Length`=as.character(`Sepal.Length`)) %>%
  group_by(Species) %>%
  mutate(percent=n/sum(n)*100) %>%
  arrange(desc(n)) %>%
  mutate(rank=1:n()) %>%
  ungroup %>%
  mutate(rank=paste(Species,rank,sep='-')) 

chart.df.st01 <- chart.df.st00 %>%
  left_join(chart.df.st00 %>%
              distinct(`Sepal.Length`) %>%
              mutate(color=colorRampPalette(
                RColorBrewer::brewer.pal(length(unique(chart.df.st00$`Sepal.Length`)),'Set1'))(length(unique(chart.df.st00$`Sepal.Length`)))))
#> Joining, by = "Sepal.Length"

chart.color2.st00 <- chart.df.st01 %>%
  distinct(color,Sepal.Length) %>%
  arrange(Sepal.Length)

chart.color2.st01 <- chart.color2.st00$color
names(chart.color2.st01) <- chart.color2.st00$`Sepal.Length`

ggplot(data=chart.df.st01,
                 aes(x=1,y=percent)) +
  geom_bar(aes(fill=`Sepal.Length`, group = rank), stat='identity') +
  scale_fill_manual(values = chart.color2.st01) +
  facet_wrap(.~Species,ncol = 1) +
  scale_y_reverse(breaks=c(0,25,50,75,100),labels=c(100,75,50,25,0)) +
  coord_flip()


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接