如何绘制一个堆叠的条形图,其中每个条形图的顺序是基于一列的,而每个色阶的颜色是基于另一个的? [英] How to plot a stacked bar where the order in each bar is based on a column and the color of each level is based on another?

查看:75
本文介绍了如何绘制一个堆叠的条形图,其中每个条形图的顺序是基于一列的,而每个色阶的颜色是基于另一个的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一个堆叠的条形,其中级别的顺序由一列定义,而颜色由另一列定义.

我读取了一个csv文件,并且数据框具有以下列:

  • 方法(字符)
  • 属性(字符)
  • 订单(整数)
  • 成功(双精度):成功是轴"y",方法"是轴"x".

数据

我的数据框的一个子集是:

 方法<-c("MF","MF","MF","MF","MF","MF","RF","RF","RF","RF," RF," RF)属性<-c("P1","P2","P3","P6","P5","P7","P1","P6","P2","P5","P4","P7")顺序<-c(1,2,3,4,5,6,1,2,3,4,5,6)成功<-c(87.612,4.583,0.286,6.122,0.788,0.573,87.612,6.409,4.332,0.895,0.0,0.573)REF01<-data.frame(方法,属性,顺序,成功) 

成功是订单"列之后的增量列.例如,方法RF的前三个属性(P1,P6和P2)的成功为(87.612 + 6.409 + 4.332).

所需结果

我需要根据 Order 列为每个 Method 堆叠 Success ,但是我想根据 Property进行着色列.我在Excel中手动绘制了这个小示例想要看到的内容.

下图显示了蓝色的(P1)是这两种方法的第一个属性.每个方法的第二个属性是不同的.P2(棕色)是MF的第二个和RF的第三个,而P6(黄色)是RF的第二个和MF的第四个.因此,MFbar颜色如下:蓝色,棕色,灰色(RF,黄色和深蓝色不存在的属性.RFbar颜色如下:蓝色,黄色,棕色,蓝色和绿色)

有没有办法绘制这个图?我感谢您的帮助.

我也忘记写我正在使用 ggplot2 ,但是我知道代码很简单.

  p<-ggplot(REF01,aes(x = Method,y = Success))+geom_bar(aes(fill = Property),stat ="identity") 

解决方案

您可以使用 group 美学来控制堆叠顺序,如下所示:

I'm trying to plot a stacked bar where the order of the levels is defined by a column and the color is defined by another one.

I read a csv file and the dataframe has the following columns:

  • Method (character)
  • Property (character)
  • Order (integer)
  • Success (double): Success is the axis "y" and Method is the axis "x".

Data

A subset of my dataframe is:

Method <- c("MF", "MF", "MF", "MF", "MF", "MF", "RF", "RF", "RF", "RF", "RF", "RF")
Property <- c("P1","P2","P3","P6","P5","P7","P1","P6","P2","P5","P4","P7") 
Order <- c(1,2,3,4,5,6,1,2,3,4,5,6)
Success <- c(87.612,4.583,0.286,6.122,0.788,0.573,87.612,6.409,4.332,0.895,0.0,0.573)

REF01 <- data.frame(Method, Property, Order, Success)

Success is an incremental column following the Order column. For example, the Success for method RF for the first 3 Properties (P1, P6 and P2) is (87.612 + 6.409 + 4.332).

Desired Result

I need to stack Success for each Method based on the Order column but I would like to color based on the Property column. I draw manually in Excel what I would like to see for this small example.

The picture below shows that the blue one (P1) is the first property for both methods. The second property is different on each method. P2 (brown) is the second one for the MF and the third one for RF, while the P6 (yellow) is the second one for RF and the fourth for MF. So, the MFbar color follows: blue, brown, gray (a property that doesn't exist in RF, yellow and dark blue. The RF bar color follows: blue, yellow, brown, blue and green)

Is there a way to plot this? I appreciate your help.

I also forgot to write I'm using ggplot2, but I know that the code is simple.

p <- ggplot(REF01, aes(x=Method, y=Success)) +  
       geom_bar(aes(fill = Property) , stat = "identity")

解决方案

You can use the group aesthetic to control the stacking order as shown in this answer.

ggplot(df, aes(x = Method, y = Success, group = Order)) +  
    geom_col(aes(fill = Property) )

Note the default order of stacked bars is top to bottom. To reverse we can use position = position_stack(reverse = TRUE).

ggplot(df, aes(x = Method, y = Success, group = Order)) +  
    geom_col(aes(fill = Property), position = position_stack(reverse = TRUE) )

这篇关于如何绘制一个堆叠的条形图,其中每个条形图的顺序是基于一列的,而每个色阶的颜色是基于另一个的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆