按 r 中的类型为散点图着色 [英] Colouring scatter graph by type in r
问题描述
有没有办法在 R 中创建散点图并按类别为点着色?例如,我有一个包含 x 和 y 点列表的数据集(Xpoints"和Ypoints"列的比例从 0 到 100),但每个组合的 x&y 点属于五个类别之一(在第三个列类别").我想绘制所有组合的 x&y 点并按其各自的类别为每个点着色.我猜这五个类别必须在附加列中分配数字 1 到 5.但是绘制这个的代码是什么?
Is there a way you can create a scatter graph in R and colour the points by a category? For example, I have a dataset with a list of x and y points (both columns 'Xpoints' and 'Ypoints' have scales from 0 to 100), but each combined x&y point belongs to one of five categories (in a third column 'Category'). I would like to plot all the combined x&y points AND colour each point by its respective category. I'm guessing that the five categories would have to allocated numbers 1 to 5 in an additional column. But what would be the code to plot this?
推荐答案
使用广泛用于此类应用程序的 ggplot2
包.
Use the ggplot2
package which is widely used for applications such as these.
# toy data
my_data <- data.frame(x = sample(1:100, replace = T, 100),
y = sample(1:100, replace = T, 100),
cat = sample(c('first', 'second', 'third'), replace = T, 100)
)
# required packages
require(ggplot2)
# make the graph
ggplot(data = my_data, aes(x = x, y = y, color = cat)) +
geom_point()
ggsave(height = 4, width = 4, filename = 'SO36801313.png')
上面的代码提供了下图.
The code above provides the following graph.
这篇关于按 r 中的类型为散点图着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!