I have created and plotted a small dataset using ggplot2. Given my overall experimental design it was suggested to me that I reorder the columns in my graph such that the ones in the fifth/final position are moved into the first position (keeping the order of the other bars the same). Unfortunately, I have not been able to figure out how to do this.
My data looks like this:
emean_Sleep_State<-c('WAKE','SWS','REM','WAKE','SWS','REM','WAKE','SWS','REM','WAKE','SWS','REM','WAKE','SWS','REM')
emean_Treatment<-c('Disturbed_Feed','Disturbed_Feed','Disturbed_Feed','Disturbed_Foot','Disturbed_Foot','Disturbed_Foot','Disturbed_Temp','Disturbed_Temp','Disturbed_Temp','Recovery','Recovery','Recovery','UnDisturbed','UnDisturbed','UnDisturbed')
emean_Means<-c(27.68,52.64,21.36,35.38,47.96,15.59,42.28,45.84,6.03,27.56,52.2,22.36,28.62,52.84,21.01)
emean_SE<-c(3.94,2.99,3.08,3.97,3.01,3.1,3.84,2.91,3.03,2.94,2.34,2.51,5.75,2.92,4.18)
Estimated_Max_Means_emean<-emeanframe$emean_Means+emeanframe$emean_SE
Estimated_Min_Means_emean<-emeanframe$emean_Means-emeanframe$emean_SE
emeanframe<-data.frame(emean_Sleep_State,emean_Treatment,emean_Means,emean_SE,Estimated_Max_Means_emean,Estimated_Min_Means_emean)
realvalueemean<-apply(emeanframe[,c(3,5,6)],2,FUN=backtransANG)
emeanframe$realmean<-realvalueemean[,1]
emeanframe$realmaxmean<-realvalueemean[,2]
emeanframe$realminmean<-realvalueemean[,3]
Of additional note is that the function contained within is the following:
backtransANG<-function(ANG_value){
real_value<-(sin(ANG_value/(180/pi)))^2
return(real_value)
}
The above data was then plotted using the below code:
emeanplot2<-ggplot(emeanframe,
aes(x=emean_Sleep_State,
y=realmean,
fill=emean_Treatment))+
geom_col(position='dodge',
color='black')+
geom_errorbar(aes(ymin=realminmean,
ymax=realmaxmean),
color='black',width=0.2,position=position_dodge(width=0.9),linewidth=1)+
scale_x_discrete(limits=c('WAKE','SWS','REM'))+
labs(x='Sleep State',
y= 'Proportion of Behaviour',
color="Type of Night",size=14)+
ylim(0,1)+
guides(fill=guide_legend(title='Type of Night'))+
theme(axis.text.x=element_text(size=12),
axis.text.y=element_text(size=12),
axis.title.x=element_text(size=14),
axis.title.y=element_text(size=14))+
scale_fill_manual(values=c('pink','#F8766D','red','#00BA38','#619CFF'))
I thought it might be able to quickly solve this problem by altering the order of the contents in the dataset itself, moving the 'Undisturbed' terms and related values (e.g. emean_Means and emean_SE) to the front of their respective lines within the code (see below), but this did not change the location of the corresponding bars on the graph.
emean_Sleep_State<-c('WAKE','SWS','REM','WAKE','SWS','REM','WAKE','SWS','REM','WAKE','SWS','REM','WAKE','SWS','REM')
emean_Treatment<-c('UnDisturbed','UnDisturbed','UnDisturbed','Disturbed_Feed','Disturbed_Feed','Disturbed_Feed','Disturbed_Foot','Disturbed_Foot','Disturbed_Foot','Disturbed_Temp','Disturbed_Temp','Disturbed_Temp','Recovery','Recovery','Recovery')
emean_Means<-c(28.62,52.84,21.01,27.68,52.64,21.36,35.38,47.96,15.59,42.28,45.84,6.03,27.56,52.2,22.36)
emean_SE<-c(5.75,2.92,4.18,3.94,2.99,3.08,3.97,3.01,3.1,3.84,2.91,3.03,2.94,2.34,2.51)
Estimated_Max_Means_emean<-emeanframe$emean_Means+emeanframe$emean_SE
Estimated_Min_Means_emean<-emeanframe$emean_Means-emeanframe$emean_SE
emeanframe<-data.frame(emean_Sleep_State,emean_Treatment,emean_Means,emean_SE,Estimated_Max_Means_emean,Estimated_Min_Means_emean)
realvalueemean<-apply(emeanframe[,c(3,5,6)],2,FUN=backtransANG)
emeanframe$realmean<-realvalueemean[,1]
emeanframe$realmaxmean<-realvalueemean[,2]
emeanframe$realminmean<-realvalueemean[,3]
I have tried various commands such as levels, and scale_x_manual but these have either had no effect, have resulted in an error, or have overwritten my x axis.