Katharina
Katharina

Reputation: 17

x axis tick mark labels of different length starting at same position

I have problems with the axis labels and tick marks in ggplot2. The x axis displays different length classes, the y axis the number of individuals. How can I make all the tick mark labels of the length classes of the x axis start at the same position (at the top)? at the moment the shorter labels e.g. (51-60) are centered, whereas the longer (121-130) ones are written at a higher position. how can I arrange them so that they start at the same level/position in height? also I do not know why it does not display my x and y axis titles.

Thanks for the help!

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"))

the data:

Langenklasse_Zahl Langenklasse Anz 10 ha
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

Upvotes: 0

Views: 1199

Answers (1)

Didzis Elferts
Didzis Elferts

Reputation: 98529

To have your x axis labels in the same starting position, add hjust=1 or hjust=0 to the theme() elemet axis.text.x=

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

Your axis titles are not displayed because you have to scale_x_continuous() and scale_y_continuous() calls. Move the titles of axis to the same scale_... call where you provide breaks, label and limits, for example,

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

Upvotes: 0

Related Questions