【问题标题】:How to change background in ggrepel in R?如何在 R 中更改 ggrepel 的背景?
【发布时间】:2021-04-30 09:52:36
【问题描述】:

我在数据可视化方面遇到了麻烦。

我的代码如下所示:

t <- structure(list(occurrences_article = c(4, 11, 6, 5, 4, 7, 8, 2, 4, 4, 11, 3, 6, 10, 2, 1, 2),
                    score = c(2, 1.76, 0.9, 1.875, 1.93, 1.92, 1.42, 1.5, 1.6, 1.75, 1.29, 1.5, 2, 1.65, 2, 2, 2
                    ), 
                    Barrier_code = c("information", "beliefs", "trust", "lack_traditional_motivations", "perceived_quality", "proximity", "shortage", "access_criteria", "functioning", "perceived_accessibility", "fees", "perceived_affordability", "lack_of_subsidies", "cultural_ressources", "social_ressources", 
                                     "sustainability", "satisfaction"), 
                    Quality_coded = structure(c(1L, 3L, 1L, 2L, 3L, 2L, 3L, 1L, 3L, 2L, 2L, 2L, 3L, 1L, 1L, 3L, 2L
                    ), .Label = c("Low", "Medium", "High"), class = "factor"), treatable_behavioral_intervention = c(1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1)), row.names = c(NA, -17L), class = c("tbl_df", "tbl", "data.frame"))

library(ggplot2)
library(tidyverse)
library(ggrepel)
library(RColorBrewer)

t$Barrier_code <- as.factor(t$Barrier_code)
t$score <- as.numeric(t$score)
t$Quality <- as.numeric(t$Quality)
t$Quality_coded <- as.factor(t$Quality_coded)

ggplot(t, aes(occurrences_article, score, label = Barrier_code, colour = Quality_coded))+
  geom_point(size =2)+
  geom_label_repel(aes(label = Barrier_code, fill = levels(t$treatable_behavioral_intervention), color = "black"))+ 
  scale_color_brewer(palette = "RdYlGn")


但是生成的图不满意see

问题是我想用两种不同的颜色更改标签的背景,这取决于treatable_behavioral_intervention 的两个级别(+ scale_fill_manual(values = setNames(c("lightblue","darkgreen"), levels(t$treatable_behavioral_intervention))) 的尝试失败,也是为了使黄色标签可读.. 你能帮帮我吗?

【问题讨论】:

    标签: r ggplot2 data-visualization ggrepel


    【解决方案1】:

    我想这更像是一个错字 - 你非常正确。您的代码没有按照发布的方式运行(您的数据存在很大差距,但没关系)。

    在 aes 中避免使用$,我不太清楚你为什么使用levels...

    不要!只需使用as.factor(variable)。或者也可以as.character

    library(ggrepel)
    #> Loading required package: ggplot2
    
    t <- structure(list(occurrences_article = c(4, 11, 6, 5, 4, 7, 8, 2, 4, 4, 11, 3, 6, 10, 2, 1, 2),
                        score = c(2, 1.76, 0.9, 1.875, 1.93, 1.92, 1.42, 1.5, 1.6, 1.75, 1.29, 1.5, 2, 1.65, 2, 2, 2
                        ), 
                        Barrier_code = c("information", "beliefs", "trust", "lack_traditional_motivations", "perceived_quality", "proximity", "shortage", "access_criteria", "functioning", "perceived_accessibility", "fees", "perceived_affordability", "lack_of_subsidies", "cultural_ressources", "social_ressources", 
                                         "sustainability", "satisfaction"), 
                        Quality_coded = structure(c(1L, 3L, 1L, 2L, 3L, 2L, 3L, 1L, 3L, 2L, 2L, 2L, 3L, 1L, 1L, 3L, 2L
                        ), .Label = c("Low", "Medium", "High"), class = "factor"), treatable_behavioral_intervention = c(1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1)), row.names = c(NA, -17L), class = c("tbl_df", "tbl", "data.frame"))
    
    
    t$Barrier_code <- as.factor(t$Barrier_code)
    t$score <- as.numeric(t$score)
    t$Quality_coded <- as.factor(t$Quality_coded)
    
    ggplot(t, aes(occurrences_article, score, label = Barrier_code, colour = Quality_coded))+
      geom_point(size =2)+
      geom_label_repel(aes(label = Barrier_code, 
                           fill = as.factor(treatable_behavioral_intervention)# that's the trick
                           ), # changed bracket
                           color = "black")+ 
      scale_color_brewer(palette = "RdYlGn") +
      scale_fill_brewer()
    

    或者,手动定义颜色:

    ggplot(t, aes(occurrences_article, score, label = Barrier_code, colour = Quality_coded))+
      geom_point(size =2)+
      geom_label_repel(aes(label = Barrier_code, 
                           fill = as.factor(treatable_behavioral_intervention)# that's the trick
      ), # changed bracket
      color = "black")+ 
      scale_color_manual(values = c("Red", "Yellow", "Green")) +
      scale_fill_brewer()
    

    【讨论】:

    • 嗨,非常感谢,它有很大帮助,我想我的代码搞混了,然后以为我做错了什么......但是,我想要“高”、“中” ”和“低”点分别用“绿色”,“黄色”和“橙色”(或红色,我不介意)着色。我不明白为什么红色有一个“黑色”级别显示这些标签。我的意思是,它试图指定 color = "black" 以便黄色标签变得可读但是......
    • @LaudineCarbuccia 恰好发生在我们当中。完全不用担心
    • 还有一件事(很抱歉):我希望将“高”、“中”和“低”点分别涂成“绿色”、“黄色”和“橙色”(或红色) ,我不介意)。我不明白为什么这些标签会显示红色的“黑色”级别。我的意思是,它试图指定color = "black"以便黄色标签变得可读但是.. .
    • @LaudineCarbuccia 不确定我是否理解。在我的图表中,点是彩色的......?
    • @LaudineCarbuccia 您需要从 aes 中删除 color = black。使用 brewer RdYlGn 实际上有效。查看更新的代码
    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2018-01-04
    • 2014-03-21
    • 2021-06-30
    • 2013-06-10
    • 2014-04-09
    • 1970-01-01
    相关资源
    最近更新 更多