1

How does ggplot identify if a variable is continuous or discrete?

I am writing a function to identify it but ggplot does it well. What does the function that accomplishes this in ggplot look like?

I am trying to write general function, where we provide just dataframe and function should approximate what variables are discrete and what are continuous...
My basic function looks like this we provide Dataframe and maximum number of unique values

    Classify_numeric <- function(DF = DATA, n_unique) {
  for (i in 1:ncol(DF)) {
    if (is.numeric(DF[ , i])) {
      print(colnames(DF)[i])
      if (length(unique(DF[ ,i])) <= n_unique) {print("Discrete")}
    else {print("Continuous")}
    }
  }
}

Classify_numeric(A, n_unique = 30) # this parameter can be change to whatever or we can 
# write it that n_unique can be some % of lenght of Dataframe

and same function just with is.factor function

    Classify_factor <- function(DF = DATA, n_unique) {
  for (i in 1:ncol(DF)) {
    if (is.factor(DF[ , i])) {
      print(colnames(DF)[i])
      if (length(unique(DF[ ,i])) <= n_unique) {print("Klasifikovatelny")}
  else {print("NEklasifikovatelny")}
    }
  }
}



Classify_factor(A, n_unique = 20)
Thomas Kyle
  • 101
  • 1
  • 8
  • You'll want to look at the methods for `scale_type()` here: https://github.com/tidyverse/ggplot2/blob/92099706e0e009fbd58bfa83f7fc8b41deec8877/R/scale-type.R#L59 – duckmayr Sep 25 '19 at 12:42
  • ggplot2 is pretty complex. If your actual question is how to deal with specific classes (like "character", "factor", "numeric", ...) the answer is: Use `is.character`, ... and an `if` condition or use the S3 system to create specific methods for these classes. The latter is often preferable. – Roland Sep 25 '19 at 12:43
  • 1
    If the `class()` of the variable is "factor", "character", or "logical", it's often assumed to be discrete. Anything else is continuous. But it's not easy to answer such general questions. It would be easier to help if you provide specific [reproducible examples](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we can say for sure what's going on. – MrFlick Sep 25 '19 at 14:54
  • I am trying to write general function, where we provide just dataframe and function should approximate what variables are discrete and what are continuous... `enter code here` – Thomas Kyle Sep 26 '19 at 06:07

0 Answers0