ordinate axe in geom_density

47 views
Skip to first unread message

Gr Hasan

unread,
Jan 7, 2014, 10:18:17 AM1/7/14
to ggp...@googlegroups.com
hello every one,

in the plot function i have:

n= 500
x = rnorm(n)

dx = density(x)
dx$y = dx$y * n

plot(dx)

this instructions change the ordinate axe values from graphe one to the graphe two as you can see in the attached file

my question is if we can do that with ggplot2, my ggplot code is:

ggplot(dataKey,aes(PTIME_ACTUAL)) + geom_density(aes(color=STATION, fill= STATION), alpha= 0.5)

thanks for your aide

regards


graphe.png

Vivek Patil

unread,
Jan 7, 2014, 12:56:33 PM1/7/14
to Gr Hasan, ggplot2
In the case of simulated data, you could potentially do it this way. I am not sure how your specific data are though.

# Base R versions
n= 500
set.seed(123)
x = rnorm(n)
dx = density(x)
plot(dx)

dx$y = dx$y * n
plot(dx)

### ggplot2 versions

n= 500
set.seed(123)
x = rnorm(n)
dx = density(x)
x=dx$x
y=dx$y
df=data.frame(x,y)
ggplot(df,aes(x=x,y=y))+geom_line()
ggplot(df,aes(x=x,y=y*n))+geom_line()+labs(y="y")



--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Dennis Murphy

unread,
Jan 7, 2014, 4:31:43 PM1/7/14
to Gr Hasan, ggplot2
Hi:

Here's a way to plot your toy example using count as the y-axis rather
than density:

DF <- data.frame(x = rnorm(500))

library(ggplot2)
ggplot(DF, aes(x = x)) + geom_density(aes(y = ..count..), fill = "darkorange",
color = "darkorange", alpha = 0.5)

If you're plotting multiple densities in one graphics region, here's a
similar toy example:

DF2 <- data.frame(x1 = rnorm(500), x2 = rnorm(500, 1, 0.5))
library(reshape2)
DF2m <- melt(DF2)

ggplot(DF2m, aes(x = value, fill = variable)) +
geom_density(aes(y = ..count..), alpha = 0.5, color = NA)

To see why I used color = NA, try

ggplot(DF2m, aes(x = value, fill = variable, color = variable)) +
geom_density(aes(y = ..count..), alpha = 0.5)


Dennis

On Tue, Jan 7, 2014 at 7:18 AM, Gr Hasan <ghat...@gmail.com> wrote:

Gr Hasan

unread,
Jan 15, 2014, 10:21:38 AM1/15/14
to Dennis Murphy, ggplot2
Hello every one,

it works very well, thanks for all of you, and for this wonderful group

regards

Alex


2014/1/7 Dennis Murphy <djm...@gmail.com>
Reply all
Reply to author
Forward
0 new messages