3
votes

I am a newbie at R, and have been trying to use the 'attitude' dataset to create histograms for each of the columns.

I can achieve this manually by typing out:

par(mfrow=c(1,7)) hist(attitude$rating) hist(attitude$complaints) hist(attitude$privileges) hist(attitude$learning) hist(attitude$raises) hist(attitude$critical) hist(attitude$advance)

However, what I'd like to do is use a single function to plot all the histograms, possibly using ggplot. This is the command I used after searching on Stackoverflow:

ggplot(attitude, aes(x=variable)) + geom_histogram()

but it seems I'm doing it wrong since I get this message:

Error in eval(expr, envir, enclos) : object 'variable' not found

I will appreciate any pointers in this regard. Thank you.

1

1 Answers

3
votes

You need to convert the attitude data to long data format first - e.g., by using melt from reshape2:

attitudeM <- melt(attitude)

Then you can facet your ggplot by variable and automatically create separate histograms for each dimension.

g <- ggplot(attitudeM,aes(x=value))
g <- g + geom_histogram()
g <- g + facet_wrap(~variable)
g