выровняйте надписи на боксплоте в ggplot - PullRequest
1 голос
/ 20 марта 2019

У меня есть несколько боксов, которые я хочу обозначить фактическими значениями выше. Однако я не могу заставить их выровняться. Я получаю что-то вроде следующего, где метки не выровнены.

Boxplot Я пытался повозиться с

geom_text(aes(label=value), position=position_dodge(width=0.9), vjust=-1) 

но безрезультатно. Любой совет будет принята с благодарностью.

Мой скрипт выглядит следующим образом:

library(reshape2)
library(tidyverse)
library(dplyr)
library(ggplot2)
library(stringr)

barCount <- tibble::tribble(
  ~Year, ~Lateral.bar, ~Bar.accreted.to.island, ~Mid.channel.bar,
  1990,          105,                     134,               62,
  2010,          102,                     189,              102
)


df2 <- melt(barCount, id="Year") %>% 
  mutate(
    Year = Year %>% as.factor(),
    variable = variable %>% str_replace_all("\\.", " ")
  )

barPlot <- ggplot(df2, aes(Year,value)) +
  geom_bar(aes(fill=variable),position="dodge",stat="identity") +
  labs(y="Quantity",fill="")+
  scale_fill_manual("Legend",
                    values=c("Lateral bar"="khaki4", "Bar accreted to island"="Khaki2", 
                             "Mid channel bar"="honeydew4"))+
  geom_text(aes(label=value), position=position_dodge(width=0.9),vjust=-1) 


#modifying axis 
barPlot <- barPlot + theme(
  axis.title.x = element_blank(),
  axis.title.y = element_text(size=14),
  axis.text.x = element_text(size=14),
  legend.position="bottom" 

)

barPlot

1 Ответ

3 голосов
/ 20 марта 2019

Приветствия, меняя fill=variable с вашей панели геом на ваш ggplot() звонок, как это (и с помощью регулировки горизонтали с помощью hjust= .25:

barPlot <- ggplot(df2, aes(Year,value, fill= variable)) +
  geom_bar(position="dodge",stat="identity") +
  labs(y="Quantity",fill="")+
  scale_fill_manual("Legend",values=c("Lateral bar"="khaki4","Bar accreted to island"="Khaki2","Mid channel bar"="honeydew4"))+
  geom_text(aes(label=value),position=position_dodge(width=0.9),vjust=-1, hjust= .25) 

это приводит к: enter image description here

...