Question:
I am trying to plot a demand curve based on quantity and cost data. The problem is that when plotting the data, I get an upward slopping curve (Figure 1); whereas I am interested in a downward slopping curve to reflect demand behavior. I kind of manage to generate the plot by having negative values for the x-axis (Figure 2), however that is not optimal since I want to plot on the same plot a supply curve.
Data:
Code:
# Load Data
df = tbl_df(read_excel("BeWhere_BiomassDemand.xlsx", sheet = "PlotDat", col_names = TRUE))
# Add variables
df.fin = ddply(df, .(Operation,Feedstock), transform, wSupply = cumsum(Supply), wDemand = cumsum(Demand))
df.fin = mutate(df.fin, wmSupply = wSupply - Supply,
wtSupply = wmSupply + (wSupply - wmSupply)/2,
wmDemand = wDemand - Demand,
wtDemand = wmDemand + (wDemand - wmDemand)/2)
pDem = ggplot(df.fin, aes(ymin = 0))
pDem = pDem + geom_rect(aes(xmin = -wmDemand, xmax = -wDemand, ymax = Cost, fill = Feedstock), alpha = 0.35)
pDem = pDem + geom_line(aes(x = -wDemand, y = Cost, group = Feedstock, color = Feedstock), size = 1)
pDem = pDem + scale_fill_brewer(palette = "PuOr")
pDem = pDem + scale_color_brewer(palette = "PuOr")
pDem = pDem + labs(title = "Forest-based feedstock demand curves in Sweden",
x = "Demand (in TWh/year)",
y = "Cost (in MEUR/year)")
pDem = pDem + facet_grid(. ~ Operation, scales = "free") + theme_bw()
pDem = pDem + theme(plot.title = element_text(size = rel(2), face = "bold", hjust = 0.5, vjust = 1),
axis.title.x = element_text(size = rel(1.6), hjust = 0.5, vjust = 0),
axis.title.y = element_text(size = rel(1.6), hjust = 0.5, vjust = 1),
axis.text.x = element_text(size = rel(1.85), hjust = 0.5, vjust = 0.5),
axis.text.y = element_text(size = rel(1.85), hjust = 0.5, vjust = 0.5),
legend.title = element_text(size = rel(1.5)),
legend.text = element_text(size = rel(1.5)),
strip.text = element_text(size = rel(1.5), face = "bold"))
pDem = pDem + scale_y_continuous(breaks = seq(0,40,5))
pDem