0
votes

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")
4

4 Answers

1
votes

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:

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

enter image description here

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

Switch the x and y and close the bracket of the 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

0
votes

To achieve this, you need to restructure your data first, to have three columns: Technology, Scenario and Percentage. 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.

library(tidyr)

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