Reputation: 351
I've created a data frame below that I would like to plot
Sample <- c("A1","B1","C1","A1","B1","C1")
X <- c(1,1,2,4,3,5)
Y <- c(2,3,1,5,4,6)
df <- data.frame(Sample, X, Y)
df
Sample X Y
1 A1 1 2
2 B1 1 3
3 C1 2 1
4 A1 4 5
5 B1 3 4
6 C1 5 6
ggplot(data = df, aes(x = X, y = Y, label = Sample)) +
geom_point()
However, I want to be able to customize the colors and shapes of each point. For instance, how would I make it so all my "A1" points are red and circular, all "B1" points blue and square and all "C1" points green and triangular?
Upvotes: 0
Views: 57
Reputation: 66490
It's usually simplest to map those aesthetics and then define their values using scale_*_manual
:
ggplot(data = df, aes(x = X, y = Y, label = Sample, shape = Sample, color = Sample)) +
geom_point(size = 3) +
scale_shape_manual(values = c("A1" = 16, "B1" = 15, "C1" = 17)) +
scale_color_manual(values = c("A1" = "red", "B1" = "blue", "C1" = "green"))
It's also possible to specify each group as a layer, but this can quickly get cumbersome, and doesn't lend itself as easily to having a legend:
ggplot(df, aes(X, Y)) +
geom_point(data = filter(df, Sample == "A1"), color = "red", shape = 16, size = 3) +
geom_point(data = filter(df, Sample == "B1"), color = "blue", shape = 15, size = 3) +
geom_point(data = filter(df, Sample == "C1"), color = "green", shape = 17, size = 3)
Upvotes: 2