如何将x轴标签添加到具有奇数个面板的绘图中?

时间:2013-01-24 13:12:20

标签: r ggplot2 labels

  

可能重复:
  add “floating” axis labels in facet_wrap plot

数据链接: 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...

结果: Resulting plot

我的问题: ggplot中是否有一种方法可以将标签添加到最后两列的x轴上。我知道我可以通过在facet_wrap()中将“free_y”替换为“free”来实现,但这会将标签添加到绘图中的所有面板上,这会使它对眼睛不友好。

我要找的是在剩下的两列之下添加标签,或者添加两组x轴标签,但与“wht”面板的标签处于同一级别。

我希望我的问题很明确。

提前致谢。

P.S:我试图使用由Julius创建的函数来回复add “floating” axis labels in facet_wrap plot来解决问题,代码在下面

    #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”调用它,但会弹出一条错误消息,如下所示:

的ErrorMessage:

  

facetAdjust(PlotAvgccshocks)   错误:图中没有图层

有什么想法吗?

再次感谢

0 个答案:

没有答案