簡體   English   中英

R:在R中添加一個圖例

[英]R: adding a plot legend in R

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

在此輸入圖像描述

我知道我可以使用legend()函數手動設置我的圖例。 但是,我不知道在我的數據中為不同的物種分配了哪種顏色? 是否有一種自動方式來獲取plot()來添加圖例?

正如@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())如果您不喜歡灰色背景,請使用theme_set(theme_bw()) )。

在此輸入圖像描述

內置的lattice包也可以做自動圖例:

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

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM