简体   繁体   English

如何将点绘制为 R 中的颜色?

[英]How can I plot points as colors in R?

I have a CSV file that has 50 rows and three columns.我有一个有 50 行和三列的 CSV 文件。 I want to plot variable 1 against variable 2, while coloring each point depending on variable 3 (which is only one of 4 values).我想针对变量 2 绘制变量 1,同时根据变量 3(这只是 4 个值之一)为每个点着色。

I can successfully do this with the following code, however the points are all shades of blue (ie a gradient) instead of 4 unique colors.我可以使用以下代码成功地做到这一点,但是这些点都是蓝色阴影(即渐变)而不是 4 种独特的颜色。 As such, it's hard to distinguish between the points and would be much more useful if the 4 values of variable three were red, green, yellow, etc.因此,很难区分这些点,如果变量 3 的 4 个值是红色、绿色、黄色等,则会更有用。

This is really basic but I can't figure out why it would default to a gradient and not 4 random colors?这真的很基本,但我不明白为什么它会默认为渐变而不是 4 种随机颜色?

  sample<- read_csv("Sample Data.csv")
  ggplot(data=sample, mapping = aes(x=var_1, y=var_2))+
  geom_point(mapping = aes(color=var_3))

Perhaps, can change the 'var_3' to factor也许,可以将“var_3”更改为factor

library(dplyr)
library(ggplot2)
sample %>%
      mutate(var_3 = factor(var_3)) %>%
      ggplot(aes(x = var_1, y = var_2, color = var_3)) +
            geom_point()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM