簡體   English   中英

將標簽對齊到ggplot中的boxplot

[英]align labels to boxplot in ggplot

我有多個箱圖,我希望用上面的實際值標注。 但是,我似乎無法使它們對齊。 我最終得到了以下標簽未對齊的內容。

箱形圖 我試過修修補補

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

干杯,改變fill=variable從你的geom bar到你的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) 

結果如下: 在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM