如何在R中制作图例? [英] How to make legend in R?

查看:277
本文介绍了如何在R中制作图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据属于许多类别之一.这是我对数据所做的非常简化的版本.我想制作一个散点图,其中不同的颜色代表不同的类别.但是,有许多不同的类别,因此我不是通过手动选择颜色,而是通过在plot函数中设置col=data$category来让R为我选择.但是,我无法弄清楚如何生成图例-放在legend函数中的每个参数实际上都不会生成任何东西.有人可以帮忙吗?

I have data that falls into one of many categories. Here is a very simplified version of what I'm doing to my data. I want to make a scatterplot where different colors represent different categories. However, there are many different categories so instead of manually choosing the colors, I'm letting R choose for me by setting col=data$category within the plot function. However, I can't figure out how to generate a legend -- every parameter that I put inside the legend function never actually generates anything. Can someone help?

data <- data.frame(rnorm(50),sample(1:10,50,replace=TRUE))
colnames(data) <- c("data", "category")
plot(data$data, col=data$category)
legend("topright", data$category)

推荐答案

尝试类似的方法

legend("topright", legend=unique(data$category), pch=1, col=unique(data$category))

这篇关于如何在R中制作图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆