1

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?

Community
  • 1
  • 1
donnek
  • 221
  • 1
  • 9
  • Possible duplicate of [How to Plot a Pre-Binned Histogram In R](http://stackoverflow.com/questions/3789260/how-to-plot-a-pre-binned-histogram-in-r) – ArunK May 12 '16 at 10:23

2 Answers2

4

You can plot very esily using the following methods. check these for example.

> x <- data.frame(foo = letters[1:5],count = runif(5,1,10))
> 
> x
  foo    count
1   a 8.788269
2   b 3.832541
3   c 1.964557
4   d 9.505890
5   e 2.924173


 barplot(height = x$count,names.arg = x$foo,)

enter image description here

Or with ggplot2 package

library(ggplot2)
ggplot(x,aes(foo,count))+geom_bar(stat="identity")

enter image description here

Koundy
  • 5,265
  • 3
  • 24
  • 37
  • Cool! Thank you so much! Yes, it's really easy now you've told me, but I couldn't find that info anywhere. Thanks also to Gaurav, but this answer is simpler. – donnek May 12 '16 at 10:50
2
require(ggplot2)

d <- data.frame(foo = as.factor(seq(1:50)),count = abs(round(rnorm(50)*10)))
ggplot(data=d,aes(x=foo,y=count))+geom_bar(stat="identity")   

enter image description here

Gaurav Taneja
  • 1,084
  • 1
  • 8
  • 19