sophie.h sophie.h - 3 months ago 16
R Question

R graphic for each varaible

I want to write a function. This function should print for each variable of a data set a graphic. For each numeric variable I want to get a boxplot. For each factor variable I want to get a barplot. Furthermore, I should define a plot method for the class numeric.

I have tried so:

uebersicht <- function ( daten ){
par(ask = TRUE)
amount<-length(names(daten))
i=1
while(i<=amount){
if( (is.numeric(daten[,i])
|is.factor(daten[,i]))==T ){
plot(daten,i)
}
else{
return("The variable is neither numeric nor factor ")
}
i=i+1
}
}


And for this function I defined two plot methods for numeric and factor:

plot.numeric <- function(x,...) {
boxplot(x,main=names(x)[i])
}

plot.factor<-function(x,i, ...) {
barplot(table(x[,i]),main=names(x)[i])
}


But this function doesn't work and I don't know why this code is error.

Answer

Try this:

uebersicht <- function(daten, ask = TRUE) {
  par(ask = ask)
  plot.numeric <- function(x, nm, ...) boxplot(x, main = nm, ...)
  plot.factor <- function(x, nm, ...) barplot(table(x), main = nm, ...)
  mapply(function(x, nm) {
    if (is.factor(x) || is.numeric(x)) {
      plot(x, nm)
    } else {
      warning("unsupported type: ", sQuote(class(x)))
    }
  }, daten, names(daten))
}

The reason I put the definitions of plot.numeric and plot.factor within the function (instead of outside it as in your example) is that there are identically-named functions in the graphics namespace, and unless you intend the default behavior to change for every use even outside of uebersicht, this remapping should really only be done during the function call.

Sample usage:

set.seed(42)
par(mfrow = c(1,4))
weirdData <- data.frame(
  num_type = rexp(100),
  int_type = sample(100, size = 100, replace = TRUE),
  fac_type = sample(LETTERS[1:6], size = 100, replace = TRUE),
  char_type = sample(LETTERS[1:6], size = 100, replace = TRUE)
)
weirdData$char_type <- as.character(weirdData$char_type)
str(weirdData)
# 'data.frame': 100 obs. of  4 variables:
#  $ num_type : num  0.1983 0.6609 0.2835 0.0382 0.4732 ...
#  $ int_type : int  40 68 78 19 3 14 69 94 56 61 ...
#  $ fac_type : Factor w/ 6 levels "A","B","C","D",..: 4 1 5 1 4 4 4 5 1 2 ...
#  $ char_type: chr  "A" "E" "C" "E" ...
uebersicht(weirdData, ask = FALSE)
### ...snip... lots of output, should really be filtered or
### perhaps I should use 'invisible(mapply(...))'
# [1] "unsupported type: 'character'"

enter image description here

(Yes, I know the image has an empty fourth block ... I left it there because the naïve intention when calling the function was to plot four columns. *shrug*) The use of par(mfrow=c(1,4)) was merely for demonstration here on SO, as is the addition of the ask option to the function.