histogram (Freq) with distribution curve

17 views
Skip to first unread message

Patrick Hausmann

unread,
Mar 5, 2009, 1:28:13 PM3/5/09
to ggplot2
Dear list,

I am trying to produce a histogram (Freq) overlaid by a distribution
curve with ggplot2. (Which seems not to be sooo clever
http://finzi.psych.upenn.edu/R/Rhelp02/archive/37166.html)
This is my code so far:

source("http://www.davidwilke.de/hausmann/data.r")
library(ggplot2)
df.mean <- round( mean(df$energie), 2)
p <- ggplot(df, aes(x = energie) )
p <- p + geom_histogram(colour = "black",
fill = alpha("grey", 0.70), binwidth = 0.25)
p <- p + scale_x_continuous(paste("Energie", df$light, df.mean,
df$co, sep = " - "), limits=c(0, 12) ) + scale_y_continuous
("Count")
p <- p + geom_vline(xintercept = df$xlow, size=0.75,
colour = alpha("blue", 0.70))
p <- p + geom_vline(xintercept = df$m, size=0.75,
colour = alpha("red", 0.70))
p <- p + geom_vline(xintercept = df$xhigh, size=0.75,
colour = alpha("blue", 0.70))
p <- p + theme_bw()
print(p)

So, how can I overlay this plot with the distribution curve?

2.
The graphic should look like this http://www.davidwilke.de/hausmann/dokument2.pdf
I don't know how to split or rearrange the plot for the textfield on
the right. I think I have to play with "grid"?...

Thanks for any help!!
Patrick

hadley wickham

unread,
Mar 9, 2009, 8:59:32 AM3/9/09
to Patrick Hausmann, ggplot2
On Thu, Mar 5, 2009 at 12:28 PM, Patrick Hausmann <c1...@uni-bremen.de> wrote:
>
> Dear list,
>
> I am trying to produce a histogram (Freq) overlaid by a distribution
> curve with ggplot2. (Which seems not to be sooo clever
> http://finzi.psych.upenn.edu/R/Rhelp02/archive/37166.html)
> This is my code so far:
>
> source("http://www.davidwilke.de/hausmann/data.r")
> library(ggplot2)
> df.mean <- round( mean(df$energie), 2)
> p <- ggplot(df, aes(x = energie) )
> p <- p + geom_histogram(colour = "black",
>     fill = alpha("grey", 0.70), binwidth = 0.25)
> p <- p + scale_x_continuous(paste("Energie", df$light, df.mean,
>     df$co, sep = " - "), limits=c(0, 12) ) + scale_y_continuous
> ("Count")
> p <- p + geom_vline(xintercept = df$xlow, size=0.75,
>     colour = alpha("blue", 0.70))
> p <- p + geom_vline(xintercept = df$m, size=0.75,
>     colour = alpha("red", 0.70))
> p <- p + geom_vline(xintercept = df$xhigh, size=0.75,
>     colour = alpha("blue", 0.70))
> p <- p + theme_bw()
> print(p)
>
> So, how can I overlay this plot with the distribution curve?

What distribution curve do you want to overlay? You might want to
have a look at stat_function.

> 2.
> The graphic should look like this http://www.davidwilke.de/hausmann/dokument2.pdf
> I don't know how to split or rearrange the plot for the textfield on
> the right. I think I have to play with "grid"?...

Yes, have a look at the polishing your plot chapter, and you may need
to get a copy of Paul Murrell's R graphics.

Hadley

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

Timothy Bates

unread,
Mar 9, 2009, 9:28:15 AM3/9/09
to ggplot2
this is off topic, but might perhaps be worht understanding to help
people use URL data as examples in questions.

if I run
> source("http://www.davidwilke.de/hausmann/data.r")
I get an error:
Error in file(file, "r", encoding = encoding) :
cannot open the connection

What looks to me a more explicit call also returns an error:
> source(file=url("http://www.davidwilke.de/hausmann/data.r"))
Error in source(file = url("http://www.davidwilke.de/hausmann/
data.r")) :
cannot open the connection


On 9 Mar 2009, at 12:59 PM, hadley wickham wrote:

>
> On Thu, Mar 5, 2009 at 12:28 PM, Patrick Hausmann <c18g@uni-

hadley wickham

unread,
Mar 9, 2009, 9:36:29 AM3/9/09
to Timothy Bates, ggplot2
On Mon, Mar 9, 2009 at 7:28 AM, Timothy Bates <timothy...@gmail.com> wrote:
>
> this is off topic, but might perhaps be worht understanding to help
> people use URL data as examples in questions.
>
> if I run
>  > source("http://www.davidwilke.de/hausmann/data.r")
> I get an error:
> Error in file(file, "r", encoding = encoding) :
>   cannot open the connection
>
> What looks to me a more explicit call also returns an error:
>  > source(file=url("http://www.davidwilke.de/hausmann/data.r"))
> Error in source(file = url("http://www.davidwilke.de/hausmann/
> data.r")) :
>   cannot open the connection

It appears that http://www.davidwilke.de/hausmann/data.r automatically
redirects to https://www.davidwilke.de/hausmann/data.r, and if you try
this url directly, you'll see the real problem:

source("https://www.davidwilke.de/hausmann/data.r")

Hadley

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

Wayne Folta

unread,
Mar 9, 2009, 9:58:27 AM3/9/09
to hadley wickham, ggplot2
Hadley,

You beat me by a minute or so...

To add a tip, at least on MacOS X (and other UNIX's with curl), you
can make this particular instance work with:

source (pipe ("curl -s 'https://www.davidwilke.de/hausmann/
data.r'"))

which uses the curl command to handle the https connection. (Your curl
has to be configured to support SSL, which I assume most installations
do.)

Wayne
Reply all
Reply to author
Forward
0 new messages