reverse color in geom_line() and geom_ribbon()

Dear all,

I am working on the following dataset (300 rows not sure why I am not able to upload it using dput())

Here it is .

It produces the following plot.

test

I'm wondering how I can reverse the colours so that fricative becomes green and nasal becomes orange-ish.

Here is my code so far.

sm %>% filter(manner!="stop", voicing!= "voiceless"|f_word_>6) %>% ggplot(aes(f_word_, est,group=smooth)) +
    geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci, fill=manner), alpha = 0.3) +
    geom_line(aes(colour = manner, linetype=voicing)) + 
    theme(legend.position="top", legend.direction="horizontal", legend.box="horizontal", axis.text.x = element_text(angle = 45, hjust = 1),             text=element_text(family="Times")) + xlab('Normalised time - coded for interval') + ylab('F0 estimated splines') +
  scale_x_continuous(breaks=c(1,6.1,10), labels=c("closure mid","vowel onset","vowel mid")) + geom_vline(xintercept=6.1) +
   guides(linetype = guide_legend(override.aes = list(colour='black')), manner = guide_legend(override.aes = list(colour='black'))) + theme_classic()+
   theme(legend.position="top") 
   

Thank you in advance!

I cannot access your data link because the permission is not set correctly. I invented a little data set as an alternative. The key to setting the colors is changing the order of the factor levels of manner. The default is to set the levels alphabetically. In the code below, I show the default and the effect of setting the levels manually.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
sm <- data.frame(f_word_ = rep(1:10, 2),
                 est = c(1:10 *2, 1:10*2 + 5),
                 lower_ci = c(1:10 * 2 -1, 1:10 * 2 +4),
                 upper_ci = c(1:10 * 2 +1, 1:10 * 2 +6),
                 manner = rep(c("fricative", "nasal"), each = 10))
ggplot(sm, aes(f_word_, est)) +
  geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci, fill=manner), alpha = 0.3) +
  geom_line(aes(colour = manner))

sm <- sm |> mutate(manner = factor(manner, levels = c("nasal", "fricative")))

ggplot(sm, aes(f_word_, est)) +
  geom_ribbon(aes(ymin = lower_ci, ymax = upper_ci, fill=manner), alpha = 0.3) +
  geom_line(aes(colour = manner))

Created on 2022-03-29 by the reprex package (v2.0.1)

1 Like

Thank you, @FJCC!
It worked!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.