Reputation: 11
I already browsed a lot, but unfortunately I just find vague information. I want to display a sex ratio over length classes. To achieve that I did a stacked barplot with ggplot2 (geom_bar). This is the code:
Data <- data.frame(LCnew=c(30,30,31,31,32,32,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40,40),
sex=c(1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2))
ggplot(Data, aes(x=factor(LCnew), fill=factor(sex)))+geom_bar(position="fill") + scale_x_discrete("Lengthclass [mm]") + scale_y_continuous("Proportion of sexes") + scale_fill_manual(values=cols)
cols is just a simple vector with colours in it.
This is the plot so far:
My problem is that I am not able to change the axis text. I just want text below every fifth bar, otherwise they are overlapping. I tried it with the limits and breaks command, but this just gave me an empty graph.
scale_x_continuous("Standard Length [mm]",limits=c(30,85),breaks=c(30,35,40,45,50,55,60,65,70,75,80,85))
Any suggestions?
Upvotes: 1
Views: 16760
Reputation: 83215
If you want to prevent that the labels are overlapping, there are several strategies you can follow:
1) Define the breaks:
ggplot(y, aes(x=factor(length), fill=factor(sex))) +
geom_bar(position="fill", width=0.7) +
scale_x_discrete("Standard Length [mm]", breaks=c(300000,350000,400000)) +
scale_y_continuous("Proportion of sexes", expand=c(0,0)) +
scale_fill_manual("Sex", values=c("blue","pink"), labels=c("Male","Female")) +
theme_minimal()
which gives the following plot:
2) Change the angle of the labels:
ggplot(y, aes(x=factor(length), fill=factor(sex))) +
geom_bar(position="fill", width=0.7) +
scale_x_discrete("Lengthclass [mm]", expand=c(0,0)) +
scale_y_continuous("Proportion of sexes", expand=c(0,0)) +
scale_fill_manual("Sex", values=c("blue","pink"), labels=c("Male","Female")) +
theme_minimal() +
theme(axis.text.x = element_text(angle=90, vjust = 0.5))
which gives:
3) Rotate the plot:
ggplot(y, aes(x=factor(length), fill=factor(sex))) +
geom_bar(position="fill", width=0.7) +
scale_x_discrete("Lengthclass [mm]", expand=c(0,0)) +
scale_y_continuous("Proportion of sexes", expand=c(0,0)) +
scale_fill_manual("Sex", values=c("blue","pink"), labels=c("Male","Female")) +
coord_flip() +
theme_minimal()
which gives:
Used data:
y <- structure(list(length = c(300000, 300000, 310000, 310000, 320000, 320000, 330000, 330000, 340000, 340000, 350000, 350000, 360000, 360000, 370000, 370000, 380000, 380000, 390000, 390000, 400000, 400000, 300000, 310000, 330000, 340000, 360000, 380000, 390000),
sex = c(1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 1, 2)), .Names = c("length", "sex"), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "110", "41", "71", "101", "131", "171", "201"), class = "data.frame")
Upvotes: 4
Reputation: 1027
Try adding 'labels'
scale_x_continuous("Standard Length [mm]", breaks=c(30,35,40,45,50,55,60,65,70,75,80,85),labels=c(30,35,40,45,50,55,60,65,70,75,80,85))
Upvotes: 1