I am having trouble getting the order correct for my data when I plot it in ggplot geom_bar
This is my script. I am not a natural coder and am struggling to get the hang of R but it produces such good results for my ecological studies. I know my script if clunky but it gets the job done (Almost!)
data<- read.csv(file.choose(), header=TRUE)
data
Boat Movement X.
1 A Never 52
2 A Rarely 19
3 A Most trips 8
4 A Every trip 22
5 B Never 0
6 B Rarely 21
7 B Most trips 14
8 B Every trip 64
9 C Never 22
10 C Rarely 33
11 C Most trips 11
12 C Every trip 33
13 D Never 22
14 D Rarely 22
15 D Most trips 11
16 D Every trip 44
a<-ggplot(data=data, aes(x=Boat, y=X., fill=Movement))
+ geom_bar(stat="identity", position=position_dodge()) +
(xlab("Boat type")) + (ylab("% Movement")) + (theme(legend.position=c(.92, .9)))
+ (scale_fill_manual(values=c("#D55E00", "#CC79A7", "#56B4E9", "#009E73")))
+ theme(axis.title.x = element_blank(), axis.text.x = element_text(size=15),
axis.title.y = element_text(size=25), axis.text.y = element_text(size=15))
+ scale_x_discrete(breaks=c("A","B","C", "D"),labels=c("L","P","K","O"))
a
This Is the piece of code I have been using and what I get is a bar plot. The trouble is that I cant order the bars within each of my 4 groups. They come out in the order 'Every trip, Most trips, Never, Rarely' within each group even tho my data are ordered 'Never, Rarely, Most trips, Every trip'. How can I get my plot to display the data in the way it is ordered in the file?
Can anyone suggest an add on patch of text to sort my order out please?
Any suggestion on how the script could be streamlined would be appreciated also.
I have used the new code suggested by @Henrik
data$Movement <- factor(data$Movement, levels = c("Never", "Rarely",
"Most_trips", "Every_trip"))
ggplot(data=data, aes(x=Boat, y=X., fill=Movement))
+ geom_bar(stat="identity", position=position_dodge())
I am still getting the order which the data is presented within the bars wrong. It is occurring in my original code and the updated code. In this output picture the bars marked with the red arrows represent the Rarely data and not the Never data, despite what the legend says, but they are in the wrong order, the purple bars should come before the turquoise despite these data not being ordered this way in the file.