R语言 计算值时,如何在条形图上方添加geom_text?

ehxuflar  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(125)

我在ggplot2中创建了一个条形图,我想在条形图上方添加每个条形的平均值,但不确定在这种情况下如何编写geom_text。

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")

这是我目前所拥有的,它会生成图形,但我无法在条形图上方添加geom_text而不出错。

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"))
6ju8rftf

6ju8rftf1#

使用ggplot有很多方法可以实现这一点,以下是使用内置mtcars数据集的两个潜在选项:

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")

创建于2023年1月13日,使用reprex v2.0.2

相关问题