Home > Enterprise >  R reorder ggplot x axis to end with zero
R reorder ggplot x axis to end with zero

Time:10-22

I have a script

combined_co2%>%
  rename(carbon_dioxide_ppmv=co2)%>%
  ggplot(mapping =aes(x = yrBP, y = carbon_dioxide_ppmv)) 

  geom_line()->g_line
  g_line 
  annotate( geom ="curve",
            x  = 2e 05,
            y  = 400,
            xend = 280,
            yend  = 400,
            curvature = 0.3,
             arrow = arrow(length=unit(1.5, "mm")),
             alpha = 0.8, 
              colour = "#00BFC4") ->g_curve
             g_curve 
            annotate(
              geom = "text",
              x = 3e 05,
              y = 400,
              label = "2021 average:400 ppmv",
              hjust = 0.1, vjust = -0.1,
              lineheight = 0.8, 
              colour= "#00BFC4"
            )

This script produces the below chart

enter image description here

Now I am trying to flip the x-axis to get the below chart instead:

enter image description here

How can I achieve this please? I have tried multiple orderings and sorting but to no avail

CodePudding user response:

Without any data it's possible to give only a generic solution, you can try this:

library(ggplot2) 
set.seed(123)
# some fake data
df <- data.frame(x = 1900:2000, y =rnorm(101) )
ggplot(df, aes(x = x, y = y))   geom_line()    scale_x_reverse()

enter image description here

Without the scale_x_reverse():

enter image description here

  • Related