[R] Create histogram from data matrix

hadley wickham h.wickham at gmail.com
Fri Apr 17 19:32:51 CEST 2009


On Fri, Apr 17, 2009 at 9:59 AM, Paul Warren Simonin
<Paul.Simonin at uvm.edu> wrote:
> Hello!
>  Thanks for reading this request for assistance. I have a question regarding
> creating a histogram-like figure from data that are not currently in the
> correct format for the "hist" command.
>  Specifically, my data have been processed and are in a matrix with columns
> containing the variables of interest and separate columns containing the
> number of times this variable was observed (counts). This data frame/matrix
> is rather large (1600 rows), and there are multiple rows corresponding to
> the same variable level (e.g., "temperature=8, 5 observations" in one row,
> then the next: "temperature=8, 9 observations", and so on). In other words,
> the data are not one long vector R can read and plot as a histogram, nor are
> they condensed. My goal is to create a figure in which one axis is bins
> (e.g., temperature values) and the other is number of observations in this
> bin (e.g., number of organisms seen).
>  My question is: Is there a way R can be told to read my data to create a
> plot like that I desire? So far I have tried several options, including bar
> plots with no success.
>
>  If there is no way to do this with my data as the are currently arrange, is
> there an efficient way to re-arrange them?

In fact, this is very easy to do with ggplot2:

install.packages("ggplot2")
library(ggplot2)

qplot(value, weight = numofobservations, data = mydf, geom="histogram")

Hadley

-- 
http://had.co.nz/




More information about the R-help mailing list