简体   繁体   English

ggplot中的geom_text,带有geom_col和位置“ dodge”

[英]geom_text in ggplot with geom_col and position “dodge”

I have issues with my labelling. 我的标签有问题。 I have read the other posts on the topic, but can't figure out what is wrong. 我已经阅读了有关该主题的其他文章,但无法弄清楚出了什么问题。 Can you please help? 你能帮忙吗?

Data: 数据:

df <- structure(list(type = c("Activity (ACTIV)", "Activity (ACTIV)", 
"Activity (ACTIV)", "Function (MEREC)", "Meeting (MEONE)", "Meeting (MEONE)", 
"Meeting (MEONE)", "Training (TRAIN)", "Training (TRAIN)", "Training (TRAIN)"
), month = structure(c(1546300800, 1548979200, 1551398400, 1551398400, 
1546300800, 1548979200, 1551398400, 1546300800, 1548979200, 1551398400
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), attendance = c(70, 
258, 125, 150, 2, 71, 180, 80, 105, 32)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), vars = "type", drop = TRUE, indices = list(
    0:2, 3L, 4:6, 7:9), group_sizes = c(3L, 1L, 3L, 3L), biggest_group_size = 3L, labels = structure(list(
    type = c("Activity (ACTIV)", "Function (MEREC)", "Meeting (MEONE)", 
    "Training (TRAIN)")), class = "data.frame", row.names = c(NA, 
-4L), vars = "type", drop = TRUE))

Which is this: 这是什么

[![# A tibble: 10 x 3
# Groups:   type \[4\]
   type             month               attendance
   <chr>            <dttm>                   <dbl>
 1 Activity (ACTIV) 2019-01-01 00:00:00         70
 2 Activity (ACTIV) 2019-02-01 00:00:00        258
 3 Activity (ACTIV) 2019-03-01 00:00:00        125
 4 Function (MEREC) 2019-03-01 00:00:00        150
 5 Meeting (MEONE)  2019-01-01 00:00:00          2
 6 Meeting (MEONE)  2019-02-01 00:00:00         71
 7 Meeting (MEONE)  2019-03-01 00:00:00        180
 8 Training (TRAIN) 2019-01-01 00:00:00         80
 9 Training (TRAIN) 2019-02-01 00:00:00        105
10 Training (TRAIN) 2019-03-01 00:00:00         32

ggplot: ggplot:

ggplot(df, aes(month, attendance, fill = type)) + 
  geom_col(position = "dodge") + 
  geom_text(aes(label = attendance), position = position_dodge(width =-1), vjust=1)

I get this, but I want the text to be above the relevant dodged columns, not all in the middle. 我明白了,但我希望文本位于相关的躲避栏上方,而不是全部位于中间。 Can you help? 你能帮我吗?

图片

I believe the issue is that your x aesthetic is a date. 我认为问题在于您的x美学是一个约会。 Some ways around this would be to use x = as.factor(month) and then manually set the labels for the scale: 解决此问题的一些方法是使用x = as.factor(month) ,然后手动设置刻度的标签:

ggplot(df, aes(x = as.factor(month), y = attendance, fill = type, label = attendance)) + 
  geom_col(position = "dodge") +
  geom_text(position = position_dodge(width = 0.9), vjust = -0.5) +
  scale_x_discrete(labels = function(x) format(as.Date(x), "%b"))

情节1

Alternatively, you may want to use facets instead: 或者,您可能要改用构面:

ggplot(df, aes(x = type, y = attendance, fill = type, label = attendance)) +
  geom_col() +
  geom_text(vjust = -0.5, size = 3) +
  facet_wrap(~ month) +
  theme(
    axis.text.x = element_text(angle = 60, hjust = 1, size = 6)
  )

情节2

Finally, and this is personal opinion, but if a comparison over "time" is truly wanted, I would argue for a line chart. 最后,这是个人观点,但是如果确实需要对“时间”进行比较,我会建议使用折线图。 Something like: 就像是:

ggplot(df, aes(x = month, y = attendance, color = type, label = attendance)) +
  geom_line() +
  geom_point(size = 2.5, fill = "white", shape = 21, stroke = 1, show.legend = FALSE) +
  scale_x_datetime(date_breaks = "1 month", date_labels = "%b")

情节3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM