Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add counts to geom_hex as labels

Tags:

r

ggplot2

I would like to add labels to geom_hex, which raises two questions:

  1. how to I get their coordinates;
  2. how do I extract their count value?

minimal example:

pipeline <- read.csv(url('http://dl.dropboxusercontent.com/u/7446674/pipeline.csv'),sep="\t",header=T)
pipeline <- pipeline[pipeline$Units>0,]

ggplot(pipeline,aes(x=Longitude,y=Latitude))+
    #stat_density2d(n=25,aes(fill=..level..), geom="polygon") +
      geom_hex(bins=12)+
      coord_equal(ratio = 1/1)+
      theme_bw()+
      ggtitle('San Francisco Development Pipeline\nQ2 2013')

(Also, on the geom_hex, if anyone knows if weighs have been implemented yet I would be interested in knowing that, too) enter image description here

like image 810
ako Avatar asked Sep 13 '25 15:09

ako


1 Answers

You can label each bin with its count using:

ggplot(pipeline,aes(x=Longitude,y=Latitude))+
  geom_hex(bins=12)+
  stat_binhex(aes(label=..count..), geom="text", bins=12, colour="white") +
  coord_equal(ratio = 1/1)+
  theme_bw()+
  ggtitle('San Francisco Development Pipeline\nQ2 2013')

(PS: was it the counts you wanted as labels? It seemed that way, but I wasn't entirely sure)

Labelled counts

like image 124
Marius Avatar answered Sep 16 '25 04:09

Marius