Home > other >  ggplot, scale_colour_manual issues
ggplot, scale_colour_manual issues

Time:11-06

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

CodePudding user response:

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()
  • Related