ggplot,scale_colour_manual 个问题

ggplot, scale_colour_manual issues

谁能看出这段代码有什么问题?

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

这是我不断收到的消息:

Error: Unknown colour name: Afghanistan

这是数据集:https://drive.google.com/file/d/1j5I6odeWxaAiJlc7dHtD-Qj42xuP-gMs/view?usp=sharing

我建议你看看 ggplot 和“图形语法”是如何工作的(例如:https://ramnathv.github.io/pycon2014-r/visualize/ggplot2.html)。

所以首先你需要重塑你的数据以满足ggplot的要求:

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

生成的小标题:

    # 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

那么你就可以正确使用ggplot了:

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