为一组点着色标签

时间:2013-01-13 14:22:24

标签: r ggplot2

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

我想在下面的图中为一些左侧文字标签着色,以显示红色的摇摆状态,以及图表中显示的红色标记:

electoral dot plot

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

情节远非完美,因此非常欢迎其他建议。如果有人感兴趣的话,那里有far better graphs(但我还不够好对它们进行编码)。

3 个答案:

答案 0 :(得分:5)

标签颜色(轴文本)由函数element_text=中的参数theme()设置。您可以为每个标签设置不同的颜色。由于存在具有级别的列Swing,因此可以使用它来设置颜色。

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

答案 1 :(得分: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()

screenshot

答案 2 :(得分:2)

上述答案从今天起生效;但是,它们会发出警告,不建议在ifelse内使用element_text()的建议方法。 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.