【问题标题】:How to set the x label position? [duplicate]如何设置 x 标签位置? [复制]
【发布时间】:2018-06-11 23:19:27
【问题描述】:

谁能告诉我如何调整 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))  

【问题讨论】:

  • 使用position = position_dodge(width = 0.9)) 使文本居中

标签: r ggplot2


【解决方案1】:

使用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)) 

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多