histogram for data frame binned data
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
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: au). 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, ie, if 2 fishes having 34.5(au), 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!
链接地址: http://www.djcxy.com/p/30846.html上一篇: ggplot2多个地块保留旧地块
下一篇: 数据帧分箱数据的直方图