-1

Possible Duplicate:
how to define fill colours in ggplot histogram?

I want to plot (histogram) counts of fishes per length class from a data frame which summarize this information in a binned format (data.frame(...,length=length, counts=N)). I'm a new user of ggplot2, but seems It could be a good choice. Can I create this histogram from a data.frame object?

Sorry for being so brief, Here is the structure of a piece of df,

'data.frame':   416 obs. of  3 variables:
 $ specie: Factor w/ 1 level "speciesA": 1 1 1 1 1 1 1 1 1 1 ...
 $ length: num  34.5 35 35.5 36 36.5 37 37.5 38 38.5 39 ...
 $ Counts: num  2 0 0 0 1 0 1 1 0 1 ...

In this case, fishers catch 2 individuals 34.5 cm large, 1 36.5, and so on... Thank you very much for your answers

Community
  • 1
  • 1
jrs-x
  • 336
  • 1
  • 2
  • 10
  • Would you please provide a few lines of your data? – Ali Oct 05 '12 at 12:20
  • Welcome to SO, your question would be better understood if you give us a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and a sample from your data using `dput(your.data)` – Jilber Urbina Oct 05 '12 at 13:28
  • try this, but I can't give you a reasonable solution without any hint on the structure of your data from your side. Set the value for xmin and xmax. Look into the help page from ggplot2 package: ?geom_histogram – Sathish Oct 05 '12 at 15:00
  • require("ggplot2"); ggplot(data=data, aes(x=`Fishes per length`, xmin=, xmax=)) + stat_bin(binwidth = 1) – Sathish Oct 05 '12 at 15:01

1 Answers1

0

Since you did not provide a sample of your data, I have created data per your description.

Length of fishes from 34.5 to 49.5 (arbitrary units: a.u.). Min length of fishes was 34.5 and Max length of fishes was 49.5

 x <- 34.5:49.5

Number of fishes having a specific length starts at 0 and ends at 2.

 y <- 0:2

Create a sample of counts having the length of x

 Counts <- sample(y, length(x), replace=TRUE)

Create a dataframe1 that includes length and counts. Notice: The structure of df1 satisfies the data structure per your description

df1 <- data.frame(length=x, Counts = Counts)

Since you already counted the number of fishes having a specific length, you have to convert it back to the original format, i.e., if 2 fishes having 34.5(a.u), it should be converted back to two 34.5s.

df2 <- data.frame(length = rep(df1$length, df1$Counts))

Plot the data using ggplot function. Notice there are many ways to do the same plot. You may try geom_histogram to get the same plot as well.

require("ggplot2")
ggplot(data=df2, aes(x=length, xmin=34.5, xmax=49.5)) + stat_bin(binwidth=1)

Check this site for more info

Hope this helps!

Sathish
  • 12,453
  • 3
  • 41
  • 59