データリンク: 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 で最後の 2 つの列の下の x 軸にラベルを追加する方法はありますか。facet_wrap() で "free_y" を "free" に置き換えることでできることはわかっていますが、それではプロット内のすべてのパネルにラベルが追加されてしまい、目に優しくありません。
私が探しているのは、残りの 2 つの列のすぐ下にラベルを追加するか、2 セットの x 軸ラベルを追加しますが、「wht」パネルのラベルと同じレベルに追加することです。
私の質問が明確だったことを願っています。
前もって感謝します。
PS:問題を解決するために、facet_wrap プロットに「フローティング」軸ラベルを追加するために、Julius によって作成された関数を使用しようとしました。コードは以下のとおりです。
#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) エラー: プロットにレイヤーがありません
何かご意見は?
再度、感謝します