带ggplot2的R Barplot-带有不同数字的两个类别!价值观 [英] R Barplot with ggplot2 - two categories with different Numeric! values

查看:71
本文介绍了带ggplot2的R Barplot-带有不同数字的两个类别!价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用ggplot创建一个特定的条形图.到目前为止,一切顺利,这是到目前为止我得到的:

I want to create a specific barplot with ggplot. So far so good, here is what I've got so far:

ggplot(only_savings, aes(DivisionName,  Total_CR)) +
geom_bar(stat="summary", fun.y="sum")

如您所见-有2个部门:电气产品和动力研磨.在Y轴上,我们总结了数字节省量(Total_CR-总成本降低).但是,我想分两个部分来划分标准:Repetitive_Savings和MDF_Savings.因此,它看起来像这样:

As you can see - there are 2 Divisions: Electrification Products and Power Grinds. On the Y-Axis we have numeric Savings that are summed up (Total_CR - total cost reduction). BUT, I would like to SPLIT the Bar in 2 more parts: Repetitive_Savings and MDF_Savings. So it would look like this:

这是数据:(好的,我无法发布屏幕截图,因此我将粘贴一些行)

And here is the data: (Ok, I can't post a screenshot, so I'll paste some rows)

DivisionName                Repetitive_Savings       MDF_Savings    Total_CR
Power Grids                 86.571656                0              86.571656
Power Grids                 183.461221               0              183.461221
Power Grids                 2326.963118              0              2326.963118
Electrification Products    1249.323277              0              1249.323277
Electrification Products    6.849336                 0              6.849336
Electrification Products    3.808845                 0              3.808846

DivisionName是一个因子,其他3个是数字值.如何获得我在涂料中绘制的Barplots?

DivisionName is a factor, the other 3 are numeric Values. How can I achieve the Barplots that I've sketched in paint?

推荐答案

读入数据

我对您的示例进行了一些更改,因为值0不会为我们显示任何内容.

Read in data

I changed your example a little, since values of 0 aren't going to show anything for us.

only_savings <- read.table(header = TRUE, text = "
DivisionName                Repetitive_Savings       MDF_Savings    Total_CR
'Power Grids'                 86.571656                500              86.571656
'Power Grids'                 183.461221               500              183.461221
'Power Grids'                 2326.963118              500              2326.963118
'Electrification Products'    1249.323277              500              1249.323277
'Electrification Products'    6.849336                 500              6.849336
'Electrification Products'    3.808845                 500              3.808846
")

重塑

ggplot 要求事物采用长格式或整洁"的形式,这意味着每个观察值应为单独的行,该行的另一列告诉使用该行是重复行还是MDF.一种简单的方法是使用 tidyr 包.

Reshape

ggplot requires things to be in long form, or 'tidy' form, which means that each observation should be seperate row, which an additional column telling use whether that row belongs to Repetitive or MDF. One easy way to do that is with the tidyr package.

但是,由于不需要绘制所有行,因此我们必须用Total过滤掉所有行.

We'll have to filter out all the rows with Total though, since they aren't needed to be plotted.

library(tidyr)
pd <- gather(only_savings, 'key', 'value', -DivisionName)
pd <- pd[pd$key != 'Total_CR', ]

创建情节

现在剩下要做的就是为 key 指定填充颜色.

library(ggplot2)
ggplot(pd, aes(DivisionName,  value, fill = key)) +
  geom_bar(stat = "summary", fun.y = "sum")

请注意,我们也可以将其编写如下,其中观察值的叠加与首先求和相同.

Note that we can also write it as follows, where the stacking of the observations is the same as summing them first.

ggplot(pd, aes(DivisionName,  value, fill = key)) +
  geom_bar(stat = "identity")

结果

这篇关于带ggplot2的R Barplot-带有不同数字的两个类别!价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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