简体   繁体   English

使用R和ggplot的堆积条形图

[英]Stacked bar plot using R and ggplot

I have tried the suggestions for a stacked barplot in here, but can't seem to make it work. 我在这里尝试了有关堆积条形图的建议,但似乎无法使它起作用。

The data im trying to get plotted: 即时通讯试图绘制的数据:

Technology   TodayScenario  WindScenario  BiomassScenario  
Biomass        0.130          0.0646         0.182 
Fossil gas     0.0965         0.00309        0     
Coal           0.218          0              0     
Oil            0.00696        0              0     
PV             0.0328         0.0245         0.0266
Waste          0.0420         0              0     
Onshore        0.323          0.311          0.337 
Offshore       0.150          0.597          0.454

So im trying to get each scenario plotted with the percentage of how much there is of each technology in the scenario 因此,我试图用每种方案中每种技术的数量百分比来绘制每种方案

I have tried the following for getting a stacked plot, but is not working. 我已经尝试了以下方法来获得堆积图,但是没有用。

ggplot(Distribution, 
       aes(x = WindScenario, y = Technology) + 
   geom_bar(stat = "identity")

In order to have a stacked bar graph with your data, you will need to organise your data into three columns - Technology, Values, and Scenarios - like so (you can copy and paste this into your console): 为了使数据具有堆叠的条形图,您需要将数据分为三列-技术,值和方案-像这样(您可以将其复制并粘贴到控制台中):

structure(list(Technology = structure(c(1L, 1L, 1L, 3L, 3L, 3L, 
2L, 2L, 2L, 5L, 5L, 5L, 7L, 7L, 7L, 8L, 8L, 8L, 6L, 6L, 6L, 4L, 
4L, 4L), .Label = c("Biomass", "Coal", "Fossil gas", "Offshore", 
"Oil", "Onshore", "PV", "Waste"), class = "factor"), Values = c(0.13, 
0.0646, 0.182, 0.0965, 0.00309, 0, 0.218, 0, 0, 0.00696, 0, 0, 
0.0328, 0.0245, 0.0266, 0.042, 0, 0, 0.323, 0.311, 0.337, 0.15, 
0.597, 0.454), Scenarios = structure(c(2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L), .Label = c("Biomass Scenario", "Today Scenario", "Wind Scenario"
), class = "factor")), class = "data.frame", row.names = c(NA, 
-24L))

Sorting your scenario data by factor: 按因素对方案数据进行排序:

df$Scenarios <- factor(df$Scenarios,levels = c("Today Scenario",
                                           "Wind Scenario",
                                           "Biomass Scenario"))

Once arranged correctly, you can now output the following stacked bar graph in ggplot2: 正确安排后,您现在可以在ggplot2中输出以下堆叠的条形图:

library(ggplot2)
ggplot(df, aes(fill=Technology, y=Values, x=Scenarios)) + 
   geom_bar( stat="identity")

在此处输入图片说明

Switch the x and y and close the bracket of the ggplot() : 切换xy并关闭ggplot()的括号:

ggplot(Distribution, 
       aes(x = Technology, y = WindScenario)) + 
  geom_bar(stat = "identity")

if you want the bars on the vertical axis, you can add coord_flip() to the plot 如果要在垂直轴上显示条形图,可以将coord_flip()添加到绘图中

ggplot(my_df, 
       aes(x = Technology, y = WindScenario)) + 
  geom_bar(stat = "identity", position = "stack")

To achieve this, you need to restructure your data first, to have three columns: Technology , Scenario and Percentage . 为此,您需要首先重组数据,使其具有三列: TechnologyScenarioPercentage Something like: 就像是:

Technology   Scenario         Percentage     
Biomass      TodayScenario    0.130 
Fossil gas   TodayScenario    0.0965     
Biomass      WindScenario     0.0646
Fossil gas   WindScenario     0.00309
Biomass      BiomassScenario  0.182
Fossil gas   BiomassScenario  0

You can use tidyr::gather() for that. 您可以使用tidyr::gather()

library(tidyr)

my_df %>%
  gather(Scenario, Percentage, c(TodayScenario, WindScenario, BiomassScenario)) %>%
  ggplot(aes(Scenario, Percentage, fill = Technology)) +
  geom_bar(stat = 'identity')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM