0

I'm trying to add a single point to a curve (line equation in terms of x) in ggplot in r along an x axis from 0 to 35. Whenever I try to plot a point I run into the problem where I get an error with aes()... how do I fix this? All I need is a singular point labeled "point" that is blue and at those coordinates.

ggplot(data.frame(x=c(0,35)), aes(x)) +
  stat_function(fun=function(x)10^(-0.05841*x)+10^7.2241, geom="line") +
 geom_point() +
annotate("point", x = 23, y = 39000, colour = "blue")

And when I run this I get this error:

Aesthetics must be either length 1 or the same as the data (2): y

thanks!

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
Neel Agarwal
  • 29
  • 1
  • 3
  • What's do you intend the `geom_point` call to do? If you take it out, this runs. – alistaire Jun 30 '19 at 03:30
  • 1
    Duplicate of [Color one point and add an annotation in ggplot2?](https://stackoverflow.com/questions/14351608/color-one-point-and-add-an-annotation-in-ggplot2) – M-- Jun 30 '19 at 04:30

2 Answers2

4

Perhaps like this?

library(ggplot2)
ggplot(data.frame(x=c(0,35)), aes(x)) +
  stat_function(fun=function(x)10^(-0.05841*x)+10^7.2241, geom="line") +
  annotate("point", x = 23, y = 39000, colour = "blue") +
  annotate("text", x = 23, y = 39000, label = "point", colour = "blue", vjust = -0.5)

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
2

If I try to run your code I get a different error:

Error: geom_point requires the following missing aesthetics: y

So it's easily solved:

ggplot(data.frame(x=c(0,35)), aes(x)) +
     stat_function(fun=function(x)10^(-0.05841*x)+10^7.2241, geom="line") +
     geom_point(aes(x = 23, y = 39000), color = "blue", size = 2) +
     geom_text(data = data.frame(x = 23, y = 39000), aes(x, y, label = "point"))

Notice that I changed your annotate with geom_text, just in case you want to annotate not one, but several and do it from a data.frame

PavoDive
  • 6,322
  • 2
  • 29
  • 55