2015-01-05 38 views
0

我在ggplot2中遇到了軸標籤和刻度線問題。 x軸顯示不同的長度等級,y軸顯示個人數量。如何使x軸的長度類別的所有刻度標記標籤從相同位置(頂部)開始?目前較短的標籤例如(51-60)居中,而較長的(121-130)寫在較高的位置。我怎樣才能安排他們,讓他們在同一個高度/位置開始呢?我也不知道它爲什麼不顯示我的x和y軸標題。從同一位置開始的不同長度的x軸刻度標記

感謝您的幫助!

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 ANZ10公頃
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

+0

請添加您的數據的最小樣本。沒有一個可重複的例子,很難理解什麼是錯的,以及如何解決它。 – tonytonov

+0

我添加了1-32的額外列,因爲Langenklasse不知何故無法被我的代碼識別。使用Langenklasse而不是重命名刻度標記可能更容易。 – Katharina

+0

Langenklasse_Zahl從1-32開始,Langenklasse是實際尺寸,31-40等等,Anz 10 ha是個人數量 – Katharina

回答

0

爲了在相同的起始位置的x軸的標籤,添加hjust=1hjust=0theme() elemet axis.text.x=

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

你軸標題沒有顯示,因爲你必須scale_x_continuous()scale_y_continuous()調用。將軸的標題移動到您提供中斷,標籤和限制的相同scale_...呼叫,例如,

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

作品完美!非常感謝你 – Katharina