Martz - 7 months ago 44

R Question

I am trying to make boxplots for a matrix (athTp) with 6 variables (columns) but with many missing values, '

`ggplot(athTp)+geom_boxplot()`

But maybe sth I am doing wrong...

I tried also to make many box plots and after to arrange the grid, but the final plot was very small (in desired dimensions), loosing many of details.

`q1 <- ggplot(athTp,aes(x="V1", y=athTp[,1]))+ geom_boxplot()`

..continue with other 5 columns

`grid.arrange(q1,q2,q3,q4,q5,q6, ncol=6)`

ggsave("plot.pdf",plot = qq, width = 8, height = 8, units = "cm")

Do you have any ideas?

Thanks in advance!

Answer

```
# ok so your data has 6 columns like this
set.seed(666)
dat <- data.frame(matrix(runif(60,1,20),ncol=6))
names(dat) <- letters[1:6]
head(dat)
# so let's get in long format like ggplot likes
library(reshape2)
longdat <- melt(dat)
head(longdat)
# and try your plot call again specifying that we want a box plot per column
# which is now indicated by the "variable" column
# [remember you should specify the x and y axes with `aes()`]
library(ggplot2)
ggplot(longdat, aes(x=variable, y=value)) + geom_boxplot(aes(colour = variable))
```

Source (Stackoverflow)