【问题标题】:Justify individual axis labels in bold using ggplot2使用 ggplot2 以粗体显示单个轴标签
【发布时间】:2018-01-16 16:49:51
【问题描述】:

问题改编自这个问题和解决方案:Highlighting individual axis labels in bold using ggplot2

我想根据满足标准有选择地证明水平轴标签的合理性。所以借用上面的问答我已经建立了一个例子:

require(ggplot2)
require(dplyr)
set.seed(36)
xx<-data.frame(YEAR=rep(c("X", "Y"), each=20),
           CLONE=rep(c("A", "B", "C", "D", "E"), each=4, 2),
           TREAT=rep(c("T1", "T2", "T3", "C"), 10),
           VALUE=sample(c(1:10), 40, replace=T))

# Simple plot with factors on y axis
ggplot(xx, aes(x = VALUE, y=CLONE, fill=YEAR)) + 
    geom_bar(stat="identity", position="dodge") +
    facet_wrap(~TREAT)

好的,所以我采用上述问题+答案中的函数来生成理由向量:

# Modify to control justification
colorado2 <- function(src, boulder) {
    if (!is.factor(src)) src <- factor(src)                   
    src_levels <- levels(src)                                 
    brave <- boulder %in% src_levels                         
    if (all(brave)) {                                         
        b_pos <- purrr::map_int(boulder, ~which(.==src_levels)) 
        b_vec <- rep(0.2, length(src_levels))               
        b_vec[b_pos] <- 0.9                                 
        b_vec                                                  
    } else {
        stop("All elements of 'boulder' must be in src")
    }
}

# Redraw the plot with modifcation
ggplot(xx, aes(x = VALUE, y=CLONE, fill=YEAR)) + 
    geom_bar(stat="identity", position="dodge") +
    facet_wrap(~TREAT) +
    theme(axis.text.y=element_text(hjust=colorado2(xx$CLONE, c("A", "B", "E"))))

我得到了这个不幸的混乱:

标签在我想要的方向上是合理的 - 但由于我无法弄清楚的原因占据了太多的情节。我该如何解决这个问题?

【问题讨论】:

    标签: r ggplot2 labels axes


    【解决方案1】:

    我做了一些挖掘工作。问题在于 ggplot 如何设置 y 轴 grob 的 grob 宽度。它假定hjust 在所有标签中都是相同的。我们可以通过修改 grob 树来解决这个问题。以下代码是用 ggplot2 的开发版本测试的,可能无法像当前发布的版本那样工作。

    首先,一个简单的可重现示例:

    p <- ggplot(mpg, aes(manufacturer, hwy)) + geom_boxplot() + coord_flip() + 
      theme(axis.text.y = element_text(hjust = c(rep(1, 10), rep(0, 5))))
    p # doesn't work
    

    问题在于轴 grob 的 grob 宽度设置为整个绘图区域。但是我们可以手动进去固定宽度。不幸的是,我们必须在多个位置修复它:

    # get a vector of the y labels as strings
    ylabels <- as.character(unique(mpg$manufacturer))
    
    library(grid)
    g <- ggplotGrob(p)
    
    # we need to fix the grob widths at various locations in the grob tree
    g$grobs[[3]]$children[[2]]$widths[1] <- max(stringWidth(ylabels))
    g$grobs[[3]]$width <- sum(grobWidth(g$grobs[[3]]$children[[1]]), grobWidth(g$grobs[[3]]$children[[2]]))
    g$widths[3] <- g$grobs[[3]]$width
    
    # draw the plot
    grid.newpage()
    grid.draw(g)
    

    ggplot2 的轴绘制代码可能可以修改为像我从一开始在这里所做的那样计算宽度,然后问题就会消失。

    【讨论】:

      【解决方案2】:

      您采用的方法是破解底层网格图形引擎,结果可能并不总是很明显。请参阅 here 以获得进入 grob 树并修复问题的答案。

      但是,对于问题中所述的问题,有一个不需要任何黑客攻击的简单解决方案。只需制作带有一些尾随空格的标签。我在这里手动完成了这项工作,但您也可以编写一个函数来执行此操作。

      ggplot(xx, aes(x = VALUE, y=CLONE, fill=YEAR)) + 
        geom_bar(stat="identity", position="dodge") +
        scale_y_discrete(breaks = c("A", "B", "C", "D", "E"),
                         labels = c("A    ", "B    ", "C", "D", "E    ")) +
        facet_wrap(~TREAT)
      

      【讨论】:

      • 呃没有意识到这个解决方案正在破解任何东西!因此,我在您的解决方案中看到的问题是对齐。在我的真实数据中,它不是字母,而是不同长度的单词。假设单词是:c("beer", "heineken", "guinness", "wine", "merlot", "chardonnay", "whiskey", "middelton","jameson"),我想要啤酒、葡萄酒和威士忌左对齐,其他右对齐 - 他们需要左对齐或右对齐
      • 我发布了第二个答案来解决这个问题。
      猜你喜欢
      • 1970-01-01
      • 2022-11-25
      • 2019-07-28
      • 1970-01-01
      • 2021-03-19
      • 2021-07-23
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多