Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 为一组点的标签着色_R_Ggplot2 - Fatal编程技术网

R 为一组点的标签着色

R 为一组点的标签着色,r,ggplot2,R,Ggplot2,在ggplot2中,是否可以为一组点的标签着色? 除了图中显示的红色标记外,我还想给下图中的一些左侧文本标签上色,以红色显示摆动状态: --编辑以反映答案 我知道(),但这不是我需要的 理想情况下,我也想改进传奇,但我不确定具体如何改进 情节还远远不够完美,因此我们非常欢迎提出更多建议。如果有人感兴趣(但我没有足够好的能力来编写它们)。标签(轴文本)的颜色由函数theme()中的参数element\u text=设置。可以为每个标签设置不同的颜色。由于有一列带有级别的Swing,因此可以使

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

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

--编辑以反映答案

  • 我知道(),但这不是我需要的
  • 理想情况下,我也想改进传奇,但我不确定具体如何改进
情节还远远不够完美,因此我们非常欢迎提出更多建议。如果有人感兴趣(但我没有足够好的能力来编写它们)。

标签(轴文本)的颜色由函数
theme()
中的参数
element\u text=
设置。可以为每个标签设置不同的颜色。由于有一列带有级别的
Swing
,因此可以使用该列设置颜色

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

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

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()
库(ggplot2)
种子集(1234)

x上述答案截至今天仍有效;但是,它们会发出警告,不鼓励在
element\u text()中使用建议的方法。
ggplot2
中未记录
element_text()
中的矢量化,原因是该功能不受支持(可能会起作用)。请参阅GitHub上讨论此特定问题的以下内容

上述答案将导致以下警告:

# Warning message:
# Vectorized input to `element_text()` is not officially supported.
# Results may be unexpected or may change in future versions of ggplot2. 
下面的代码说明了这一点(使用SlowLearner提供的稍微更新的示例-原始数据不可用),并显示了我的解决方案,该解决方案支持使用包和
元素标记()进行矢量化

库(ggplot2);包装版本(“ggplot2”)
# ‘3.3.0’
图书馆(ggtext);包装版本(“ggtext”)#;安装程序包(“ggtext”)#https://github.com/wilkelab/ggtext
# ‘0.1.0’
种子集(1234)

df在您的标签着色链接中,您可以使用蓝色和红色等颜色向量,而不只是使用“红色”,按照标签在您的页面中出现的顺序着色标签!非常感谢你。政府应该提到这一点。祝您有愉快的一天。您的方法可能比我的代码更简单,所以谢谢您——再次感谢您在Cross Validated上的理解。然而,由于2008/2012/swing三要素,我的数据有点复杂。我希望将来能简化代码。@Fr。是的,您的数据(和图表)更复杂,而这只是一个最小的可复制示例。我查看了您的原始代码,它似乎是可用的,所以我认为您可以将这些代码修补在一起。
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.