I have stumbled upon visualization issue, regarding KNN clustering in R. In the following link: how to plot KNN clusters boundaries in r the first answer explicitly shows how to plot KNN clusters in R.
My question is, based on that code, how would it be possible to control the color and the symbol types of both clusters?
I am new user in Stack and hardly can add a comment in the question from the link above. Therefore, I would be very thankful if someone can help or give a hint.
Thanks in advance :)
The colors and shapes are controled by the aes()
and by the layers scale_*
.
If the legends for each of the aesthetics are to be merged, set names
to the same character string, in the code below I replaced "predicted"
by "Cluster"
. Try keeping one of the scale_
's name and see the result.
ggplot(plot.df, aes(Sepal.Length, Sepal.Width,
color = predicted, fill = predicted, shape = predicted)) +
geom_point(size = 5) +
geom_polygon(data = boundary, aes(x,y), alpha = 0.5) +
#
scale_color_manual(name = "Cluster", values = c("red", "green", "blue")) +
scale_fill_manual(name = "Cluster", values = c("red", "green", "blue")) +
scale_shape_manual(name = "Cluster", values = c(22, 23, 24))