Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Adding geom_vline labels to geom_histogram labels

Tags:

r

ggplot2

I would like to add the labels to the x-axis for the data from the geom_vline layer without having to regenerate the existing ones:

library(dplyr)
library(ggplot2)

data_frame(x = rnorm(10000)) %>% 
  ggplot(aes(x = x)) + 
  geom_histogram(bins = 100) + 
  geom_vline(aes(xintercept = mean(x) + 2.6)) + 
  theme_bw() 

enter image description here

like image 356
tchakravarty Avatar asked Dec 06 '25 06:12

tchakravarty


1 Answers

You could do this:

library(dplyr)
library(ggplot2)

data_frame(x = rnorm(10000)) %>% 
  ggplot(aes(x = x)) + 
  geom_histogram(bins = 100) + 
  geom_vline(aes(xintercept = mean(x) + 2.6)) + 
  theme_bw() +
  geom_text(aes(x=mean(x) + 2.6, label="My label text", y=0), colour="blue", angle=90)
like image 125
Hack-R Avatar answered Dec 07 '25 20:12

Hack-R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!