可能重复:
在 facet_wrap 图中添加“浮动”轴标签
数据链接: https ://www.dropbox.com/s/q45t9hng4mryi6y/GTAP_CCShocks2.csv
代码:
# Loading the data
climshocks <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/GTAP-CGE/GTAP_NewAggDatabase/NewFiles/GTAP_CCShocks2.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)
# Data manipulations
ccshocks <- as.data.frame(climshocks)
ccshocks[4:4] <- sapply(ccshocks[4:4],as.numeric)
ccshocks <- droplevels(ccshocks)
ccshocks <- transform(ccshocks,region=factor(region,levels=unique(region)))
library(reshape)
library(ggplot2)
library(grid)
#--------------------------------------------------------------------------------
#### Average of climate-induced yield percent change for all regions by crop
#--------------------------------------------------------------------------------
#_Code_Begin...
Avgccshocks.f <- melt(ccshocks)
Avgccshocks.f <- Avgccshocks.f[Avgccshocks.f$sres %in% c("AVERAGE"), ]
PlotAvgccshocks <- ggplot(data = Avgccshocks.f, aes(factor(region), value))
PlotAvgccshocks + geom_bar(stat="identity") +
theme(axis.text.x = element_text(colour = 'black', angle = 90, size = 14, hjust = 0.5, vjust = 0.5),axis.title.x=element_blank()) +
ylab("Yield(%change)") + theme(axis.text.y = element_text(colour = 'black', size = 14, hjust = 0.5, vjust = 0.5), axis.title.y = element_text(size = 14, hjust = 0.5, vjust = 0.5, face = 'bold')) +
theme(strip.text.x = element_text(size = 16, hjust = 0.5, vjust = 0.5, face = 'bold')) +
facet_wrap(~crops, scales="free_y", ncol=3)
ggsave("PlotAvgccshocks.png")
#_Code_End...
结果:
我的问题: ggplot 中有没有办法将标签添加到最后两列下的 x 轴。我知道我可以通过在 facet_wrap() 中将“free_y”替换为“free”来做到这一点,但这会将标签添加到图中的所有面板中,这会使其不友好。
我正在寻找的是在剩下的两列正下方添加标签,或者添加两组 x 轴标签,但与“wht”面板的标签处于同一级别。
我希望我的问题很清楚。
提前致谢。
PS:我尝试使用 Julius 创建的函数来响应在 facet_wrap 图中添加“浮动”轴标签来解决问题,代码如下
#7- Function to add x-axis labels to all plots using facet_wrap()
#----------------------------------------------------------------
library(grid)
# pos - where to add new labels
# newpage, vp - see ?print.ggplot
facetAdjust <- function(x, pos = c("up", "down"),
newpage = is.null(vp), vp = NULL)
{
# part of print.ggplot
ggplot2:::set_last_plot(x)
if(newpage)
grid.newpage()
pos <- match.arg(pos)
p <- ggplot_build(x)
gtable <- ggplot_gtable(p)
# finding dimensions
dims <- apply(p$panel$layout[2:3], 2, max)
nrow <- dims[1]
ncol <- dims[2]
# number of panels in the plot
panels <- sum(grepl("panel", names(gtable$grobs)))
space <- ncol * nrow
# missing panels
n <- space - panels
# checking whether modifications are needed
if(panels != space){
# indices of panels to fix
idx <- (space - ncol - n + 1):(space - ncol)
# copying x-axis of the last existing panel to the chosen panels
# in the row above
gtable$grobs[paste0("axis_b",idx)] <- list(gtable$grobs[[paste0("axis_b",panels)]])
if(pos == "down"){
if pos == down then shifting labels down to the same level as
# the x-axis of last panel
rows <- grep(paste0("axis_b\\-[", idx[1], "-", idx[n], "]"),
gtable$layout$name)
lastAxis <- grep(paste0("axis_b\\-", panels), gtable$layout$name)
gtable$layout[rows, c("t","b")] <- gtable$layout[lastAxis, c("t")]}}
# again part of print.ggplot, plotting adjusted version
if(is.null(vp)){
grid.draw(gtable)}
else{
if (is.character(vp))
seekViewport(vp)
else pushViewport(vp)
grid.draw(gtable)
upViewport()}
invisible(p)}
所以,基本上我为“facetAdjust()”函数运行此代码并为我的绘图“PlotAvgccshocks”调用它,但弹出一条错误消息,如下所示:
错误信息:
facetAdjust(PlotAvgccshocks)错误:绘图中没有图层
有什么想法吗?
再次感谢