首页 文章

x轴刻度标记从同一位置开始的不同长度的标记

提问于
浏览
0

我在ggplot2中遇到轴标签和刻度线问题 . x轴显示不同的长度类别,y轴显示个体数量 . 如何使x轴长度等级的所有刻度标记从相同位置(顶部)开始?目前较短的标签,例如(51-60)是居中的,而较长的(121-130)是在较高的位置 . 我如何安排他们,使他们从相同的高度/位置开始?我也不知道它为什么不显示我的x和y轴 Headers .

谢谢您的帮助!

ggplot(data=ALL, aes(x=Langenklasse_Zahl, y=Anz.10.ha)) + 
  geom_bar(stat="identity")+
  scale_x_continuous(name="Längenklasse")+
  scale_y_continuous(name="Anzahl Bachforellen")+
  scale_y_continuous(limits=c(0, 84))+
  scale_x_discrete(breaks=c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32"), 
  labels=c("31-40","41-50","51-60","61-70","71-80","81-90","91-100","101-110","111-120","121-130","131-140", "141-150", "151-160", "161-170", "171-180", "181-190", "191-200", "201-210", "211-220", "221-230", "231-240", "241-250", "251-260", "261-270", "271-280", "281-290", "291-300", "301-310", "311-320", "321-330","331-340", ">340"))+
 theme(axis.title.y = element_text(vjust=1.3, size=15),
    axis.text.y  = element_text(vjust=0.5, size=15),
    axis.title.x = element_text(vjust=-.5, size=15),
    axis.text.x  = element_text(angle=90,vjust=0.5, size=15))+
 ggtitle("Längendiagramm der kanalisierten Strecke im Mai 2014") + 
 theme(plot.title = element_text(lineheight=3, size=20, face="bold"))

数据:

Langenklasse_Zahl Langenklasse Anz 10公顷
1 31-40 0
2 41-50 0
3 51-60 0
4 61-70 0
5 71-80 0
6 81-90 0
7 91-100 0
8 101-110 3
9 111-120 12
10 121-130 12
11 131-140 15
12 141-150 9
13 151-160 9
14 161-170 6
15 171-180 3
16 181-190 0
17 191-200 3
18 201-210 3
19 211-220 0
20 221-230 0
21 231-240 0
22 241-250 0
23 251-260 3
24 261-270 3
25 271-280 9
26 281-290 0
27 291-300 3
28 301-310 3
29 311-320 0
30 321-330 3
31 331-340 0
32> 340 6

1 回答

  • 0

    要使x轴标签位于相同的起始位置,请将 hjust=1hjust=0 添加到 theme() elemet axis.text.x=

    + theme(axis.text.x  = element_text(angle=90,vjust=0.5, size=15,hjust=1))
    

    您的轴 Headers 不会显示,因为您必须进行 scale_x_continuous()scale_y_continuous( )调用 . 将轴 Headers 移动到相同的 scale_... 调用,例如,您提供中断,标签和限制

    + scale_y_continuous(name="Anzahl Bachforellen",limits=c(0, 84))+
    

相关问题