微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在R中更改ggrepel的背景?

如何解决如何在R中更改ggrepel的背景?

我在数据可视化方面遇到问题。

我的代码如下:

t <- structure(list(occurrences_article = c(4,11,6,5,4,7,8,2,3,10,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.65,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,2L
                    ),.Label = c("Low","Medium","High"),class = "factor"),treatable_behavioral_intervention = c(1,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))) 失败,也是为了使黄色标签可读.. 你能帮我吗?

解决方法

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

避免在 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,3,10,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.65,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,2L
                    ),.Label = c("Low","Medium","High"),class = "factor"),treatable_behavioral_intervention = c(1,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,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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?