[R] Creating a histogram properly

Joshua Wiley jwiley.psych at gmail.com
Mon Oct 24 20:09:47 CEST 2011


Hi Joe,

What about this?  Btw, the real key to a reproducible example is one
that we can actually work with----output of the summary of the data is
not the same as the data itself.  That makes it tricky to know what
exactly you are working with (although sometimes the original form of
the data can be intuited from the summary output).  For future
reference, providing the output of something like:

dput(head(Names))

would make it easier for us to read your data in, and then we have
exactly what you are working with and can offer advice more easily.


## Read data in
Names <- read.table(textConnection("
              V1            V2
1  First001  Last001
2  First002  Last001
3  First003  Last002
4  First004  Last003"), header = TRUE)
closeAllConnections()
# barplots of table()d results
barplot(table(Names$V2))
barplot(table(Names$V2))

Cheers!

Josh


On Mon, Oct 24, 2011 at 11:03 AM, Joe Carl <joseph.w.carl.jr at gmail.com> wrote:
> I thought I did, but I'll restate it again
>
> I have data imported from a file with first and last names
>
>> Names <- read.table("Name.txt", header = FALSE)
>> Names
>               V1            V2
> 1  First001  Last001
> 2  First002  Last001
> 3  First003  Last002
> 4  First004  Last003
>
> I can produce a summary of the Names data
>
>> Summary(Names)
>
>            V1                 V2
>  First001: 3   Last001: 9
>  First002: 3   Last002: 7
>  First003: 3   Last003: 7
> (Other): 52    (Other): 27
>
> This summary result contains the quantity of First001 entries   I want
> this put into a vector along with the quantity so I can actually plot
> it.
>
> the command you gave will not work
>
>>hist(Names$V1)
> Error in hist.default(H02) : 'x' must be numeric
>
> You could try
>>hist(table(Names$V1))
> but it produces the wrong plot
>
> table(Names$V1) is the right stuff
> but the plot produced by hist(Names) is not correct
>
>>hist(Names$V1, plot = FALSE)
> it produces
> $breaks
> [1] 1.0 1.5 2.0 2.5 3.0
>
> $counts
> [1] 34 11  0  4
>
> $intensities
> [1] 1.3877551 0.4489796 0.0000000 0.1632653
>
> $density
> [1] 1.3877551 0.4489796 0.0000000 0.1632653
>
> $mids
> [1] 1.25 1.75 2.25 2.75
>
> $xname
> [1] "pathways"
>
> $equidist
> [1] TRUE
>
> attr(,"class")
> [1] "histogram"
>
>
>
> Which is all wrong!!!!
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list