[R] Create histogram from data matrix

David Winsemius dwinsemius at comcast.net
Sat Apr 18 02:28:14 CEST 2009


Looks to me that Holtman's solution was perfect:

 > fdf <- read.table(textConnection(txt), header=T)
 > fdf
         IDdt Temp N.fish
1  200706183    5    456
2  200706183    5    765
3  200706183    4    567
4  200706183    3    876
5  200706183    3    888
6  200706183    2    111
7  200706184    8   2345
8  200706184    8    654
9  200706184    8   7786
10 200706184    7    345
11 200706184    6    234
12 200706184    6    123
 > with(fdf, hist(rep(Temp, N.fish))  )

--  
David Winsemius

On Apr 17, 2009, at 3:07 PM, Paul Warren Simonin wrote:

> Thank you all for your advice.
>  I have received some good tips, but it was suggested I write back  
> with a small simulated data set to better illustrate my needs. So,  
> currently my data frame looks something like:
>
> ID (date)  Temperature	Number of fish
> 200706183	5	456
> 200706183	5	765
> 200706183	4	567
> 200706183	3	876
> 200706183	3	888
> 200706183	2	111
> 200706184	8	2345
> 200706184	8	654
> 200706184	8	7786
> 200706184	7	345
> 200706184	6	234
> 200706184	6	123
>
>
> I need to create a plots for each ID (date) of the number of fish  
> observed at each temperature. Obviously my data frame is much  
> larger. These plots do not have to be in a specific histogram  
> format, but it seems this may be appropriate.
> Thanks for any additional advice as to how this may be done, either  
> using plot commands or reformatting my data.
>
> It seemed the ggplot2 options may be good but so far I have tried  
> qplot with no success:
>
> my most recent code looks like:
>
> qplot(temp,number of fish, geom="histogram",binwidth=1)
>
> I have tried various tweaks of this, but no success.
>
> Thanks for any advice!
> -Paul Simonin
>
>
> Quoting stephen sefick <ssefick at gmail.com>:
>
>> It would be easier if you were to make a data frame of fake values to
>> illustrate your point, so that we could just copy it into an R  
>> session
>> and see if we can't get it to work.  So, a stab in the dark would to
>> look at ggplot2 specifically the hist argument in qplot and the
>> facets, the lattice package, or reshape to change the format of your
>> data. If you want me to fool around with it use dput and take a small
>> subset of your data or make up data to be able to be copied into an R
>> session.
>> hope that helps,
>>
>> Stephen
>>
>> On Fri, Apr 17, 2009 at 10: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?
>>>
>>> Thanks a lot for any assistance or advice!
>>>
>>> Sincerely,
>>> Paul Simonin
>>>
>>>
>>> --
>>> Paul W. Simonin
>>> Graduate Research Assistant, MS Program
>>> Vermont Cooperative Fish and Wildlife Research Unit
>>> The Rubenstein School of Environment and Natural Resources
>>> University of Vermont
>>> 81 Carrigan Dr.
>>> Burlington, VT 05405
>>> Ph:802-656-3153
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>>
>> --
>> Stephen Sefick
>>
>> Let's not spend our time and resources thinking about things that are
>> so little or so large that all they really do for us is puff us up  
>> and
>> make us feel like gods.  We are mammals, and have not exhausted the
>> annoying little problems of being mammals.
>>
>> 								-K. Mullis
>>
>
>
>
> -- 
> Paul W. Simonin
> Graduate Research Assistant, MS Program
> Vermont Cooperative Fish and Wildlife Research Unit
> The Rubenstein School of Environment and Natural Resources
> University of Vermont
> 81 Carrigan Dr.
> Burlington, VT 05405
> Ph:802-656-3153
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list