我正在使用starwars
数据集(dplyr
包(,我想制作一个图,其中自变量是角色的身高,因变量是他们的体重。此外,我还想通过颜色辨别物种:
library(dplyr)
starwars
par(mar = c(5.3, 4.3, 4.3, 8.3), xpd = TRUE)
plot(starwars$mass ~ starwars$height, ylim = c(0, 200),
col = as.factor(starwars$species), bty = "l")
legend("topright", inset = c(-0.2, 0), legend = as.factor(starwars$species),
cex = 0.50, col = as.factor(starwars$species),
ncol = 2, pch = 16)
图
请注意,有些物种在传说中重复出现。如何排除这些重复?
在legend
:中仅包括unique
值
plot(starwars$mass ~ starwars$height, ylim = c(0, 200),
col = as.factor(starwars$species), bty = "l")
legend("topright", inset = c(-0.2, 0),
legend = as.factor(unique(starwars$species)),
cex = 0.50, col = as.factor(unique(starwars$species)),
ncol = 2, pch = 16)