geom_density as a line (not a closed polygon)?

285 views
Skip to first unread message

Mike Lawrence

unread,
Mar 26, 2009, 8:38:43 AM3/26/09
to ggp...@googlegroups.com
Currently geom_density creates a closed polygon (plotted in red
below); is there any way to make it a single line as in the normal
density function (plotted in green below)?

a = data.frame(obs = test_data,condition = 'None')
p = ggplot(
data = a
,aes(
x = obs
)
)+geom_histogram(
aes(
y = ..density..
)
)+geom_density(
colour='red'
)+stat_function(
fun = dnorm
,args = list(
m=mean(a$obs)
,sd=sd(a$obs)
)
,colour = 'green'
)

--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University

Looking to arrange a meeting? Check my public calendar:
http://tinyurl.com/mikes-public-calendar

~ Certainty is folly... I think. ~

baptiste auguie

unread,
Mar 26, 2009, 8:52:44 AM3/26/09
to Mike Lawrence, ggp...@googlegroups.com
Hi,

it seems you can do the following,

m <- ggplot(movies, aes(x=rating))
m + stat_density(mapping=aes(ymax=1),geom="path")

I'm not quite sure about the meaning of ymax though.

Alternatively you could overwrite the bottom line with an horizontal
line of the same colour as the grid and axes (this way you can still
use a fill colour).


HTH,

baptiste
--

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag
______________________________

Mike Lawrence

unread,
Mar 26, 2009, 12:25:11 PM3/26/09
to baptiste auguie, ggp...@googlegroups.com
setting ymax to max(..density..) seems to work well:

m <- ggplot(movies, aes(x=rating))
m + stat_density(mapping=aes(ymax=max(..density..)),geom="path")

hadley wickham

unread,
Mar 26, 2009, 12:27:20 PM3/26/09
to Mike Lawrence, baptiste auguie, ggp...@googlegroups.com
The error message is a better guide to what the real problem is -
stat_density defaults to position stack which doesn't make sense for
lines. This is a better alternative:

m + stat_density(geom="path", position="identity")

Hadley
--
http://had.co.nz/
Reply all
Reply to author
Forward
0 new messages