【发布时间】:2018-11-13 10:37:22
【问题描述】:
您可以在下面找到我的代码及其产生的情节。我想在上面添加横向字幕“Scenario1”和“Scenario2”,而不是与“Sub-Scenario1”和“Sub-Scenario2”一起添加。更多解释请参见图。
myd<- data.frame( var1 = rep(c("Scenario1: Sub-Scenario 1","Scenario1: Sub-Scenario 2","Scenario2: Sub-Scenario 1","Scenario2: Sub-Scenario 2"),each=2),
samp = rep(c("Outcome 1","Outcome 2"),4),
V4=rep(NA,8),V3 = rep(NA,8), V2 = rep(NA,8), V1 = rep(NA,8) )
myd[myd$samp=="Outcome 1","V1"]=runif(4,100,200)
myd[myd$samp=="Outcome 1","V2"]=runif(4,100,200)
myd[myd$samp=="Outcome 1","V3"]=runif(4,100,200)
myd[myd$samp=="Outcome 1","V4"]=runif(4,100,200)
myd[myd$samp=="Outcome 2","V1"]=runif(4,100,200)
myd[myd$samp=="Outcome 2","V2"]=runif(4,100,200)
myd[myd$samp=="Outcome 2","V3"]=runif(4,100,200)
myd[myd$samp=="Outcome 2","V4"]=runif(4,100,200)
library(reshape2)
library(ggplot2)
meltd<- melt(myd, id.vars=1:2)
par(mfrow = c(1,1),lwd=2,oma=c(0,0.1,0,0))
m <- matrix(c(1,2),nrow = 2,ncol = 1,byrow = TRUE)
layout(mat = m,heights = c(0.85,0.15))
plot1<-ggplot(meltd, aes(x = var1, y = value, fill = variable, label = paste0(round(value, 1), "%"))) +
geom_bar(stat = "identity", position = position_dodge(width = 0.8), width = 0.6) +
facet_grid(samp ~ ., switch = "y", scales = "free_y", space = "free",labeller=label_wrap_gen(width=25, multi_line=T)) +
coord_flip() +
scale_fill_manual("legend",values = c("V4"="violet","V3" = "orange", "V2" = "red", "V1" = "blue", "Baseline" = "black")) +
geom_text(aes(y = value + 20 * sign(value)), position = position_dodge(width = 0.8), size=4)+
theme_bw() +
theme(
legend.position = "none",
strip.placement = "outside",
#axis.title.x = element_blank(),
axis.title.y = element_blank(),
# added face and size arguments
axis.text.y = element_text(colour="black", size=12),
axis.text.x= element_text(size=13),
strip.text.y = element_text(size = 10, colour = "black", face='bold'),
plot.margin = margin(0.1, 0.1, 2, 0.1, "cm"))+
ylab("Relative change in 2016 (in %)")
plot1
【问题讨论】: