Adding a extra point in a ggplot2 graph

2019-04-29 19:38发布

问题:

I have created a plot of the Sepal.Length and the Sepal.Width (using the iris dataset) with ggplot2.

  ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, col = Species)) + geom_point()

Works fine but now I would like to add a seperate point to the graph with a blue color. So for example:

  df = data.frame(Sepal.Width = 5.6, Sepal.Length = 3.9) 

Any thoughts on how I can accomplish this?

回答1:

Add another layer:

ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, col = Species)) + 
  geom_point() +
  geom_point(aes(x=5.6, y=3.9), colour="blue")


回答2:

library('ggplot2')

df = data.frame(Sepal.Width = 5.6, Sepal.Length = 3.9) 

ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, col = Species)) +
  geom_point() +
  geom_point(data = df, col = 'blue')



回答3:

Using annotate:

ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, col = Species)) + 
  geom_point() +
  annotate("point", x = 5.6, y = 3.9, colour = "blue")


标签: r ggplot2