plot(iris$Sepal.Length, iris$Sepal.Width, col = iris$Species)
我知道我可以使用legend()
功能手动设置我的图例。但是,我不知道在我的数据中为不同的物种分配了哪种颜色?是否有自动方式让plot()
添加图例?
答案 0 :(得分:3)
正如@rawr所说,palette()
确定使用的颜色序列。如果使用整数指定颜色,它也会查看palette()
。因此
with(iris,plot(Sepal.Length, Sepal.Width, col = Species))
legend("topright",legend=levels(iris$Species),col=1:3, pch=1)
效果很好。
Base R没有自动图例工具:ggplot2
包。
library(ggplot2)
ggplot(iris,aes(Sepal.Length,Sepal.Width,colour=Species))+geom_point()
为您提供带有自动图例的情节(如果您不喜欢灰色背景,请使用theme_set(theme_bw())
。)
内置的lattice
包也可以自动传说:
library(lattice)
xyplot(Sepal.Width~Sepal.Length,group=Species,data=iris,auto.key=TRUE)