Home > Blockchain >  How do I add geom_text above bars when value is calculated?
How do I add geom_text above bars when value is calculated?

Time:01-13

I created a bar plot in ggplot2 and I want to add the mean of each bar above the bar, but am not sure how to write geom_text in this situation.

ggplot(trim_merg_divvy_df03)   
  geom_bar(aes(x = member_casual, y = ride_length, fill = member_casual),
           width = 0.2,
           stat = "summary",
           fun = "mean") 
  xlab("User Type") 
  ylab("Mean Ride Length")  
  labs(title = "Mean Ride Length (dec. hr.)", subtitle = "Member vs Casual", fill = "User Type")

This is what I have so far and it produces the graph but I am not able to add the geom_text above the bars without error.

structure(list(ride_id = c("7C00A93E10556E47", "90854840DFD508BA", 
"0A7D10CDD144061C", "2F3BE33085BCFF02", "D67B4781A19928D4", "02F85C2C3C5F7D46", 
"EF780B807EF7835A", "93FC4662BDC5C061", "B06B064398A360B3", "A2A194358CA64D0B", 
"A839D4F88EDA71EB", "647C651CAFF802D0", "2B363CD2C6F6B8D8", "FA8C6337712E753A", 
"D61A2352CA2C5EFA", "13AB8309FA38693A", "D263489979BDC0B3", "68445DE9B28E1421", 
"E27A3D728692BD37", "8590EDD890F9088C"), rideable_type = c("electric_bike", 
"electric_bike", "electric_bike", "electric_bike", "electric_bike", 
"electric_bike", "electric_bike", "electric_bike", "electric_bike", 
"electric_bike", "electric_bike", "electric_bike", "electric_bike", 
"electric_bike", "electric_bike", "electric_bike", "electric_bike", 
"electric_bike", "electric_bike", "electric_bike"), started_at = structure(c(1638019658, 
1638020305, 1637964214, 1638007009, 1637953768, 1637951647, 1638019872, 
1638004473, 1638029611, 1638017350, 1638019319, 1638010985, 1638032284, 
1635855652, 1635837019, 1635801595, 1635874302, 1635834159, 1635867871, 
1635838714), tzone = "UTC", class = c("POSIXct", "POSIXt")), 
    ended_at = structure(c(1638020798, 1638021370, 1637964356, 
    1638007310, 1637955041, 1637952769, 1638020232, 1638004776, 
    1638030170, 1638017567, 1638020839, 1638011233, 1638032992, 
    1635858338, 1635837394, 1635802034, 1635874820, 1635834465, 
    1635868566, 1635840064), tzone = "UTC", class = c("POSIXct", 
    "POSIXt")), start_lat = c(41.93, 41.96, 41.96, 41.94, 41.9, 
    41.9, 41.81, 41.8, 41.78, 41.91, 41.92, 41.92, 41.92, 41.9, 
    41.78, 41.8, 41.8, 41.8, 41.96, 41.89), start_lng = c(-87.72, 
    -87.7, -87.7, -87.79, -87.63, -87.62, -87.6, -87.6, -87.6, 
    -87.72, -87.71, -87.71, -87.78, -87.7, -87.6, -87.59, -87.59, 
    -87.6, -87.68, -87.77), end_lat = c(41.96, 41.92, 41.96, 
    41.93, 41.88, 41.9, 41.8, 41.79, 41.8, 41.92, 41.97, 41.91, 
    41.92, 41.88, 41.8, 41.78, 41.78, 41.8, 41.94, 41.89), end_lng = c(-87.73, 
    -87.7, -87.7, -87.79, -87.62, -87.63, -87.6, -87.6, -87.59, 
    -87.71, -87.68, -87.72, -87.78, -87.63, -87.59, -87.6, -87.6, 
    -87.59, -87.69, -87.77), member_casual = c("casual", "casual", 
    "casual", "casual", "casual", "casual", "casual", "casual", 
    "casual", "casual", "casual", "casual", "casual", "casual", 
    "casual", "casual", "casual", "casual", "casual", "casual"
    ), ride_length = structure(c(0.32, 0.3, 0.04, 0.08, 0.35, 
    0.31, 0.1, 0.08, 0.16, 0.06, 0.42, 0.07, 0.2, 0.75, 0.1, 
    0.12, 0.14, 0.09, 0.19, 0.38), class = "difftime", units = "hours"), 
    start_day = structure(c(6L, 6L, 5L, 6L, 5L, 5L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 2L, 2L, 1L, 2L, 2L, 2L, 2L), levels = c("Mon", 
    "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"), class = "factor"), 
    start_hr = c(13L, 13L, 22L, 9L, 19L, 18L, 13L, 9L, 16L, 12L, 
    13L, 11L, 16L, 12L, 7L, 21L, 17L, 6L, 15L, 7L)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

CodePudding user response:

There are many ways to do this with ggplot, here are two potential options using the built-in mtcars dataset:

library(ggplot2)

# use a function
n_fun <- function(x){
  return(data.frame(y = mean(x)   7, label = round(mean(x), digits = 2)))
}

ggplot(mtcars, aes(x = factor(cyl), y = hp, fill = factor(cyl)))   
  geom_bar(width = 0.2,
           stat = "summary",
           fun = "mean")  
  stat_summary(fun.data = n_fun, geom = "text")  
  xlab("Number of Cylinders")  
  ylab("Mean Horsepower")  
  labs(title = "Mean Horsepower of Cars in the mtcars dataset",
       subtitle = "Member vs Casual", fill = "Cylinders")

library(tidyverse)

# calculate the statistics beforehand

mtcars %>%
  group_by(cyl) %>%
  mutate(mean_hp = round(mean(hp), digits = 2)) %>%
  ungroup() %>%
  ggplot(aes(x = factor(cyl), y = hp, fill = factor(cyl)))   
  geom_bar(width = 0.2,
           stat = "summary",
           fun = "mean")  
  geom_text(aes(y = mean_hp   7, label = mean_hp),
            check_overlap = TRUE)  
  xlab("Number of Cylinders")  
  ylab("Mean Horsepower")  
  labs(title = "Mean Horsepower of Cars in the mtcars dataset",
       subtitle = "Member vs Casual", fill = "Cylinders")

Created on 2023-01-13 with reprex v2.0.2

  • Related