Cbhihe - 9 months ago 66

R Question

I am completely new to R, and want to insert a simple *colored box legend* identifying each distribution as "data.1" and "data.2", in the following

`qplot`

`ggplot2`

`> V1 <- matrix(unlist(rnorm(200,-0.2,1)))`

> V2 <- matrix(unlist(rnorm(200,0.3,4)))

> m <- data.frame(V1,V2)

> qplot(V1, main="Observed distr.", data=m,

geom='density',xlab="x",ylab="count",fill=I('green'), alpha=I(.5)) +

geom_density(aes(V2),data=m,fill='red', alpha=I(.5))

I have found solutions for

`ggplot`

`qplot`

`geom='density'`

I would accept any solution that gives me a density plot with transparency, labelled axes, a title and a colored box legend. Thank you.

Answer

As someone put it *"ggplot likes data in the 'long' format: i.e., a column for every dimension, and a row for every observation"*. So we `melt`

the `data.frame`

```
require(ggplot2)
require(data.table)
set.seed(10) # this is so you get the same numbers from rnorm.
m <- data.frame(V1 = matrix(unlist(rnorm(200, -0.2, 1))),
V2 = matrix(unlist(rnorm(200, 0.3, 4))))
m <- melt(m) # This comes from data.table, yet, many alt. ways to achieve this
head(m)
variable value
1 V1 -0.18125383
2 V1 -0.38425254
3 V1 -1.57133055
4 V1 -0.79916772
5 V1 0.09454513
6 V1 0.18979430
ggplot(data = m, aes(value, fill = variable)) +
geom_density(alpha = 0.5)
```

Source (Stackoverflow)