Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an extra point in a ggplot2 graph

Tags:

r

ggplot2

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?

like image 446
Frits Verstraten Avatar asked Apr 11 '16 06:04

Frits Verstraten


People also ask

How do I add a point to an existing plot in R?

To add new points to an existing plot, use the points() function. The points function has many similar arguments to the plot() function, like x (for the x-coordinates), y (for the y-coordinates), and parameters like col (border color), cex (point size), and pch (symbol type).

How do you add data point labels to Ggplot?

To put labels directly in the ggplot2 plot we add data related to the label in the data frame. Then we use functions geom_text() or geom_label() to create label beside every data point. Both the functions work the same with the only difference being in appearance.

How do I connect dots in ggplot2?

In ggplot2 we can add lines connecting two data points using geom_line() function and specifying which data points to connect inside aes() using group argument.


Video Answer


1 Answers

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")
like image 121
erc Avatar answered Oct 06 '22 22:10

erc