0

I would like to color the background of my ggplot based on the grouping factor of Ranking (strong, moderate, weak). I have used geom_mark_rect() but it would be great if there are any other ways. I assume another option is to use the importance values (< or >) to color the background, rather than using geom_mark_rect().

I looked at the following answers: Make the background of a graph different colours in different regions

dput(data)

structure(list(Category = c("Place", "Place", "Place", "Place", 
"Place", "Place", "Place", "Place", "Place", "Place", "Place", 
"Place", "Place", "Place", "Place", "Place"), Ranking = 
c("Strong", 
"Strong", "Strong", "Strong", "Moderate", "Moderate", 
"Moderate", 
"Moderate", "Moderate", "Weak", "Weak", "Weak", "Weak", "Weak", 
"Weak", "Weak"), Features = c("F2_Pre", "F3_Pre", "F1_Pre", 
"F2_Fol", 
"f0_Pre", "f0_Fol", "CPP_Pre", "F1_Fol", "SD", "Release", 
"F3_Fol", 
"CPP_Fol", "CoG", "H1*-H2*_Pre", "H1*-H2*_Fol", "Closure"), 
Importance = c(24, 
12, 11, 11, 8, 7, 6, 6, 5, 3, 2, 2, 1, 1, 1, 0)), class = 
c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -16L), spec = 
structure(list(cols = list(Category = structure(list(), class = 
c("collector_character", "collector")), Ranking = 
structure(list(), class = c("collector_character", 
"collector")), Features = structure(list(), class = 
c("collector_character", 
"collector")), Importance = structure(list(), class = 
c("collector_double", 
"collector"))), default = structure(list(), class = 
c("collector_guess", 
"collector")), skip = 1L), class = "col_spec"))

Here is the code:

library(ggplot2)

library(tidyverse)

library(ggforce)

ggplot(data, aes(x = reorder(Features, -Importance), y = Importance, fill = Importance)) +
geom_bar(stat = "identity", width=0.9, position = position_dodge(width=0.1)) +
geom_mark_rect(aes(fill = Importance, group = "fill", label=Ranking,  filter = Ranking == c("Strong")), 
             label.fontsize = 8,  label.buffer = unit(10, 'mm'), label.hjust = 8,  show.legend = FALSE)+
geom_mark_rect(aes(fill = Importance, group = "fill",  label=Ranking, filter = Ranking == c("Moderate")), 
             label.fontsize = 8, label.buffer = unit(10, 'mm'), label.hjust = 8,  show.legend = FALSE)+
geom_mark_rect(aes(fill = Importance, group = "fill",  label=Ranking, filter = Ranking == c("Weak")), 
             label.fontsize = 8, label.buffer = unit(10, 'mm'), label.hjust = 8,  show.legend = FALSE)+
xlab("Acoustic Features")+
ylab("Importance")+
coord_cartesian(ylim=c(0,40)) +
theme_bw() +
theme(axis.title.y = element_text(size = rel(1.0), color="black", vjust = 0.4, family= "Arial"))+
theme(axis.title.x = element_text(size = rel(1.0), color="black", vjust = 0.4, family= "Arial"))+
theme(axis.text.y = element_text(size = rel(1.0), color="black", vjust = 0.4, family= "Arial"))+
theme(axis.text.x = element_text(size = rel(1.0),color="black", vjust = 0.6, angle=45, family= "Arial"))

PLOT:

Plot created with the above code

candle786
  • 41
  • 7

0 Answers0