R geom_bar 和 geom_line 分组条的标签

R geom_bar and geom_line labels for grouped bars

我想绘制带标签的分组条形图,但它们仅显示为堆叠条形标签。

我正在使用以下库:

library(dplyr)
library(ggplot2)
library(scales)

在以下我使用的数据框中:

df1 <- data.frame(month = c("2017-10-01", "2017-10-01", "2017-10-01", "2017-11-01", "2017-11-01", "2017-11-01", 
                            "2017-12-01", "2017-12-01", "2017-12-01", "2018-01-01", "2018-01-01", "2018-01-01", 
                            "2018-02-01", "2018-02-01", "2018-02-01", "2018-03-01", "2018-03-01", "2018-03-01"), 
                 source = c("a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c"),
                 value1 = c(sample (c(1000L:4000L),18, replace = FALSE)),
                 stringsAsFactors = FALSE
                 )

df1$month <- as.Date(as.character(df1$month, format = "%Y%m$d"))
df1$month <- as.Date(format(df1$month, "%Y-%m-01"))
df1 <- arrange(df1, month)

df2:

df2 <- data.frame(month = c("2017-10-01", "2017-11-01", "2017-12-01", "2018-01-01", "2018-02-01", "2018-03-01"), 
                  value2 = c(sample (c(5000000L:6000000L),6, replace = FALSE)), 
                  stringsAsFactors = FALSE
                  )
df2$month <- as.Date(as.character(df2$month, format = "%Y%m$d"))
df2$month <- as.Date(format(df2$month, "%Y-%m-01"))
df2 <- arrange(df2, month)

图表代码:

ggplot() +
  geom_bar(data = df1, aes(month, value1*5000000/5000, fill = source), stat="identity", position = "dodge") +
  geom_point(data = df2, aes(month, value2), color = "blue")+
  geom_line(data = df2, aes(month, value2), group = 1, color = "blue") +
  labs(x = "month", y="value2 (line)") +
  scale_y_continuous(sec.axis = sec_axis(~.*5000/5000000, name = "value1 (bars)"), 
                     labels= format_format(big.mark = ".", decimal.mark = ",", scientific = FALSE)) +
  scale_x_date(labels = date_format("%Y-%b"),
               breaks = seq(from = min(df2$month), to = max(df2$month), by = "month")) +
  scale_fill_manual(values= c("darkseagreen4","darkseagreen", "darkseagreen3", "darkseagreen2")) +
  geom_text(aes(label=df1$value1, x = df1$month, y = df1$value1*6000000/5000, group = df1$source), position = position_dodge(1.5), vjust = 1.5, size = 3) +
  geom_text(aes(label=df2$value2, x = df2$month, y = df2$value2), vjust = 1.5, size = 3, color = "grey47") +
  theme_light()

有谁知道我必须更改什么才能使标签处于正确的位置(对于分组条)?

geom_text()geom_bar() 中使用 position = position_dodge(),并将相同的数字参数传递给 position_dodge()。例如:

ggplot() +
  geom_bar(data = df1, aes(month, value1*5000000/5000, fill = source), stat="identity", position = position_dodge(25)) +
  geom_point(data = df2, aes(month, value2), color = "blue")+
  geom_line(data = df2, aes(month, value2), group = 1, color = "blue") +
  labs(x = "month", y="value2 (line)") +
  scale_y_continuous(sec.axis = sec_axis(~.*5000/5000000, name = "value1 (bars)"), 
                     labels= format_format(big.mark = ".", decimal.mark = ",", scientific = FALSE)) +
  scale_x_date(labels = date_format("%Y-%b"),
               breaks = seq(from = min(df2$month), to = max(df2$month), by = "month")) +
  scale_fill_manual(values= c("darkseagreen4","darkseagreen", "darkseagreen3", "darkseagreen2")) +
  geom_text(aes(label=df1$value1, x = df1$month, y = df1$value1*6000000/5000, group = df1$source), position = position_dodge(25), vjust = 1.5, size = 3) +
  geom_text(aes(label=df2$value2, x = df2$month, y = df2$value2), vjust = 1.5, size = 3, color = "grey47") +
  theme_light()