R - cannot use autoplot with dataframes

78 Views Asked by At

I ran into this when trying to throw together some quick plots for exploratory analysis. I wanted to use the autoplot function from ggplot2 so that I wouldn't have to take the time to specify all the parameters and such. Again, this is for exploratory analysis, not for generating plots to be shared/published.

For a quick, minimal example:

DF <- data.frame(major = c("Bio", "Chem", "Comp", "DataSci", "Maths", "STEM_other"),
                 count = c(38, 16, 19, 8, 15, 56))

... which, of course, results in the following dataframe:

> DF
       major count
1        Bio    38
2       Chem    16
3       Comp    19
4    DataSci     8
5      Maths    15
6 STEM_other    56

My hope was that autoplot would give me a simple bar chart or something, just enough for a quick look without too much trouble. When trying to use the autoplot function, I get this error:

> autoplot(DF)
Error in `autoplot()`:
! Objects of class <data.frame> are not supported by autoplot.
ℹ have you loaded the required package?
Run `rlang::last_trace()` to see where the error occurred.

It seems odd to me that dataframes would not be supported, when I would think many data sources would be organized in dataframes. Is there something I am missing?

All of my searching pointed to issues with autoplot handling time-series data, which apparently can be corrected by also loading ggfortify. This is not time-series data, so I would think loading ggfortify would not be any help in this case... I tried anyway to confirm, and I was correct (no change).

Is there some other package I need to install/load to make autoplot work? Isn't the point to be able to make quick plots without spending too much time on them, or am I totally misusing autoplot?

Thanks for any guidance.

1

There are 1 best solutions below

2
Nir Graham On

In R you can write your own functions easily (and if you want to easily reuse them throughout your projects, you can go further and maintain your own packages...)

Here I throw up a low effort auto type tool; if you at least know that the dataframe is suitable for a bar chart, having a non-numeric field, and a numeric field, then this code can attempt to identify those (first encountered are used) and throw up a plot

df1 <- data.frame(major = c("Bio", "Chem", "Comp", "DataSci", "Maths", "STEM_other"),
           count = c(38, 16, 19, 8, 15, 56))
library(tidyverse)


myauto <- function(data){
  ggplot(data=data) + 
    aes(x=!!sym(names(select(data,where(\(x)!is.numeric(x)))) |> head(1)),
        y=!!sym(names(select(data,where(\(x)is.numeric(x)))) |> head(1))) + geom_col()
  
}
myauto(df1)