firmo23
firmo23

Reputation: 8454

Add annotation text in an arrow in ggplot2

I have the dataframe below:

df <- data.frame(result=floor(rnorm(1000, 100, 20)))    

to create this histogram:

require(scales)
df <- data.frame(result=floor(rnorm(1000, 100, 20)))   
ggplot(df)+
  geom_histogram(aes(result, fill = (result<=80 | result > 95)),
                 binwidth = 5, center = 2.5, color = "black") +
  scale_fill_manual(values=c("darkblue", "lightblue"), guide = F) +
  labs(y = "Frequency", x = "Result") + scale_y_continuous(labels = comma)+
  theme_classic(base_size = 16)+annotate("label",x=130,y=90, label = "text" )

I have added a label but I would like to know if there are other shapes -maybe colored- in which I could put the text. Ideally an arrow but also the label with the text and the arrow below. enter image description here

Upvotes: 5

Views: 8277

Answers (1)

Jon Spring
Jon Spring

Reputation: 66935

Adding a narrow arrow can be done like so:

... +
annotate("segment", x = 87.5, y = 110, xend = 87.5, yend = 98,
         arrow = arrow(type = "closed", length = unit(0.02, "npc")))

enter image description here

Or a thicker version with text in it:

...+
annotate("segment", x = 87.5, y = 115, xend = 87.5, yend = 100, 
         size = 8, linejoin = "mitre",
         arrow = arrow(type = "closed", length = unit(0.01, "npc"))) +
annotate("text", x=87.5,y=115, label = "text", color = "white", 
         angle = 90, hjust = 1.5, size = 5, fontface = "bold") 

enter image description here

Upvotes: 13

Related Questions