Enrique - 1 month ago 5x

R Question

I need to create different histograms from a dataframe. Currently I am using this loop to generate individual histograms

An example:

`df<-matrix(NA,2000,5)`

df[,1]<-rnorm(2000,1,1)

df[,2]<-rnorm(2000,2,1)

df[,3]<-rnorm(2000,3,1)

df[,4]<-rnorm(2000,4,1)

df[,5]<-rnorm(2000,5,1)

df<-data.frame(df)

colnames(df) <- c("HB1", "HB2", "HB3","HB4", 'HB5')

Loop:

`out<-list()`

for (i in 1:5){ x = df[,i]

out[[i]] <- ggplot(data.frame(x), aes(x)) +

geom_histogram(aes(y=..count../sum(..count..)), fill="red",

lwd=0.9, breaks=seq(0,5,0.1), col=("black"), alpha=I(.9)) +

labs(x=expression(d["HB"]), y="Frequency")

grid.arrange(out[[i]], ncol=1)

}

The output are 5 figures like this one:

But now I would like to do a comparison overlapping all of them.

This figure is what I really want:

Thanks in advance

Answer

Reshape the data with `tidyr::gather`

then use the fill aesthetic and 'position = "identity"'

```
df2 <- tidyr::gather(df)
head(df2)
# key value
#1 HB1 0.7493090
#2 HB1 2.5475796
#3 HB1 0.7756661
#4 HB1 1.2562534
#5 HB1 0.2757356
#6 HB1 2.4831947
ggplot(df2, aes(x = value, fill = key)) +
geom_histogram(aes(y=..count../sum(..count..)), breaks=seq(0,5,0.1), alpha=.6, position = "identity") +
labs(x=expression(d["HB"]), y="Frequency")
```

To try a density plot instead, which as eipi10 points out might be easier to understand, use

```
ggplot(df2, aes(x = value, fill = key)) +
geom_density(alpha=.6) +
labs(x=expression(d["HB"]), y="Frequency")
```

Source (Stackoverflow)

Comments