10

I'm an absolute beginner at R, so please excuse the simplicity of this question. I'm having trouble loading a file in R and making a histogram plot from a column of data. Here's my code:

library('ggplot2')
df <- read.csv('/PATH/TO/FILE', sep=' ', head=FALSE)
vals <- df[,2]
qplot(df, data=vals, geom="histogram")

Error: ggplot2 doesn't know how to deal with data of class numeric

Can anyone show me what the problem is? Thanks in advance for the help.

6
  • 1
    You are asking qplot to plot two data frames, i.e. df and vals. You need to remove one of these. Commented Jan 15, 2013 at 13:07
  • Thanks for the help, this works with qplot. When I try the same thing with ggplot, ggplot(vals) + geom_histogram(). I still get errors. Do you know what the problem could be? Commented Jan 15, 2013 at 13:15
  • Yes, when you use ggplot you need to specify the aesthetics with aes() Commented Jan 15, 2013 at 13:15
  • 1
    ggplot(df, aes(x=vals)) + geom_histogram() looks more likely. Commented Jan 15, 2013 at 13:25
  • 2
    Have you tried replicating (all of) the results of histograms documented here: docs.ggplot2.org/current/geom_histogram.html ? Commented Jan 15, 2013 at 14:45

1 Answer 1

9

If you need frequency histogram, try this code:

ggplot() + aes(vals)+ geom_histogram(binwidth=1, colour="black", fill="white")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.