Shin Yu Wu
Shin Yu Wu

Reputation: 1199

Plot categorical variable

I am new in R, and I follow the instruction slides to plot:

enter image description here

The survey[["Program"]] is the categorical data column from the data frame.

> survey[["Program"]]  # returns the Program column as a vector
 [1] "PPM"   "PPM"   "PPM"   "Other" "PPM"   "PPM"   "PPM"   "Other" "PPM"   "Other" "MISM"  "PPM"   "MISM" 
[14] "Other" "PPM"   "PPM"   "PPM"   "PPM"   "PPM"   "Other" "PPM"   "MISM"  "PPM"   "PPM"   "PPM"   "MISM" 
[27] "PPM"   "Other" "Other" "PPM"   "Other"

However, when I implement plot(survey[["Program"]]), I get the error:

Error in plot.window(...) : need finite 'ylim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

I don't know why I cannot get the same result as I show in the figure.

Upvotes: 2

Views: 91

Answers (1)

Axeman
Axeman

Reputation: 35377

That plot command only works on a factor column. (Well, it works on a lot of things, but the confusion here is because of variable not being a factor.) Compare e.g.

plot(c('a', 'a', 'b', 'b', 'c'))
Error in plot.window(...) : need finite 'ylim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

with

plot(factor(c('a', 'a', 'b', 'b', 'c')))

enter image description here

You can check the classes of your columns by calling str(survey). You may have read in the data in a different way than those slides assumed.

You can either use

plot(factor(survey[["Program"]]))

Or perhaps

barplot(table(survey[["Program"]]))

to get the same result.

Upvotes: 3

Related Questions