Reordering Fascets of geochem data with tidypaleo package

Hello, Sorry I am a noob. Hopefully I produced a decent reprex. Trying to reorder the facets in this dataset. I reproduced the problematic code as prescribed by the author of this package as listed on his website, but I am getting an error. . If you can please assist.

Thank you!

p.s. any suggestions on how to produce better questions/ topics/ reprex is also appreciated.

Thank you! Thank you!

k2_xrf<-tibble::tribble(
          ~Location, ~depth, ~units, ~param, ~count, ~units,  ~age, ~units_age,
            "Kiani",     0L,   "cm",   "Si",  2984L,  "ppm", 3000L,       "BP",
            "Kiani",     2L,   "cm",   "Si",  3318L,  "ppm",    NA,         NA,
            "Kiani",     4L,   "cm",   "Si",  3296L,  "ppm",    NA,       "BP",
            "Kiani",     6L,   "cm",   "Si",  3712L,  "ppm", 5400L,       "BP",
            "Kiani",     8L,   "cm",   "Si",  4416L,  "ppm",    NA,       "BP",
            "Kiani",     0L,   "cm",    "S",  3172L,  "ppm", 3000L,       "BP",
            "Kiani",     2L,   "cm",    "S",  3411L,  "ppm",    NA,       "BP",
            "Kiani",     4L,   "cm",    "S",  3835L,  "ppm",    NA,       "BP",
            "Kiani",     6L,   "cm",    "S",  4281L,  "ppm", 5400L,       "BP",
            "Kiani",     8L,   "cm",    "S",  3896L,  "ppm",    NA,       "BP",
            "Kiani",     0L,   "cm",    "K",   325L,  "ppm", 3000L,       "BP",
            "Kiani",     2L,   "cm",    "K",    99L,  "ppm",    NA,       "BP",
            "Kiani",     4L,   "cm",    "K",   180L,  "ppm",    NA,       "BP",
            "Kiani",     6L,   "cm",    "K",   559L,  "ppm", 5400L,       "BP",
            "Kiani",     8L,   "cm",    "K",   504L,  "ppm",    NA,       "BP"
          )
#This correctly produces the plot
library(tidyverse)
library(tidypaleo)
theme_set(theme_bw(8))

k2_xrf <- ggplot(k2_xrf, aes(x = count, y = depth)) +
geom_lineh() +
scale_y_reverse() +
facet_geochem_gridh(vars(param)) +
labs(x = "Count (ppm)" , y = "Depth (mm)")

k2_xrf
#Trying to reorder the facets with the code prescribed the author 

k2_xrf %>%
  mutate(param = fct_relevel(param, "Si", "S", "K")) %>%
  ggplot(aes(x = value, y = depth)) +
  geom_lineh() +
  scale_y_reverse() +
  facet_geochem_gridh(vars(param)) +
  labs(x = "Count (ppm)" , y = "Depth (mm)")

k2_xrf

#Returns following error:
#**Error in UseMethod("mutate_") : no applicable method for 'mutate_' applied to an object of class "c('gg', 'ggplot')"**

reprex is perfect.

I hate it when this happens to me. You've clobbered k2_xrf when you did

k2_xrf <- ggplot(k2_xrf ...

meaning it is no longer a data frame but a ggplot object.

What I like to do is to create a base object

p <- ggplot(k2_xrf, aes(x = count, y = depth))

and then add the embellishments one by one

p + geom_line()
p + geom_line() + scale_y_reverse() ...

that makes it much easier to see where the error comes from.

Hello technocrat,

Thanks for your response, I see what you mean, I changed the base object for the ggplot object, but the problem is with the mutate function. I get the same error.

Error in UseMethod("mutate_") : no applicable method for 'mutate_' applied to an object of class "c('gg', 'ggplot')"

Here's the debug checklist I go through to troubleshoot messages like this.

  1. What is the function that is returning the message?
  2. What is the message complaining about?
  3. What argument to the function might be to blame?

So,

  1. mutate_
  2. doesn't work with the type of object that it was fed
  3. and it was fed
k2_xrf %>%
  mutate

So, what is k2_xrf

class(k2_xrf)

If it's not a data frame or tibble type object, naturally mutate will choke.

I bet if you just

k2_xrf

you'll see something that doesn't resemble a data frame at all

1 Like

Glad you like the tidypaleo package! I'm hoping to spruce up and improve for a workshop/publication in the next few weeks.

There's two problems here...as noted above, you've re-assigned k2_xrf, which is probably not what you meant. You've also assigned the units column twice in your input, which will result in errors when you try to pass the data frame to any function.

Here's my take on your plot (including the age-depth model, which you emailed about):

k2_xrf <- tibble::tribble(
  ~Location, ~depth, ~depth_unit, ~param, ~count, ~units,  ~age, ~units_age,
  "Kiani",     0L,   "cm",   "Si",  2984L,  "ppm", 3000L,       "BP",
  "Kiani",     2L,   "cm",   "Si",  3318L,  "ppm",    NA,         NA,
  "Kiani",     4L,   "cm",   "Si",  3296L,  "ppm",    NA,       "BP",
  "Kiani",     6L,   "cm",   "Si",  3712L,  "ppm", 5400L,       "BP",
  "Kiani",     8L,   "cm",   "Si",  4416L,  "ppm",    NA,       "BP",
  "Kiani",     0L,   "cm",    "S",  3172L,  "ppm", 3000L,       "BP",
  "Kiani",     2L,   "cm",    "S",  3411L,  "ppm",    NA,       "BP",
  "Kiani",     4L,   "cm",    "S",  3835L,  "ppm",    NA,       "BP",
  "Kiani",     6L,   "cm",    "S",  4281L,  "ppm", 5400L,       "BP",
  "Kiani",     8L,   "cm",    "S",  3896L,  "ppm",    NA,       "BP",
  "Kiani",     0L,   "cm",    "K",   325L,  "ppm", 3000L,       "BP",
  "Kiani",     2L,   "cm",    "K",    99L,  "ppm",    NA,       "BP",
  "Kiani",     4L,   "cm",    "K",   180L,  "ppm",    NA,       "BP",
  "Kiani",     6L,   "cm",    "K",   559L,  "ppm", 5400L,       "BP",
  "Kiani",     8L,   "cm",    "K",   504L,  "ppm",    NA,       "BP"
)

library(tidyverse)
library(tidypaleo)

adm <- age_depth_model(depth = c(0, 6), age = c(3000, 5400))

k2_xrf %>%
  mutate(param = fct_relevel(param, "Si", "S", "K")) %>%
  ggplot(aes(x = count, y = depth)) +
  geom_lineh() +
  scale_y_depth_age(adm, age_breaks = c(3000, 5400)) +
  facet_geochem_gridh(vars(param))

Created on 2020-03-27 by the reprex package (v0.3.0)

2 Likes

Thanks so much for posting paleolimbot and technocrat for your helpful replies!! I see the error now and cleaned up the issues and it runs great! Tidypaleo really produces such clean figures! Thanks so much for producing this package !

I did have trouble with the age-depth in my actual data set,though. There was an erroneous date from the lab results. So the 3rd date is younger than the second date.

adm <- age_depth_model(depth = c(150,450,550,900,940,980,1422), age = c(2748, 3313,2229,4419,4613,5399,5835))

Running it returns the error:
> Error: transformation for secondary axes must be monotonic

I am assuming that monotonic in some way means that the dates must be sequential. But this is a data error and I still must express it in my figure, so is there any workaround for this?

Thank you

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.