繁体   English   中英

如何更改ggplot的条形图中x轴文本的位置?

[英]How to change position of x-axis text in bar graph in ggplot?

我想更改此条形图中“ Richness”一词的位置。 我希望该单词与其他标签的第一个单词的位置对齐,即放置在顶行而不是底行。

p0 <-
  ggplot(lineshift, aes(x=name, y=value, fill=factor(product))) +
  geom_bar(stat="identity", position="dodge", width=0.8) +

  geom_text(aes(x=name, y=value, ymax=value, label=round(value), 
            hjust=0.5, vjust=-0.5),
        position = position_dodge(width=0.8)) +

  scale_fill_manual(values=c("blue", "red"),
                name="Treatment",
                breaks=c(1, 2),
                labels=c("A", "B")) +

  xlab("Biodiversity") +
  ylab("Relative occurences (%)") +

  scale_y_continuous(expand=c(0.0,0.5),
                 limits=c(0, 71),
                 breaks=c(0,10,20,30,40,50,60,70),
                 labels=c("0","10","20","30","40","50","60","70")) +
  scale_x_discrete(limits=c("Biodiversity of tropical lowland rainforest", "Species composition in understorey", "Richness"),
               labels =  sapply(strwrap(as.character(lineshift$name), width=30, simplify=F), paste, collapse="\n")) +

  theme_bw() +
  theme(legend.position=c(0.9,0.9)) +
  theme(legend.title=element_blank()) +
  theme(axis.title.x = element_text(vjust=0.1,face="bold", size=16),
    axis.text.x = element_text(vjust=0.1, size=14, angle=0))+ 
  theme(axis.title.y = element_text(angle=90, vjust=0.70, face="bold", size=18),
    axis.text.y = element_text(size=14)) +
  theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) +
  theme(panel.border = element_rect(size=2, colour = "black", fill=NA, linetype=1)) +
  theme(plot.margin = unit(c(0.3,0.4,0.28,0.0),"lines"))

在此处输入图片说明

这是数据:

structure(list(product = c(1L, 1L, 1L, 2L, 2L, 2L), name = structure(c(1L, 
3L, 2L, 1L, 3L, 2L), .Label = c("Biodiversity of tropical lowland rainforest", 
"Richness", "Species composition in understorey"), class = "factor"), 
value = c(62.96, 22.22, 14.81, 60, 40, 0), treatment = structure(c(1L, 
1L, 1L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor")), .Names = c("product", 
"name", "value", "treatment"), class = "data.frame", row.names = c(NA, 
-6L))

非常感谢您的任何建议。

您可以在x轴文本的主题中更改vjust:

p0 + theme(axis.text.x=element_text(vjust=1,size=14))

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM