我是R的新手,我有两个看起来像下面的图,有不同的,离散的和非数字的x轴。
以下是我试图绘制的数据:
Technology Timescale Scenario Contribution Value
PHS Short Pessimistic electricity 164
PHS Short Pessimistic facility and materials 303
PHS Short Optimistic electricity 135
PHS Short Optimistic facility and materials 205
ICAES Short Pessimistic electricity 164
ICAES Short Pessimistic facility and materials 180
ICAES Short Optimistic electricity 144
ICAES Short Optimistic facility and materials 120
P2H2P Short Pessimistic electricity 589
P2H2P Short Pessimistic facility and materials 83
P2H2P Short Optimistic electricity 321
P2H2P Short Optimistic facility and materials 63
NCA_LTO Battery Short Pessimistic electricity 144
NCA_LTO Battery Short Pessimistic facility and materials 385
NCA_LTO Battery Short Optimistic electricity 128
NCA_LTO Battery Short Optimistic facility and materials 250
PHS Medium Pessimistic electricity 164
PHS Medium Pessimistic facility and materials 17
PHS Medium Optimistic electricity 135
PHS Medium Optimistic facility and materials 12
ICAES Medium Pessimistic electricity 164
ICAES Medium Pessimistic facility and materials 9
ICAES Medium Optimistic electricity 144
ICAES Medium Optimistic facility and materials 6
P2H2P Medium Pessimistic electricity 589
P2H2P Medium Pessimistic facility and materials 23
P2H2P Medium Optimistic electricity 321
P2H2P Medium Optimistic facility and materials 23
NCA_LTO Battery Medium Pessimistic electricity 144
NCA_LTO Battery Medium Pessimistic facility and materials 396
NCA_LTO Battery Medium Optimistic electricity 128
NCA_LTO Battery Medium Optimistic facility and materials 278
PHS Long Pessimistic electricity 164
PHS Long Pessimistic facility and materials 13
PHS Long Optimistic electricity 135
PHS Long Optimistic facility and materials 9
P2H2P Long Pessimistic electricity 657
P2H2P Long Pessimistic facility and materials 80
P2H2P Long Optimistic electricity 432
P2H2P Long Optimistic facility and materials 70
以下是我的代码:
library("xlsx")
library("ggplot2")
library ("data.table")
library("grid")
library("gtable")
library("gridExtra")
##Empty memory and set working directory
rm(list=ls())
setwd("C:/Users/zhang_x/Documents/R/codes")
##Prepare data for stackbar chart
SmEffLt <- read.xlsx("Visualization.xlsx", sheetIndex=2, startRow=2, header=TRUE, colIndex=NULL,colClasses="character")
SmEffLt$Value <- as.numeric(as.character(SmEffLt$Value))
SmEffLt$Timescale <- factor(SmEffLt$Timescale, levels = c("Short", "Medium", "Long"))
SmEffLt$Technology <- factor(SmEffLt$Technology, levels = c('PHS','ICAES','P2H2P','NCA_LTO Battery'))
SmEffLt$Scenario <- factor(SmEffLt$Scenario, levels = c("Optimistic", "Pessimistic"))
SmEffLt <- data.frame(SmEffLt)
##Prepare data for range bar chart
##Transform Scenrio column into two columns: Pessimistic and Optimistic
##Sum up contribution from electricity and contribution from facility and materials by matching the fields
SmEffLtPessimistic <- subset(SmEffLt,Scenario=="Pessimistic")
SmEffLtPessimistic$Scenario <- NULL
SmEffLtPessimistic <- merge(subset(SmEffLtPessimistic, Contribution=="electricity"),subset(SmEffLtPessimistic, Contribution=="facility and materials"), by=c("Technology","Timescale"))
SmEffLtPessimistic$Contribution.x <- NULL
SmEffLtPessimistic$Contribution.y <- NULL
SmEffLtPessimistic$Value <- SmEffLtPessimistic$Value.x + SmEffLtPessimistic$Value.y
SmEffLtPessimistic$Value.x <- NULL
SmEffLtPessimistic$Value.y <- NULL
SmEffLtOptimistic <- subset(SmEffLt,Scenario=="Optimistic")
SmEffLtOptimistic$Scenario <- NULL
SmEffLtOptimistic <- merge(subset(SmEffLtOptimistic, Contribution=="electricity"),subset(SmEffLtOptimistic, Contribution=="facility and materials"), by=c("Technology","Timescale"))
SmEffLtOptimistic$Contribution.x <- NULL
SmEffLtOptimistic$Contribution.y <- NULL
SmEffLtOptimistic$Value <- SmEffLtOptimistic$Value.x + SmEffLtOptimistic$Value.y
SmEffLtOptimistic$Value.x <- NULL
SmEffLtOptimistic$Value.y <- NULL
SmEffLtRange <- merge(SmEffLtOptimistic, SmEffLtPessimistic, by=c("Technology","Timescale"))
##Plot stackbar chart
p1 <- ggplot(SmEffLt,aes(x=interaction(Scenario, Technology),y=Value, fill=Contribution))+
geom_bar(stat="identity", colour="white")+
scale_fill_manual(values=c('gold','cornsilk3'))+
facet_grid(.~Timescale, scale="free", space = "free")+
ylab("Life Cycle GHG Emissions (g/kWh)")+
xlab("")+
scale_y_continuous(expand = c(0, 0), limits=c(0,800), breaks=seq(0, 800, by=100))+
theme(axis.text.x=element_text(size=9, angle=90, hjust=1,vjust=0.5),
axis.text.y=element_text(size=9),
axis.title.y = element_text(size=8),
legend.title = element_text(size=9))+
ggtitle("Variation of Efficiency and Lifetime (1 MW)")+
theme(plot.title = element_text(size=10, color="#666666", face="bold", hjust=0.5))
##Plot range bar chart
p2 <- ggplot(SmEffLtRange,aes(x=Technology, ymin=Value.x, ymax=Value.y))+
facet_grid(.~Timescale, scale="free", space = "free")+
scale_y_continuous(limits=c(0,800), expand = c(0, 0), breaks=seq(0, 800, by=100))+
geom_errorbar(width=0.3,lwd=0.7)
我的问题是:
在图1(p1)中,我如何在x轴的底部有PHS,ICAES等,并且每两个堆叠的条形图共享一种技术?
您知道如何覆盖它们吗?例如,图2(p2)中的第一个条形线显示在图1中的第一个两个堆积条之间?