geom_text в ggplot с geom_col и позицией "dodge" - PullRequest
0 голосов
/ 01 мая 2019

У меня проблемы с маркировкой.Я прочитал другие посты на эту тему, но не могу понять, что не так.Не могли бы вы помочь?

Данные:

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

Что это такое:

[![# 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(df, aes(month, attendance, fill = type)) + 
  geom_col(position = "dodge") + 
  geom_text(aes(label = attendance), position = position_dodge(width =-1), vjust=1)

Я понял,но я хочу, чтобы текст располагался над соответствующими пунктирными столбцами, а не по центру.Вы можете помочь?

image

1 Ответ

2 голосов
/ 01 мая 2019

Я считаю, что проблема в том, что твоя эстетика - это свидание. В некоторых случаях можно использовать 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"))

plot1

В качестве альтернативы вы можете использовать фасеты вместо:

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

plot2

Наконец, и это личное мнение, но если действительно нужно сравнение по «времени», я бы поспорил за график. Что-то вроде:

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

plot3

...