1

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

Jin.w.Kim
  • 599
  • 1
  • 4
  • 15

2 Answers2

1

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

  • 1
    `scale_x_reverse(limits = c(60,0), breaks = seq(0, 60, 10))` – Jon Spring May 21 '22 at 04:16
  • Hi Jon!! Thank you so much for your answer. By the way, I think 'scale_x_reverse(limits = c(60,0), breaks= seq(60, 0, by = -10))' is correct, isn't it? Many thanks!! – Jin.w.Kim May 21 '22 at 04:31
0

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)

Jin.w.Kim
  • 599
  • 1
  • 4
  • 15