R:在R

时间:2016-10-01 13:54:31

标签: r plot visualization

plot(iris$Sepal.Length, iris$Sepal.Width, col = iris$Species)

enter image description here

我知道我可以使用legend()功能手动设置我的图例。但是,我不知道在我的数据中为不同的物种分配了哪种颜色?是否有自动方式让plot()添加图例?

1 个答案:

答案 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)

效果很好。

enter image description here

Base R没有自动图例工具:ggplot2包。

library(ggplot2)
ggplot(iris,aes(Sepal.Length,Sepal.Width,colour=Species))+geom_point()

为您提供带有自动图例的情节(如果您不喜欢灰色背景,请使用theme_set(theme_bw())。)

enter image description here

内置的lattice包也可以自动传说:

library(lattice)
xyplot(Sepal.Width~Sepal.Length,group=Species,data=iris,auto.key=TRUE)

enter image description here