R按因子绘制颜色图例

标签 r plot colors legend r-factor

在 Windows 10 中使用 R 3.3.1。我正在根据 95 行数据制作 x-y 图。数据分为 6 个不同的分组(称为“组”的因素)。 情节本身很简单,但我无法让图例正确解释因素和颜色。

这是变量 v1 中的数据:

v1 <- structure(list(group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("F9", "T26", "W37", 
"W40", "W41", "W42"), class = "factor"), point = c(1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
14L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L), x = c(-7.064, -5.1681, 
-6.4866, -2.7522, -4.6305, -4.2957, -3.7552, -4.9482, -5.6452, 
-6.0302, -5.3244, -3.9819, -3.8123, -5.3085, -5.6096, -6.4557, 
-5.2549, -3.4893, -3.5909, -2.5546, -3.7247, -5.1733, -3.3451, 
-2.8993, -2.6835, -3.9495, -4.9649, -2.8438, -4.6926, -3.4768, 
-3.1221, -4.8175, -4.5641, -3.549, -3.08, -2.4153, -2.9882, -3.4045, 
-4.6394, -3.3404, -2.6728, -3.3517, -2.6098, -3.7733, -4.051, 
-2.9385, -4.5024, -4.59, -4.5617, -4.0658, -2.4986, -3.7559, 
-4.245, -4.8045, -4.6615, -4.0696, -4.6638, -4.6505, -3.7978, 
-4.5649, -5.7669, -4.519, -3.8561, -3.779, -3.0549, -3.1241, 
-2.1423, -3.2759, -4.224, -4.028, -3.3412, -2.8832, -3.3866, 
-0.1852, -3.3763, -4.317, -5.3607, -3.3398, -1.9087, -4.431, 
-3.7535, -3.2545, -0.806, -3.1419, -3.7269, -3.4853, -4.3129, 
-2.8891, -3.0572, -5.3309, -2.5837, -4.1128, -4.6631, -3.4695, 
-4.1045), y = c(7.76, 0.72, 4.1, 1.36, 0.13, -0.02, 0.13, 0.42, 
1.49, 2.64, 1.01, 0.08, 0.22, 1.01, 1.53, 4.39, 0.99, 0.56, 0.43, 
2.31, 0.31, 0.59, 0.62, 1.65, 2.12, 0.1, 0.24, 1.68, 0.09, 0.59, 
1.23, 0.4, 0.36, 0.49, 1.41, 3.29, 1.22, 0.56, 0.1, 0.67, 2.38, 
0.43, 1.56, 0.07, 0.08, 1.53, -0.01, 0.12, 0.1, 0.04, 3.42, 0.23, 
0, 0.34, 0.15, 0.03, 0.19, 0.17, 0.2, 0.09, 2.3, 0.07, 0.15, 
0.18, 1.07, 1.21, 3.4, 0.8, -0.04, 0.02, 0.74, 1.59, 0.71, 10.64, 
0.64, -0.01, 1.06, 0.81, 4.58, 0.01, 0.14, 0.59, 7.35, 0.63, 
0.17, 0.38, -0.08, 1.1, 0.89, 0.94, 1.52, 0.01, 0.1, 0.38, 0.02
)), .Names = c("group", "point", "x", "y"), class = "data.frame", row.names = c(NA, 
-95L))

这是我尝试叠加图例的情节:

> attach(v1)    
> plot(x,y, pch=16, col=group)   #simple plot, automatic colors
> #first legend
> legend("topleft", legend=group, pch=16, col=group)
> # colors matched, but it's breaking out every point
> legend("topright", legend=levels(group), pch=16, col=group)
> # Corrected the number of levels in legend, but no colors
>

您可以看到第一个图例在颜色方面显示正确,但它显示每个点的条目并且空间不足。第二个图例将组显示为因子级别,这是我想要的,但它不会改变颜色。

我意识到我可以将颜色作为矢量(例如 col(c("black","re​​d"等),但由于原始绘图命令自动分配颜色,我希望“自动”进行在我的传说中,避免在我的矢量中放置错误颜色的风险。

谢谢!

最佳答案

基础 R 解决方案:

attach(v1)    
plot(x,y, pch=16, col=group)
legend("topleft", legend=levels(group), pch=16, col=unique(group))

ggplot2解决方案

ggplot(v1)+
   geom_point(aes(x=x,y=y,colour=group))+
   theme_bw()

再次,我强烈建议您使用 ggplot2 而不是 base R,除非您只是在探索数据。关于 SO 的问题有很多问题/答案。

关于R按因子绘制颜色图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41704404/

相关文章:

python - 我在绘制球体和曲线时遇到问题

r - 使用Knitr和RStudio将ggplot2输出嵌入LaTeX pdf中

r - R Chi平方事后检验

r - 如何在plot.xts中设置线型?

r - ggplot2 和 geom_密度 : How to remove baseline?

java - 为什么我不能更改 JButton 的颜色?

java - 如何在字符串上突出显示

c++ - 在 C++ 中读取 BMP 奇怪的行为

r - 使用 T & F 而不是 TRUE & FALSE 有什么问题吗?

r - 为矩阵中的 "offset"对角线赋值