[英]align labels to boxplot in ggplot
I have multiple boxplots which I wish to label with the actual values above. 我有多个箱图,我希望用上面的实际值标注。 However, I can't seem to get them to align.
但是,我似乎无法使它们对齐。 I end up with something like the following where the labels are not aligned.
我最终得到了以下标签未对齐的内容。
I have tried tinkering around with
我试过修修补补
geom_text(aes(label=value), position=position_dodge(width=0.9), vjust=-1)
but to no avail. 但无济于事。 Any advice would be greatly appreciated.
任何建议将不胜感激。
My script as follows: 我的脚本如下:
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
Cheers, changing fill=variable
from your geom bar to your ggplot()
call like so (and by adjusting the horizontal with hjust= .25
: 干杯,改变
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.