Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place annotation at the top of a series of histograms in ggplot2 using a for loop

I am creating a number of histograms and I want to add annotations towards the top of the graph. I am plotting these using a for loop so I need a way to place the annotations at the top even though my ylims change from graph to graph. If I could store the ylim for each graph within the loop I could cause the y coordinates for my annotation to vary based on the current graph. The y value I include in my annotation must change dynamically as the loop proceeds across iterations. Here is some sample code to demonstrate my issue (Notice how the annotation moves around. I need it to change based on the ylim for each graph):

library(ggplot2)

cuts <- levels(as.factor(diamonds$cut))

pdf(file = "Annotation Example.pdf", width = 11, height = 8,
    family = "Helvetica", bg = "white")

for (i in 1:length(cuts)) {
  by.cut<-subset(diamonds, diamonds$cut == cuts[[i]])
  print(ggplot(by.cut, aes(price)) +
    geom_histogram(fill = "steelblue", alpha = .55) +
  annotate ("text", label = "My annotation goes at the top", x = 10000 ,hjust = 0, y = 220, color = "darkred"))
}    
dev.off()
like image 389
Braden Avatar asked Jun 13 '12 16:06

Braden


People also ask

What is annotate in ggplot2?

The annotate() function allows to add all kind of shape on a ggplot2 chart. The first argument will control what kind is used: rect or segment for rectangle, segment or arrow.

Which method is used to create a histogram using ggplot2 in R?

Basic histogram with geom_histogram It is relatively straightforward to build a histogram with ggplot2 thanks to the geom_histogram() function. Only one numeric variable is needed in the input.

Can you build a histogram using ggplot2?

You can also make histograms by using ggplot2 , “a plotting system for R, based on the grammar of graphics” that was created by Hadley Wickham. This post will focus on making a Histogram With ggplot2.

What function from the ggplot2 package would you use to create a histogram plot?

This R tutorial describes how to create a histogram plot using R software and ggplot2 package. The function geom_histogram() is used.


2 Answers

ggplot uses Inf in its positions to represent the extremes of the plot range, without changing the plot range. So the y value of the annotation can be set to Inf, and the vjust parameter can also be adjusted to get a better alignment.

...
print(ggplot(by.cut, aes(price)) +
      geom_histogram(fill = "steelblue", alpha = .55) +
      annotate("text", label = "My annotation goes at the top", 
               x = 10000, hjust = 0, y = Inf, vjust = 2, color = "darkred"))
...

For i<-2, this looks as:

enter image description here

like image 80
Brian Diggs Avatar answered Nov 05 '22 04:11

Brian Diggs


There may be a neater way, but you can get the max count and use that to set y in the annotate call:

for (i in 1:length(cuts)) {

  by.cut<-subset(diamonds, diamonds$cut == cuts[[i]])

  ## get the cut points that ggplot will use. defaults to 30 bins and thus 29 cuts
  by.cut$cuts <- cut(by.cut$price, seq(min(by.cut$price), max(by.cut$price), length.out=29))

  ## get the highest count of prices in a given cut.
  y.max <- max(tapply(by.cut$price, by.cut$cuts, length))

  print(ggplot(by.cut, aes(price)) +
    geom_histogram(fill = "steelblue", alpha = .55) +
    ## change y = 220 to y = y.max as defined above
    annotate ("text", label = "My annotation goes at the top", x = 10000 ,hjust = 0, y = y.max, color = "darkred"))
}  
like image 33
Justin Avatar answered Nov 05 '22 04:11

Justin