ggplot: text printed by geom_text is not clear

Viewed 12880

The text printed using geom_text is not very clear. How can I make it more clear?

data = data.frame(rnorm(1000))
colnames(data) = "numOfX"
m <- ggplot(data, aes(x=numOfX))
m + geom_histogram(colour = "blue", fill = "white", binwidth = 0.5) +
  annotate("segment", x=10,xend=10,y=20,yend=0,arrow=arrow(), color="blue") +
  geom_text(aes(10, 30, label="Observed \n value"), color = "blue") 

enter image description here

2 Answers

Expanding on Dave Gruenewald's comment, geom_text now has the check_overlap option. See the tidyverse reference:

check_overlap -- If TRUE, text that overlaps previous text in the same layer will not be plotted. check_overlap happens at draw time and in the order of the data. Therefore data should be arranged by the label column before calling geom_text(). Note that this argument is not supported by geom_label().

library(ggplot2)
data = data.frame(rnorm(1000))
colnames(data) = "numOfX"
m <- ggplot(data, aes(x=numOfX))
m + geom_histogram(colour = "blue",
                   fill = "white",
                   binwidth = 0.5) +
  annotate("segment",
           x = 10, xend = 10,
           y = 20, yend = 0,
           arrow = arrow(), color="blue") +
  geom_text(aes(10, 30, label="Observed \n value"),
            color = "blue", check_overlap = T)

enter image description here

Related