如何设置x标签位置?

时间:2018-01-02 08:31:30

标签: r ggplot2

谁能告诉我如何调整x标签位置? 我想把标签(df1 $ Value)放在每个酒吧的头上? 谢谢你的帮助! Here is my code

Yr <- c("2016","2017","2016","2017","2016","2017")
Type <- c("A","A","B","B","C","C")
Value <- c(73,183,160,476,11,73)
p1Data <- data.frame(Yr,Type,Value)
p1Data$Yr <- as.character(p1Data$Yr)
p1Data <- transform(p1Data, Type = factor(p1Data$Type, levels = c("A","B","C")))

library(ggplot2)

p1 <- ggplot(p1Data,aes(Type,Value,fill=Yr))+geom_bar(stat="identity",position='dodge')+ theme(axis.title.x=element_blank())+ geom_text(aes(label=Value,vjust=1.5))  

enter image description here

1 个答案:

答案 0 :(得分:0)

使用position = position_dodge(width = ?)设置闪避的宽度。不要将此参数放在aes调用中。

library(ggplot2)

ggplot(p1Data,aes(Type,Value,fill=Yr))+
  geom_bar(stat="identity",position='dodge')+ 
  theme(axis.title.x=element_blank())+ 
  geom_text(aes(label = Value), vjust = 1.5, position = position_dodge(width = 0.9)) 

enter image description here