CreamStat CreamStat - 1 year ago 101
R Question

All possible qplots with different categorical variables

I've made a plot of variables lsiete and lcinco with the following code:

qplot(lsiete, lcinco, data=enc, color=LENGTHE)

enter image description here

However, I also want to give colour to the scatter plot by the following factor variables to visualize it all at the same time:

> names(enc[,ind])

So, I want a panel with all the possible scatter plots with the mentioned condition.

How Can I write the code to do that?

EDIT: To be more clear,
doesnt change, instead the variables in color change

EDIT 2: To give a reproducible example. I create the following data frame with random data:

sn <- data.frame(a=rnorm(100),b=rnorm(100), cat1=sample(c('male', 'female'), 100, replace=TRUE),cat2=sample(c('U', 'AL'), 100, replace=TRUE),cat3=sample(c('AR', 'ML'), 100, replace=TRUE),cat4=sample(c('LM', 'KR'), 100, replace=TRUE))

I can create a qplot with a and b, and give colour according to cat2:


But, I want to keep a and b always, and give colour according the rest of categorical variables to have a panel of all possible scatterplots.

Answer Source

The easiest way is to reshape your data and use facetting. This will create a single plot with four panels and a shared legend.

sn2 <- tidyr::gather(sn, 'cat', 'col', cat1:cat1.1)

ggplot(sn2, aes(a, b, col = col)) + geom_point() + facet_wrap(~cat)

enter image description here

Alternatively, if you would prefer separate legends you'll need to create four plots and stitch them together, like so:

plot_fun <- function(cat) {
  ggplot(sn, aes_(~a, ~b, col = cat)) + geom_point()

plot_list <- lapply(c(~cat1, ~cat2, ~cat3, ~cat1.1), plot_fun)
cowplot::plot_grid(plotlist = plot_list, align = 'hv')

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download