0

People, please can you evaluate my code and give me a hand?

I need to reverse de X-axis of my box-plot, as you can see, the x-axis is the age in Ma, R plot the values in ascending range and I need them in the reverse form (ie 65-64, 64 -63, 63-62, 62-61, 61-60). I tried with scale_y_reverse (), but no good results

Please help me.

Thanks a lot.Box-plot

Install Tidiverse install.packages("tidyverse")

Open library

library(tidyverse)

Usign qplot

qplot(data =Filogen, x = Filogen$EDAD_1, y = Filogen$AREA_SUR, fill = Filogen$EDAD_1, geom = "boxplot", ylab = 'Área (km²)', xlab = 'Edad (Ma)')

Batman
  • 13
  • 2
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Jun 17 '21 at 00:14

1 Answers1

0

A potential solution is to use the fct_rev() function from the forcats package (part of the tidyverse), for instance, using the example dataset from the palmerpenguins package:

# Load libraries
library(tidyverse)
library(palmerpenguins)

# Create a minimal reproducible example
MRE <- penguins %>% 
  na.omit()

# Plot the penguins data
qplot(data = MRE, x = species, y = bill_length_mm, fill = species, geom =  "boxplot", ylab = 'Área (km²)', xlab = 'Edad (Ma)')

example_1.png

And, using fct_rev():

# Load libraries
library(tidyverse)
library(palmerpenguins)

# Create a minimal reproducible example
MRE <- penguins %>% 
  na.omit()

# Plot the penguins data
qplot(data = MRE, x = fct_rev(species), y = bill_length_mm, fill = species, geom =  "boxplot", ylab = 'Área (km²)', xlab = 'Edad (Ma)')

example_2.png

This solution relies on "species" being a factor. In your dataset, the equivalent variable is "EDAD_1". If "EDAD_1" is not a factor, before you plot the data, change it to a factor:

Filogen$EDAD_1 <- factor(Filogen$EDAD_1)
jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • Mr jared Thank you very much, I have had this problem for several days. You answered my question and I am very grateful to you. Greetings – Batman Jun 17 '21 at 01:21
  • 1
    @jared - why did you answer this after voting to close as duplicate? dupes should just be closed, not answered. https://stackoverflow.com/help/how-to-answer – dww Jun 17 '21 at 01:35
  • I wrote an answer for it, then voted to close as duplicate, then posted the answer as I had already written it and thought it might be of use to OP - you are absolutely correct I should have looked for duplicates first and not wasted any time on it (sorry) – jared_mamrot Jun 17 '21 at 01:43