问题
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?
回答1:
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.
回答2:
That is a really easy task for the graphical package Lattice
. It is designed especially for multivariate analysis, that can handle really easily.
I load the dataset iris
to provide an example.
library(lattice)
data("iris")
names(iris)
Lattice
allows immediate multifacet plots, showing the relationship between two variables according to a third one. In this case, we examined the relation between the Sepal Length and the Sepal Width of the flower, but for each species
in the dataset. It is expressed with the formula x ~ y | z
.
xyplot(Sepal.Length ~ Sepal.Width | Species, data = iris)
xyplot(Sepal.Length ~ Sepal.Width, data = iris,
groups = Species,
auto.key = TRUE)
For your case, we will plot all the observations in a single standard plot. In this case, the argument groups
will automatically color each observation according to the criteria you choose, in this case, species
again.
来源:https://stackoverflow.com/questions/36801274/colouring-scatter-graph-by-type-in-r