创建彩色散点图

时间:2015-10-12 04:53:32

标签: r

我在课堂上学习这些数据,教授让我们基本上复制和粘贴代码而不是教我们任何东西。我试图弄清楚如何创建一个散点图,说明无人机战争的打击率和平民伤亡。
我遇到的问题是如何使用数据中的变量来指示数据点的颜色。我想最低限度地使用" status" (死/ 2,活着/ 1)给点着色。 如果我能够根据无人机目标的国籍来弄清楚如何给点着色,那也是理想的,因为我有数据。无论如何,这是我到目前为止所拥有的。它创建了点,但不是颜色。我想知道如何创造颜色。

symbols(killVStarget$name, killVStarget$strikes, 
        circles=sqrt(killVStarget$casualties), 
        col=ifelse(killVStarget$status==2, "red", "black"), cex=0.15)

我从.csv文件导入数据。以下是从Excel中复制的前10个条目:

name                   nationality  status  strikes  casualties
baitullah mehsud       pakistani    2       7        164
qari hussain           pakistani    2       6        128
abu ubaidah al masri   pakistani    2       3        120
mullah sangeen zadran  pakistani    2       3        108
ayman al-zawahiri      pakistani    1       2        105
sirajudin haqqani      pakistani    1       5        82
hakimullah mehsud      pakistani    2       5        68
sadiq noor             pakistani    2       4        57
said al-shihri         yemeni       2       4        57

1 个答案:

答案 0 :(得分:1)

df <- data.frame(name =  c("baitullah mehsud    pakistani", "qari hussain pakistani", "abu ubaidah al masri    pakistani", "mullah sangeen zadran   pakistani",
                           "ayman al-zawahiri   pakistani", "sirajudin haqqani   pakistani", "hakimullah mehsud   pakistani", "sadiq noor  pakistani",
                           "said al-shihri  yemeni "), strikes = c(7, 6, 3, 3, 2, 5, 5, 4, 4), status = c(2, 2, 2, 2, 1, 1, 2, 2, 2),
                           casualities = c(164, 128, 120, 108, 105, 82, 68, 57, 57)
                           ) 



library(ggplot2)
 ggplot(aes(x = name, y = strikes, size = casualities, color = factor(status)), data = df) + geom_point()

enter image description here

ggplot(aes(x = strikes, y = name, size = casualities, color = factor(status)), data = df) + geom_point()

enter image description here