分组条的R geom_bar和geom_line标签

时间:2018-05-18 08:45:22

标签: r labels geom-bar

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

我正在使用以下库:

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

有人知道,我必须改变什么才能将标签放在正确的位置(对于分组的条形图)?

1 个答案:

答案 0 :(得分:1)

enter image description hereposition = position_dodge()geom_text()中使用geom_bar(),并将相同的数字参数传递给position_dodge()。 E.g:

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