更改堆叠条形图的颜色,但保持每个类的堆叠顺序递增

安娜

我一直在尝试创建一个堆积的条形图,该图的颜色与地图中的颜色有关。基本上,我具有给定多边形覆盖的面积比例。我已经预先对df进行了排序,因此每个栈Proportion的降序排列Class如果将fill的值设置为一个continues变量Cluster,但我不能更改堆栈的特定颜色),则可以正常工作;如果将它们转换为因数,Clu则堆栈的顺序会丢失或我可以管理对它们进行排序,但不是对每个类都对整个图进行排序...Cluster在不同的类中可能会发生相同的情况,即群集2

          Num    Class Cluster Proportion   Clu Order consec
1    9  Class_9       2      0.859   Two     1      1
2    9  Class_9       5      0.141  Five     2      2
3   10 Class_10       2      0.622   Two     1      3
4   10 Class_10       1      0.179   One     2      4
5   10 Class_10       7      0.165 Seven     3      5
6   10 Class_10       6      0.034   Six     4      6
7   11 Class_11       7      1.000 Seven     1      7
8   12 Class_12       2      0.571   Two     1      8
9   12 Class_12       8      0.289 Eight     2      9
10  12 Class_12       1      0.140   One     3     10
11  13 Class_13       8      0.581 Eight     1     11
12  13 Class_13       4      0.210  Four     2     12
13  13 Class_13       2      0.112   Two     3     13
14  13 Class_13       3      0.079 Three     4     14
15  13 Class_13       5      0.018  Five     5     15

我已经设法使代码走得更远了。

cols<-c(One='Blue',Two='Red',Three='Yellow',Four='lightblue',Five='darkgrey',Six='Black', Seven='cyan',Eight='Green')
  

ggplot(Tx, aes(x=Class, y=Proportion, fill= Clu)) + 
  geom_col(width = .7, colour="black", lwd=0.1) +
  geom_text(aes(label=ifelse(Proportion >= 0.05, sprintf("%.2f",Proportion),"")),
            position=position_stack(vjust=0.5), colour="white") +
  coord_flip() +
  scale_y_continuous(labels = function(y) paste0(y))+
  scale_fill_manual(values = cols)+ 
  labs(y="", x="")

总而言之,我想有一个图表,其中每个类别比例都按升序排列,但是我为每个群集指定了颜色

在此处输入图片说明

尼洛克

一种选择(与您的想法有所不同)是使用position.dodgetidytext::reorder_within

library(tidyverse)
library(tidytext)

cols<-c('Blue','Red','Yellow','lightblue','darkgrey','Black', 'cyan', 'Green')

Tx %>%
  mutate(Cluster2 = reorder_within(Cluster, Proportion, Class)) %>%
  ggplot(aes(Cluster2, Proportion, fill = as.factor(Cluster))) +
  geom_col(position = position_dodge2(preserve = "single")) +
  scale_x_reordered() +
  scale_fill_manual(values = cols) +
  coord_flip() +
  facet_grid(Class~., scales = 'free_y', space = 'free')

在此处输入图片说明


如果您确实需要按不同顺序堆叠的条形图,则另一种选择是分别为每个类生成图(这允许正确的顺序),然​​后将它们全部堆叠在一起。可以使用cowplot::plot_grid来完成cowplot::get_legend

以正确的顺序生成地块列表,并将其堆叠为一个地块。

library(tidyverse)
library(cowplot)

Tx2 <- Tx %>%
  mutate(Cluster = factor(Cluster))

cols<-c(One='Blue',Two='Red',Three='Yellow',Four='lightblue',Five='darkgrey',Six='Black', Seven='cyan',Eight='Green')


p_list <- lapply(unique(Tx2$Class), function(x){
  p <-  Tx2 %>%
    filter(Class == x) %>%
    ggplot(aes(Class, Proportion, fill = reorder(Clu, -Proportion))) +
    geom_col(color = 'black') +
    geom_text(aes(label=ifelse(Proportion >= 0.05, sprintf("%.2f",Proportion),"")),
              position=position_stack(vjust=0.5), 
              color = 'white') +
    coord_flip() +
    scale_fill_manual(values = cols) +
    labs(x = NULL, y = NULL) +
    theme_minimal() +
    theme(legend.position = 'none') 
  
 if (x != 'Class_13') p <- p + theme(axis.text.x = element_blank()) 
  
 p
})



p_col <- plot_grid(plotlist = p_list,
                   ncol = 1, 
                   align = 'v',
                   rel_heights = c(rep(1,4), 1.2))

生成要使用的图例。

p <- ggplot(Tx2, aes(Class, Proportion, fill = reorder(Clu, as.numeric(Cluster)))) +  
  geom_col(color = 'black') +
  scale_fill_manual(values = cols, labels= 1:8, name = 'Cluster')
l <- cowplot::get_legend(p)

将堆积的地块和图例放在一起。

plot_grid(p_col, l, rel_widths = c(3, .4))

!在此处输入图片说明


数据

Tx <- read.table(text = 
'  Num    Class Cluster Proportion   Clu Order consec
1    9  Class_9       2      0.859   Two     1      1
2    9  Class_9       5      0.141  Five     2      2
3   10 Class_10       2      0.622   Two     1      3
4   10 Class_10       1      0.179   One     2      4
5   10 Class_10       7      0.165 Seven     3      5
6   10 Class_10       6      0.034   Six     4      6
7   11 Class_11       7      1.000 Seven     1      7
8   12 Class_12       2      0.571   Two     1      8
9   12 Class_12       8      0.289 Eight     2      9
10  12 Class_12       1      0.140   One     3     10
11  13 Class_13       8      0.581 Eight     1     11
12  13 Class_13       4      0.210  Four     2     12
13  13 Class_13       2      0.112   Two     3     13
14  13 Class_13       3      0.079 Three     4     14
15  13 Class_13       5      0.018  Five     5     15')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

堆叠的条形图,每个堆叠的独立填充顺序

堆叠条形图的颜色

每个条带颜色渐变的堆叠条形图

更改堆叠条形图的打印顺序

使用下拉菜单更改堆叠条形图颜色

Google堆叠条形图颜色

堆叠条形图,颜色条

如何绘制堆叠的条形图,其中每个条形图的顺序是基于一列的,而每个色阶的颜色是基于另一个的?

d3.js堆叠条形图对堆叠中各个值的排序/更改顺序

更改堆叠条形图上的字体颜色

更改堆叠条形图中的颜色

堆叠堆积的条形图:如何控制每个堆积中条形的顺序

按顺序指示在ggplot2中堆叠条形图的y方向上的更改顺序

D3堆叠的条形图,每个堆叠由不同的组设置不同的颜色,

堆叠条形图断开

堆叠的 100% 条形图

堆叠水平条形图

堆叠条形图更改x轴图编号值

堆叠的条形图可能不符合更改的颜色

反转堆叠条形图的顺序-在ggplot中突出显示

按特定顺序订购堆叠的Vega Lite条形图

堆叠条形图ggplot2中的标签顺序

使用键对堆叠的条形图进行颜色编码

组合分组/堆叠条形图的不同颜色条 - R

密谋:分组和堆叠的条形图,堆叠条形的颜色不同吗?

使用R base的堆叠条形图:如何在每个堆叠条形内添加值

如何使用ggplot2将图例标题,键顺序和颜色更改为R中的多堆叠条形图

从 dataframe.plot 更改堆叠条形图中的颜色

ggplot2堆叠条形图:每个条形是一种颜色,堆叠组使用alpha表示