1

I'm just starting to use R and I'm trying to make a scatter plot which has different colors based on different attributes. For example:

data.a = 1:5
data.b = 1:5
data.c = c("yes", "no", "yes", "maybe", "no")
plot(data.a, data.b)

I want to have a legend which is different colors for points which are "yes", "no", and "maybe" for data.c. I would also like points to be the colors in the legend.

4

3 に答える 3

13

@ MYaseen208の答えを詳しく説明するには:凡例(彼のコードが与えられた場合)については、次のようなものが必要です:

legend("topleft", legend=levels(factor(data.c)), text.col=seq_along(levels(factor(data.c))))
于 2012-01-17T08:11:36.287 に答える
1

これはggplot2の良い例のようです:

library("ggplot2")
data_df <- data.frame(a = data.a, b = data.b, c = data.c)
ggplot(data_df, aes(a,b)) + geom_point(aes(color=c))

サンプルデータの GGPlot2 プロット

于 2012-01-17T09:04:57.237 に答える
0

これを試してみてください

plot(data.a, data.b, col=factor(data.c))
于 2012-01-17T08:07:19.683 に答える