Home > front end >  How to reverse the order of x-axis (numeric) which is also applied to the data point in a graph?
How to reverse the order of x-axis (numeric) which is also applied to the data point in a graph?

Time:05-21

I'd like to ask how to reverse the order of x-axis and also the direction of graph?

cv<- rep(c("cv1","cv2"), each=5)
value<- c(50,40,30,20,10,45,38,26,22,17)
index<- rep(c(5,15,27,36,45), each=2)
dataA<- data.frame(cv, value, index)

and I generated the linear graph

ggplot(data=dataA, aes(x=index, y=value)) 
  geom_smooth(aes(fill=cv), method=lm, level=0.95, se=FALSE, linetype=1, size=0.5,
  color="Black", formula=y~x)  
  geom_point (aes(shape=cv, fill=cv), col="Black", size=3)  
  scale_x_continuous(breaks = seq(-0, 60, 10), limits = c(0,60))   
  scale_y_continuous(breaks = seq(0,70,10), limits = c(0,70))  
  labs(x="Environmental Index", y="Kernel number")  
  theme_grey(base_size=15, base_family="serif") 
  theme(legend.position= 'none',
  axis.line= element_line(size=0.5, colour="black"))  
  windows(width=5.5, height=5)

enter image description here

Here, I'd like to reverse the order of x-axis, so that 60 comes first.

I changed the code from

scale_x_continuous(breaks = seq(-0, 60, 10), limits = c(0,60))  

to

scale_x_continuous(breaks = seq(-0, 60, 10), labels = rev(seq(-0, 60, 10)), limits = c(-0, 60)) 

Then I had this graph

enter image description here

If the x-axis changed by descending order, the line direction should be changed in a positive way, but here, only x-axis order was changed.

Could you let me know how to solve this problem?

Thanks

CodePudding user response:

use scale_x_reverse(limits = c(60,0)) you can also remove your other scale_x call

CodePudding user response:

Thanks to Jahi's answer, I solved the problem.

scale_x_reverse(limits = c(60,0), breaks= seq(60, 0, by = -10))

if we use the above code, we can reverse the order of x-axis and also it can be applied to data points in the graph. To adjust unit of x-axis, put breaks= seq(60, 0, by = -10) inside scale_x_reverse(). Remember!! the unit should be a negative value (i.e. -10)

  • Related