在ggplot2中绘制需求曲线?

时间:2015-11-10 12:09:35

标签: r ggplot2

问题:

我正在尝试根据数量和成本数据绘制需求曲线。问题在于,在绘制数据时,我得到一条向上倾斜的曲线(图1);而我感兴趣的是向下倾斜曲线以反映需求行为。我有点设法通过给x轴的负值生成图(图2),但这不是最优的,因为我想在同一图上绘制供给曲线。

数据:

Data

代码:

    # 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

图1:Figure 1 图2:Figure 2

0 个答案:

没有答案