I have trouble with datavisualisation.
My code looks like this:
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")
But the produced graph is unsatisfying see
The thing is I would like to change the background of the label with two different color, depending on the two levels of treatable_behavioral_intervention
(failed attempt with + scale_fill_manual(values = setNames(c("lightblue","darkgreen"), levels(t$treatable_behavioral_intervention)))
, also in order to make yellow labels readable..
Could-you help me please?
I guess it's more a typo- you were very much on the right track. Your code doesn't run as posted (there is a large gap in your data, but that's fine).
Avoid $
in aes, and I am not quite sure why you're using levels
...
Don't! Just use as.factor(variable)
. Or also possible 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()
Or, manually define the colors:
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()