Home > Enterprise >  Set y position for label - ggplot
Set y position for label - ggplot

Time:09-01

Say I have the below df

library(ggplot2)
library(data.table)

# dummy data
df <- data.table(revenue = rnorm(1e4, mean = 100, sd = 1)); df

         revenue
    1: 100.01769
    2:  98.31862
    3:  99.78464
    4: 100.17670
    5:  99.31678
   ---          
 9996:  99.47635
 9997:  98.27383
 9998:  99.48378
 9999: 100.06227
10000:  99.13972

and that I plot a histogram with a vline denoting the mean

# mean of x axis
x <- df[, mean(revenue)]

# plot
ggplot(df, aes(x = revenue))  
  geom_histogram(aes(y = (..count..) / sum(..count..)))   # turn count into %
  geom_vline(aes(xintercept = x), col = 'red', size = 1)

enter image description here

The above is fine. However, when trying to add a label showing the mean, I am unsure what to enter for y in geom_label(aes(x = x, y = ?)...:

# plot
ggplot(df, aes(x = revenue))  
  geom_histogram(aes(y = (..count..) / sum(..count..)))   # turn count into %
  geom_vline(aes(xintercept = x), col = 'red', size = 1)  
  geom_label(aes(x = x, y = ?)
             , label = x
             )

I have tried small numbers such as 0.025 (after looking at the density from the previous plot) but then R gets stuck and creating the plot and never finishes. Say, I'd like to position the numeric label at where y axis = 0, then what value should I put into y = ? Thank you

CodePudding user response:

Remove aes() from geom_label, you are plotting constants, not a data.table variables . To plot where the y axis is zero, well, make y = 0.

Use hjust and vjust to position the label relative to (the constants) x, y. From the documentation:

Alignment
You can modify text alignment with the vjust and hjust aesthetics. These can either be a number between 0 (right/bottom) and 1 (top/left) or a character ("left", "middle", "right", "bottom", "center", "top"). There are two special alignments: "inward" and "outward". Inward always aligns text towards the center, and outward aligns it away from the center.

And it does take time. Once again from the documentation, my emphasis:

geom_label()
Currently geom_label() does not support the check_overlap argument or the angle aesthetic. Also, it is considerably slower than geom_text(). The fill aesthetic controls the background colour of the label.

library(ggplot2)
#library(data.table)

# dummy data
df <- data.frame(revenue = rnorm(1e4, mean = 100, sd = 1))

xbar <- mean(df$revenue)

# plot
ggplot(df, aes(x = revenue))  
  geom_histogram(aes(y = (..count..) / sum(..count..)), bins = 30)   
  geom_vline(aes(xintercept = xbar), col = 'red', size = 1)  
  geom_label(x = xbar, y = 0, label = round(xbar, 2), 
            hjust = -0.5, vjust = 1, 
            fill = "white", color = "black")

Created on 2022-08-31 by the reprex package (v2.0.1)

  • Related