cant plot a tibble - the numeric data seems to have character class for some reason

Pretty new to R, trying to plot UK ONS agriculture data, I've sliced several rows from a tibble, bind_rows them into a new tibble, transposed the new tibble (and assigned some row names at the same time). But the tibble wont plot, it seems the tibble data points are characters. Dont know how to approach this, any ideas?

  1. script I'm trying to plot:
# extraction of various prices paid by/to farmers (indexed to annual 2015 prices = 100)
all_data <- tibble(read_ods("/home/eric/projects/agri_ss/latest_API.ods", sheet = "Outputs_monthly_2011_onwards", col_names = FALSE))
all_data <- all_data[4:ncol(all_data)]
colnames(all_data) <- slice(all_data, 7)
animals_for_slaughter_or_export <- slice(all_data, 58)
animal_products <- slice(all_data, 73)
cooking_apples <- slice(all_data, 46)
onions_brown <- slice(all_data, 40)
wheat_for_bread <- slice(all_data, 16)

# plot of all monthly output prices
selection1 <- bind_rows(animals_for_slaughter_or_export, animal_products, cooking_apples, onions_brown, wheat_for_bread)
selection1 <- selection1 %>% transpose(.names = c("animals_for_slaughter_or_export", "animal_products", "cooking_apples", "onions_brown", "wheat_for_bread"))
selection1 %>% ggplot(aes(x=c(animals_for_slaughter_or_export, animal_products, cooking_apples, onions_brown, wheat_for_bread))) +
  geom_point() + geom_smooth(method = "lm", se = FALSE) +
  scale_x_date() + theme_economist() + xlab("Jan 2011 to Present") + ylab("API - indexed for 2015 = 100")
  1. structure of my tibble:
str(selection1)
tibble [5 × 1] (S3: tbl_df/tbl/data.frame)
 $ selection1:List of 5
  ..$ animals_for_slaughter_or_export:List of 113
  .. ..$ Jan-11    : chr "90.426842383010381"
  .. ..$ Feb-11    : chr "92.1175867739164" etc
.. ..$ Mar-19    : chr "103.79046017301475"
  .. .. [list output truncated]
  ..$ animal_products                :List of 113
  .. ..$ Jan-11    : chr "102.08783664646317"
  .. ..$ Feb-11    : chr "102.99277105419571" and so on...
  1. error message from plot attempt:
selection1 %>% ggplot(aes(x=c(animals_for_slaughter_or_export, animal_products, cooking_apples, onions_brown, wheat_for_bread))) +
+   geom_point() + geom_smooth(method = "lm", se = FALSE) +
+   scale_x_date() + theme_economist() + xlab("Jan 2011 to Present") + ylab("API - indexed for 2015 = 100")
Error: `data` must be a data frame, or other object coercible by `fortify()`, not a list
Run `rlang::last_error()` to see where the error occurred.

I've tried plotting even single variables of selection1 but R wont let me do this without a Y variable, and Y are simply numbers which correspond to dates. Any suggestions on a fix route?

I haven't checked your code line by line but I expect its transposing that got you into trouble with column types.
Probably you want to pivot_longer or perform transformation in another way, and not pass vectors to aes of ggplot , rather have a column in your data that corresponds to the aesthetic

1 Like

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