ggplot2: Label not showing with the geom_text() function

Viewed 942

I have a data frame, and I've made a scatter plot based on it (with ggplot2). Something like this:

df = data.frame(x = letters, y = c(1:26))
p = ggplot(df, aes(x = x, y = y)) + geom_point()

But I want to add a specific label to the plot (using geom_text), in a certain coordenate. The problem is that the label isn't showing in the printed plot.

p + geom_text(x = 3, y = 30, label = "Scatter plot")

Plot without printed label

This particular error just appears only if I want to use coordinates, 'cause if I remove the coordinates the plot is printed with the labels (although it appears in every point in the plot).

Am I writing something wrong?

1 Answers

The ylim is just below that range i.e. it is based on the values provided in the data i.e. 1 to 26. It can be modified by specifying a custom ylim layer

library(ggplot2)
p <- ggplot(df, aes(x = x, y = y)) + 
           geom_point() + 
           ylim(c(1, 35))
p + 
     geom_text(x = 3, y = 30, label = "Scatter plot")

-output

enter image description here

Related