Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ggplot, scale_colour_manual issues

Can anyone spot what is wrong in this code?

a <- c("Afghanistan"="darkgreen","Iraq"="red" ,"Mali"="green", "Nigeria"="purple","Senegal"="orange")

ggplot(data = full) + scale_colour_manual(values=a) + 
  geom_point(aes(x=Afghanistan_GDPpC, y=Afghanistan_AS), colour = "Afghanistan") +
  geom_smooth(aes(x=Afghanistan_GDPpC, y=Afghanistan_AS), colour = "Afghanistan", method = "lm") +
  geom_point(aes(x=Iraq_GDPpC, y=Iraq_AS), colour = "Iraq") +
  geom_smooth(aes(x=Iraq_GDPpC, y=Iraq_AS), colour = "Iraq", method = "lm") +
  geom_point(aes(x=Mali_GDPpC, y=Mali_AS), colour = "Mali") +
  geom_smooth(aes(x=Mali_GDPpC, y=Mali_AS), colour = "Mali", method = "lm") +
  geom_point(aes(x=Nigeria_GDPpC, y=Nigeria_AS), colour = "Nigeria") +
  geom_smooth(aes(x=Nigeria_GDPpC, y=Nigeria_AS), colour = "Nigeria", method = "lm") +
  geom_point(aes(x=Senegal_GDPpC, y=Senegal_AS), colour = "Senegal") + 
  geom_smooth(aes(x=Senegal_GDPpC, y=Senegal_AS), colour = "Senegal", method = "lm") +
  labs (x = "Log - GDP per Capita", y = "Log - Asylum Applications - First Time", colour = "Legend") +
  theme_classic()

This is the message I keep getting:

Error: Unknown colour name: Afghanistan

Here is the dataset: https://drive.google.com/file/d/1j5I6odeWxaAiJlc7dHtD-Qj42xuP-gMs/view?usp=sharing

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I advise you to look at how ggplot and "grammar of graphics" works (here for example: https://ramnathv.github.io/pycon2014-r/visualize/ggplot2.html).

So first you need to reshape your data to meet the requirements of ggplot:

full <- full %>% pivot_longer(cols = ends_with(c("AS","GDPpC")), 
                  names_to = c("country", ".value"),
                  names_sep="_") %>% 
rename("year" = "X1")

The resulting tibble:

    # A tibble: 50 x 4
    year country        AS GDPpC
   <dbl> <chr>       <dbl> <dbl>
 1  2011 Mali         8.29  6.73
 2  2011 Nigeria      9.32  7.82
 3  2011 Senegal      7.54  7.22
 4  2011 Afghanistan  9.94  6.38
 5  2011 Iraq         9.43  8.71
 6  2012 Mali         7.75  6.66
 7  2012 Nigeria      8.56  7.91
 8  2012 Senegal      7.70  7.18
 9  2012 Afghanistan  9.90  6.46
10  2012 Iraq         9.30  8.83
# ... with 40 more rows

Then you can use the ggplot correctly:

ggplot(data = full, mapping = aes(x = GDPpC, y = AS, col = country))+
geom_point()+
scale_color_manual(values = c("Afghanistan"="darkgreen","Iraq"="red" ,"Mali"="green", "Nigeria"="purple","Senegal"="orange"))+
geom_smooth(method = "lm")+
labs (x = "Log - GDP per Capita", y = "Log - Asylum Applications - First Time", colour = "Legend") +
theme_classic()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading