Home > Mobile >  How to rotate label in the top of each bar in geom_histogram
How to rotate label in the top of each bar in geom_histogram

Time:11-26

I'm struggling on how can I rotate the label of count values in geom_histogram. I'd like to rotate the labels, that should be in the top of each bar. I'm using the following code:

Toy example:

ex_tfdp = data.frame(x=rnorm(1000,10,10))

ggplot(ex_tfdp)  
geom_histogram(aes(x = x), boundary = 0,binwidth = 5, color="black", fill="#FFC857") 
  stat_bin( geom="text", colour="black", size=3.5,
          aes(x = x,label=..count..), angle = 90, vjust = -1.5)  
 theme(axis.text.x = element_text(angle = 90, hjust = .5),
        panel.background = element_blank(),
        legend.position="bottom",
        legend.title=element_blank(),
        axis.ticks.y=element_blank())

enter image description here

Any hint on how can I change the angle of these values and put them in the top of the each bar?

CodePudding user response:

We don't have your data, but assuming it is similar to the following:

set.seed(2)
my_data <- data.frame(measure = rexp(20000)^1.2 * 150)

Then you can correctly bin your data by switching to geom_text and using stat = "bin". Then you can still pass breaks as an argument to the stat. This allows you to use the geom_text parameter angle to rotate 90 degrees. A bit of hjust and vjust tweaking then gets your labels right:

ggplot(my_data)  
  geom_histogram(aes(x = measure), boundary = 0,
                 binwidth = 100, color="black", fill="#FFC857") 
   geom_text(stat = "bin", colour = "black", size = 3.5,
             breaks = seq(0,2100,100), hjust = -0.5,
            aes(x = measure, label=..count..), vjust = 0.5, angle = 90) 
         scale_x_continuous(breaks = seq(0,2100,100), 
                            labels = seq(0,2100,100),
                            expand = expansion(add = c(0, 0)))  
  scale_y_continuous(limits = c(0, 15000))  
  theme(axis.text.x = element_text(angle = 90, hjust = .5),
        panel.background = element_blank(),
        legend.position="bottom",
        legend.title=element_blank(),
        axis.ticks.y=element_blank())

enter image description here

CodePudding user response:

I have simplified your example a bit:

require(ggplot2)
require(gridExtra)

set.seed(15)
ex_tfdp = data.frame(x=rnorm(1000,10,10))

original.plot = ggplot(ex_tfdp)  
    geom_histogram(aes(x = x), boundary = 0,binwidth = 5, color="black", fill="#FFC857") 
    stat_bin( geom="text", colour="black", size=3.5,
              aes(x = x,label=..count..), angle = 90, vjust = -1.5)

new.plot = ggplot(ex_tfdp)  
    geom_histogram(aes(x = x), boundary = 0, binwidth = 5)  ylim(c(0, 220))  
    stat_bin( geom="text", colour="black", size=3.5,
              aes(x = x,label=..count..), angle = 90, boundary = 0, binwidth = 5, hjust = -.5)

grid.arrange(original.plot, new.plot, ncol=2)    

enter image description here

The key here is to keep the boundary = 0 and binwidth = 5 when adding the stat_bin layer.

  • Related