This is probably a really simple question, but I've been googling and experimenting for a few hours now and just can't find the answer. This question is different from the one at How to Plot a Pre-Binned Histogram In R, because that is about adjusting bin sizes rather than using a pre-computed count.
I am pulling data into R from a PostgreSQL table:
mystuff<-sqldf("select foo, count(bar) from mytable group by foo order by count desc;")
giving the following dataframe contents for mystuff
:
foo count
1 gamma 39535
2 delta 21053
3 alpha 17919
4 beta 14930
foo and bar in the database are both character strings.
str(mystuff)
'data.frame': 9 obs. of 2 variables:
$ foo: chr "alpha" "beta" "gamma" "delta" ...
$ count : num 17919 14930 39535 21053 4262 ...
What I then want to do is plot a barchart showing the frequency for each foo (I think a barchart is the right thing here, and not a histogram). But of course R insists on doing its own count of foo, which comes to 1 for each of the categories. What I want it to do is to use the count I have thoughtfully provided.
I have got it to work using the following:
mystuff<-sqldf("select foo, 1 as count from mytable;")
mystuff$foo<-as.factor(mystuff$foo)
with(mystuff, Barplot(letter, xlab="foo", ylab="Frequency"))
in other words, by setting up the dataframe with a row for each foo and a count of 1 against it (!). But surely there must be an easier way using the SQL counts. So my question is: what is this Easier Way?