【问题标题】:partially bold ggplot2 guide labels based on condition基于条件的部分粗体ggplot2指南标签
【发布时间】:2018-08-02 03:24:00
【问题描述】:

我有一些我想绘制的整洁数据,将两列组合起来 分组。

在图例中,我想打印其中一列中的条目之一 大胆的脸。

这是一个最小的工作示例:

library(ggplot2)

plot.data <- data.frame(x=1:10, y=1:10,
                        class1=rep(letters[1:2], 10),
                        class2=rep(LETTERS[1:2], each=5))

# This class should be displayed in bold
# Might be subject to change; consider it a function parameter
class2.bold <- "A"

# This is the default
# Note that the used classes are automagically determined from the data
ggplot(plot.data, aes(x, y)) +
  geom_point(aes(color=paste(class1, class2, sep="/"))) +
  scale_color_discrete("class")

# This is what I want
# Note that it only works because I know the data used in this example
ggplot(plot.data, aes(x, y)) +
  geom_point(aes(color=paste(class1, class2, sep=" / "))) +
  scale_color_discrete("class",
                       label=c(expression(a / bold(A)),
                               expression(a / B),
                               expression(b / bold(A)),
                               expression(b / B)))

如何利用class2.boldplot.data 获得第二个情节 仅(即在编写代码时没有进一步了解其内容)?

例如,当我将class2.bold 更改为"B" 并将更多数据行添加到 plot.data 具有附加 class1class2 值/组合,我 仍然希望 plot 命令无需手动调整即可工作。

【问题讨论】:

    标签: r ggplot2 tidyr


    【解决方案1】:

    这对我有用。可能有更快的方法,但这可以实现。

    您的代码:

      plot.data <- data.frame(x=1:10, y=1:10,
                                class1=rep(letters[1:2], 10),
                                class2=rep(LETTERS[1:2], each=5))
        
        # This class should be displayed in bold
        # Might be subject to change; consider it a function parameter
        class2.bold <- "A"
        
        # This is the default
        # Note that the used classes are automagically determined from the data
        ggplot(plot.data, aes(x, y)) +
          geom_point(aes(color=paste(class1, class2, sep="/"))) +
          scale_color_discrete("class")
    

    对于粗体标签:

     #get unique labels
        legend_labs <- unique(paste(plot.data$class1, plot.data$class2, sep="/"))
        
        #order them in alphabetical order
        legend_labs <- legend_labs[order(legend_labs)]
        
        #Bold the correct letters
        legend_labs <- sub(pattern = class2.bold,
            replacement = paste0("bold(",class2.bold,")"),
            legend_labs)
        
        #make them expressions
        legend_labs <- lapply(parse(text = legend_labs), as.expression)
        
        #plot
        ggplot(plot.data, aes(x, y)) +
          geom_point(aes(color=paste(class1, class2, sep=" / "))) +
          scale_color_discrete("class",
                               label=legend_labs)
    

    编辑:

    如果你想加粗多个字母,请改用第三步

    #Bold the correct letters
    for (i in 1:length(class2.bold)) {
    legend_labs <- sub(pattern = class2.bold[i],
        replacement = paste0("bold(",class2.bold[i],")"),
        legend_labs)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 2020-02-18
      相关资源
      最近更新 更多