can I draw a histogram that has an outline around the entire shape, but not the individual bars?

874 views
Skip to first unread message

David L

unread,
Mar 28, 2011, 3:21:18 PM3/28/11
to ggplot2
I would like to draw a histogram that just has an outline of the basic
shape, but not for each of the individual bars, sort of like a density
plot.

The reason that I am not using a density plot is because my data is
about 25% zeros and the density() function does not describe the
density at zero correctly, possibly.

Here is what I have tried

x <- c(rep(0, 25), rlnorm (100))
xdensity <- density(x, from = 0)[c('x', 'y')]

## Ideally this density plot would be okay, but it is not:
densplot <- ggplot() + geom_line(data = as.data.frame(xdensity),
aes(x=x, y=y), size = 1, color = 'grey')

##So this is almost what I want, except that I don't need all of the
vertical bars
histplot <- ggplot() + geom_histogram(aes(x=x), binwidth = 1, color =
'black', fill = 'white')

Thanks,

David

Brandon Hurr

unread,
Mar 28, 2011, 3:27:59 PM3/28/11
to David L, ggplot2
How about...
ggplot() + geom_histogram(aes(x=x), binwidth = 1, color =
+ 'NA', fill = 'white')

B
NA.png

David L

unread,
Mar 28, 2011, 6:29:46 PM3/28/11
to ggplot2
That won't do: I am trying to overlay the densities of two plots. I
want something more like the following, but without the lines where
the densities overlap.

ggplot() +
geom_histogram(aes(c(rep(0,250),rlnorm(1000))),
binwidth = 0.2, color = 'red', fill = NA, alpha = 0.5)
+
geom_histogram(aes(x=rlnorm(1:1250)),
binwidth = 0.2, color = 'blue', fill = NA, alpha = 0.5) +
coord_cartesian(xlim = c(-1, 15))
>  NA.png
> 144KViewDownload

Brian Diggs

unread,
Mar 28, 2011, 7:19:43 PM3/28/11
to David L, ggplot2
On 3/28/2011 3:29 PM, David L wrote:
> That won't do: I am trying to overlay the densities of two plots. I
> want something more like the following, but without the lines where
> the densities overlap.
>
> ggplot() +
> geom_histogram(aes(c(rep(0,250),rlnorm(1000))),
> binwidth = 0.2, color = 'red', fill = NA, alpha = 0.5)
> +
> geom_histogram(aes(x=rlnorm(1:1250)),
> binwidth = 0.2, color = 'blue', fill = NA, alpha = 0.5) +
> coord_cartesian(xlim = c(-1, 15))

Are either one of these closer to what you want? I'm not sure I know
what you mean by "without the lines where the densities overlap"

x1 <- c(rep(0, 250), rlnorm (1000))
x2 <- c(rlnorm(1250))

x <- data.frame(x1, x2)
x.long <- melt(x, measure.vars=c("x1","x2"))

ggplot(x.long, aes(x=value)) +
geom_segment(aes(x=value, xend=..x..+0.2, y=..density..,
yend=..density.., colour=variable),
stat="bin", binwidth=0.2) +
coord_cartesian(xlim = c(-1, 15))

ggplot(x.long, aes(x=value)) +
geom_step(aes(x=value, y=..density.., colour=variable),
stat="bin", binwidth=0.2) +
coord_cartesian(xlim = c(-1, 15))


> On Mar 28, 2:27 pm, Brandon Hurr<brandon.h...@gmail.com> wrote:
>> How about...
>> ggplot() + geom_histogram(aes(x=x), binwidth = 1, color =
>> + 'NA', fill = 'white')
>>
>> B
>>
>>
>>
>>
>>
>>
>>
>> On Mon, Mar 28, 2011 at 20:21, David L<dleba...@gmail.com> wrote:
>>> ggplot() + geom_histogram(aes(x=x), binwidth = 1, color =
>>> 'black', fill = 'white')
>>
>>
>>
>> NA.png
>> 144KViewDownload
>


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

David LeBauer

unread,
Mar 28, 2011, 10:02:07 PM3/28/11
to Brian Diggs, ggplot2
Brian -

Both examples are very nice.

The second one, using geom_step, is what I originally pictured, but
the geom_segment is also appealing.

Thank you very much for your help

-David

--
David LeBauer, PhD
Energy Biosciences Institute
University of Illinois Urbana-Champaign
1206 W. Gregory Drive
Urbana, IL  61801, U.S.A.

Reply all
Reply to author
Forward
0 new messages