[R] two sample histogram

Joshua Wiley jwiley.psych at gmail.com
Tue Jul 26 01:11:29 CEST 2011


Hi,

Just a few general notes.  First, please read the posting guide.  It
requests that you:
A) state your version of R (recommended by providing the results of
sessionInfo()  )
B) provide a minimal, reproducible example
C) post in plain text not HTML

I realize that you are new, but following the guide in the future will
help you get better, faster, answers and is courteous and respectful
to the people who volunteer their time (by doing as asked).  Below is
some example code (that you should be able to directly run in your
console---it loads all required packages and creates example data).

## Install required packages
install.packages("ggplot2")

## load ggplot2 package
require(ggplot2)

## make up some example data
## note that this is typically your job as a poster
set.seed(10) # so we get the same pseudorandom data
dat <- data.frame(S1 = sample(1:1000, size = 5000, TRUE),
  S2 = sample(seq(1, 1000, by = 100), 5000, TRUE))

## look at the str()ucture of the data frame, 'dat'
str(dat)
## get a simple summary
summary(dat)

## plot histograms side by side
par(mfrow = c(1, 2))
hist(dat$S1)
hist(dat$S2)

## melt data frame for ggplot2
mdat <- melt(dat)

## look at its structure and look at the first few rows
str(mdat)
head(mdat)

## basic histogram of *all* data
## hmm, not really what we want
ggplot(data = mdat, aes(value)) +
  geom_histogram()


## To tell the variables apart, we will colour the bars
## (by setting their fill colour) to which variable
ggplot(data = mdat, aes(value)) +
  geom_histogram(aes(fill = variable))

## The default position is stacked, but you said side by side
## so we will "dodge" the bars
ggplot(data = mdat, aes(value)) +
  geom_histogram(aes(fill = variable), position = "dodge")

## this is getting close, but what about the scale?
## we can transform the scale, note how the values do not change
## rather the y axis scale changes
ggplot(data = mdat, aes(value)) +
  geom_histogram(aes(fill = variable), position = "dodge") +
  scale_y_sqrt()

## achieves a similar result by changing the coordinates
ggplot(data = mdat, aes(value)) +
  geom_histogram(aes(fill = variable), position = "dodge") +
  coord_trans(ytrans = "sqrt")


Hopefully that gives you something to work with for starters.  ggplot2
is a very nice package for graphs, though it is a bit different than
traditional graphics and the structure/syntax can take a bit of time
to get used to.  Still, I would highly recommend it.

Hope this helps,

Josh


On Mon, Jul 25, 2011 at 1:24 AM, JY <jiayujiayu2008 at 163.com> wrote:
> dear all,
> i am anewcomer. i have a set of paired data, the values are great different from each other. i want to show them in histogram.
> first, how to draw each bar for corresponding paired data  side by side;
> second, how to set the scale of y axis? one is up to 100-fold to the other. ifdoubleslash  is used to omit some coordinates, the histogram will looks better.
> is there anyone that can help me?
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list