R ggplot2 упорядочение меток (geom_text) - PullRequest
0 голосов
/ 23 мая 2018

У меня есть график, который отображает комбинацию гистограммы и линейного графика.Теперь я хочу добавить метки к графику, но все они нанесены на линию вместо правильной позиции.

Вот df, который я использую:

df1 <- data.frame(cat = c("category1", "category2", "category3", 
                            "category1", "category2", "category3",
                            "category1", "category2", "category3"), 
                  source = c("a", "a", "a", "b", "b", "b", "c", "c", "c"),
                  value = c(sample (c(1000L:6000L),9, replace = FALSE)),
                  stringsAsFactors = FALSE)

И код для черчения:

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

ggplot(df1) +
  geom_bar(data = filter(df1, source %in% c("a", "b")), aes(cat, value, fill = source), stat="identity", position = position_dodge()) +
  geom_point(data = filter(df1, source == "c"), aes(cat, value*5000/2000, color = source))+
  geom_line(data = filter(df1, source == "c"), aes(cat, value*5000/2000, color = source , group = source)) +
  labs(x= "categories", y= "a and b (bars)") +
  scale_y_continuous(sec.axis = sec_axis(~.*2000/5000, name = "c (line)"), 
                     labels= format_format(big.mark = ".", decimal.mark = ",", scientific = FALSE)) +
  scale_fill_manual(values= c("darkseagreen4", "darkseagreen3")) +
  geom_text(aes(label=value, x = cat, y = value), vjust = 1.5, size = 3) +
  theme_light() 

Кто-нибудь знает, как получить метки правильнопозиция?

1 Ответ

0 голосов
/ 23 мая 2018

Наличие двух разных «графиков» на одном графике заставит вас также иметь два разных способа включения меток.

Проблема размещения меток на каждой полосе решается с помощью position = position_dodge2(), ноэто не будет работать с линией.Просто добавьте для него новый geom_text.

df1 <- data.frame(cat = c("category1", "category2", "category3", 
                          "category1", "category2", "category3",
                          "category1", "category2", "category3"), 
                  source = c("a", "a", "a", "b", "b", "b", "c", "c", "c"),
                  value = c(sample (c(1000L:6000L),9, replace = FALSE)),
                  stringsAsFactors = FALSE)

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

ggplot() +
  geom_col(data = filter(df1, source %in% c("a", "b")), aes(cat, value, fill = source), position = position_dodge()) +
  geom_text(data = filter(df1, source %in% c("a", "b")), aes(cat, value,label=value, group = source), vjust = 1.5, size = 3, position = position_dodge(width = 1)) +

  geom_point(data = filter(df1, source == "c"), aes(cat, value*5000/2000, color = source)) +
  geom_line(data = filter(df1, source == "c"), aes(cat, value*5000/2000, color = source , group = source)) +
  geom_text(data = filter(df1, source == "c"), aes(cat, value*5000/2000, label=value), vjust = 1.5, size = 3) +

  labs(x= "categories", y= "a and b (bars)") +
  scale_y_continuous(sec.axis = sec_axis(~.*2000/5000, name = "c (line)"), 
                     labels= format_format(big.mark = ".", decimal.mark = ",", scientific = FALSE)) +
  scale_fill_manual(values= c("darkseagreen4", "darkseagreen3")) +
  theme_light() 

Создано в 2018-05-25 пакетом представить (v0.2.0).

...