【问题标题】:Colorize labels for a group of points为一组点着色标签
【发布时间】:2020-03-19 03:12:49
【问题描述】:

是否可以在 ggplot2 中为一组点的标签着色?

除了图中显示的红色标记之外,我还想为下图中的一些左侧文本标签着色以显示红色的摆动状态:

The code (with data) is here. -- 编辑以反映答案

情节远非完美,因此非常欢迎提出其他建议。如果有人感兴趣,那里有far better graphs(但我不够好,无法编写代码)。

【问题讨论】:

  • 在您为标签着色的链接中,您可以使用蓝色和红色等颜色矢量,而不是仅使用“红色”,按照标签在轴中出现的顺序为标签着色
  • @camille 是的,它看起来像下面的答案。

标签: r ggplot2


【解决方案1】:

标签(轴文本)的颜色由函数theme() 中的参数element_text= 设置。您可以为每个标签设置不同的颜色。因为有一个列Swing 有层次,它可以用来设置颜色。

dw_plot + theme(axis.text.y = element_text(colour = ifelse(dw_data$Swing=="Swing State","red","grey")))

【讨论】:

  • 太棒了!非常感谢。 documentation 应该提到这一点。祝你有美好的一天。
【解决方案2】:

上述答案从今天开始有效;但是,它们会发出警告,不鼓励在element_text() 内使用ifelse 的建议方法。 element_text() 中的矢量化未在 ggplot2 中记录的原因是因为它不受支持的功能(由于机会而起作用)。请参阅讨论此特定问题的 GitHub 上的以下issue

上面提供的答案将导致以下警告:

# Warning message:
# Vectorized input to `element_text()` is not officially supported.
# Results may be unexpected or may change in future versions of ggplot2. 

以下代码说明了这一点(使用 SlowLearner 提供的稍微更新的示例 - 原始数据不可用)并显示了我的解决方案,该解决方案支持使用 ggtext 包和 element_markdown() 进行矢量化。

library(ggplot2); packageVersion("ggplot2")
# ‘3.3.0’
library(ggtext); packageVersion("ggtext") #; install.packages("ggtext") # https://github.com/wilkelab/ggtext
# ‘0.1.0’

set.seed(1234)
df <- data.frame(state = paste("State_", LETTERS, sep = ""), 
                margin = runif(26, -50, 50), 
                swing = rep(c("no", "yes", "no"), times = c(10, 6, 10)))

mycolours <- c("yes" = "red", "no" = "black")

ggplot(data = df, aes(x = margin, y = state)) +
     geom_point(size = 5, aes(colour = swing)) +
     scale_color_manual("Swing", values = mycolours) +
     theme(
          # The following line uses vectorisation (such as rep or ifelse).
          # This is not officially supported. Works by a chance and gives impression of a feature.
          axis.text.y = element_text(colour = rep(c("black", "red", "black"), times = c(10, 6, 10)))
          )

# Throws the following warning:
# Warning message:
# Vectorized input to `element_text()` is not officially supported.
# Results may be unexpected or may change in future versions of ggplot2. 

# The following code uses ggtext method # element_markdown(),
# This is how this question should be solved because the vectorisation method may not work in the future inside element_text().
ggplot(data = df, aes(x = margin, y = state)) +
     geom_point(size = 5, aes(colour = swing)) +
     scale_color_manual("Swing", values = mycolours) +
     theme(axis.text.y = element_markdown(colour = rep(c("black", "red", "black"), times = c(10, 6, 10))))
# No warning occurs. This will also correctly calculate other aesthetic such as size.

【讨论】:

    【解决方案3】:

    其他答案已经被接受,但只是作为一个直观的例子......对于更复杂的方案,您可以简单地在数据框中添加一个具有所需颜色的列并引用它,而不是按照每个使用“红色”和“黑色”下面。

    library(ggplot2)
    
    set.seed(1234)
    x <- data.frame(state = paste("State_", LETTERS, sep = ""), 
       margin = runif(26, -50, 50), swing = rep("no", 26))
    x[c(10:15), 'swing'] <- "yes"
    mycolours <- c("yes" = 'red', "no" = "black")
    
    ggplot(data = x, aes(x = margin, y = state)) +
        geom_point(size = 5, aes(colour = swing)) +
        scale_color_manual("Swing", values = mycolours) +
        theme(axis.text.y = element_text(colour = 
            ifelse(x$swing == 'yes', 'red', 'black'))) +
        theme()
    

    【讨论】:

    • 您的方法可能比我的代码更简单,所以谢谢 - 再次感谢您对 Cross Validated 的理解。但是,由于 2008/2012/swing 三个元素,我的数据有点复杂。希望以后能简化代码。
    • @Fr.是的,您的数据(和图表)更复杂,而这只是一个最小的可重现示例。我查看了您的原始代码,它似乎可以使用,因此我认为您可以将它们修补在一起。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 2020-10-15
    • 2020-05-30
    相关资源
    最近更新 更多