任何人都可以發現這段代碼有什么問題嗎?
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
uj5u.com熱心網友回復:
我建議您查看 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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/349193.html
上一篇:添加華為套件時出現“Couldnotfindcom.huawei.hms:location:6.0.0.302”錯誤
