How to distinguish 4 factors in ggplot2?

How to distinguish 4 different factors (not using size)? Can hollow and solid points be used to distinguish a variable in ggplot2?

test=data.frame(x=runif(12,0,1), y=runif(12,0,1), siteloc=as.factor(c('a','b','a','b','a','b','a','b','a','b','a','b')), modeltype=as.factor(c('q','r','s','q','r','s','q','r','s','q','r','s')), mth=c('Mar','Apr','May','Mar','Apr','May','Mar','Apr','May','Mar','Apr','May'), yr=c(2010,2011,2010,2011,2010,2011,2010,2011,2010,2011,2010,2011)) 

where x are the observations and y are the simulation results, and I want to compare different versions of the models for several factors. Thanks!

+4
source share
3 answers

I think it is very difficult to visually distinguish / compare the x and y values ​​according to 4 factors. I would use a cut, and I reduce the number of factors using interaction , for example.

Here is an example using geom_bar :

enter image description here

 set.seed(10) library(reshape2) test.m <- melt(test,measure.vars=c('x','y')) ggplot(test.m)+ geom_bar(aes(x=interaction(yr,mth),y=value, fill=variable),stat='identity',position='dodge')+ facet_grid(modeltype~siteloc) 
+5
source

You can use hollow and solid points but only with certain shapes, as described in this answer .

So this gives you fill , colour , shape and alpha as your aesthetic fit. It looks ugly, but here it is:

 ggplot(test, aes(x, y, fill=modeltype, shape=siteloc, colour=mth, alpha=factor(yr) )) + geom_point(size = 4) + scale_shape_manual(values=21:25) + scale_alpha_manual(values=c(0.35,1)) 

Ugly, but I think this is what you asked for. (I did not bother to figure out what was happening with the legend - it obviously does not display the borders on the right.)

Ugly

If you want to map a variable to some ordinary aesthetic (hollow and solid), you need to go a little further:

 test$fill.type<-ifelse(test$yr==2010,'other',as.character(test$mth)) cols<-c('red','green','blue') ggplot(test, aes(x, y, shape=modeltype, alpha=siteloc, colour=mth, fill=fill.type )) + geom_point(size = 10) + scale_shape_manual(values=21:25) + scale_alpha_manual(values=c(1,0.5)) + scale_colour_manual(values=cols) + scale_fill_manual(values=c(cols,NA)) 

Still ugly

Still ugly, but it works. I don’t know a cleaner way of matching yr with one color if it is 2010 and mth if not; I would be glad if someone showed me a cleaner way to do this. And now the manuals (legend) are completely wrong, but you can fix it manually.

+3
source

I really like to use interaction agstudy - I would probably try this first. But if you keep things unchanged, then:

4 factors can be combined with faceting and two axes. Then there are 2 x and y metrics: one option is a bubble chart with two metrics that differ in color or shape, or both (added jitter to make the figures less overlapping):

 testm = melt(test, id=c('siteloc', 'modeltype', 'mth', 'yr')) # by color ggplot(testm, aes(x=siteloc, y=modeltype, size=value, colour=variable)) + geom_point(shape=21, position="jitter") + facet_grid(mth~yr) + scale_size_area(max_size=40) + scale_shape(solid=FALSE) + theme_bw() 

Metrics distinguished by color

 #by shape testm$shape = as.factor(with(testm, ifelse(variable=='x', 21, 25))) ggplot(testm, aes(x=siteloc, y=modeltype, size=value, shape=shape)) + geom_point(position="jitter") + facet_grid(mth~yr) + scale_size_area(max_size=40) + scale_shape(solid=FALSE) + theme_bw() 

enter image description here

 # by shape and color ggplot(testm, aes(x=siteloc, y=modeltype, size=value, colour=variable, shape=shape)) + geom_point(position="jitter") + facet_grid(mth~yr) + scale_size_area(max_size=40) + scale_shape(solid=FALSE) + theme_bw() 

enter image description here

UPDATE:

This is an attempt, based on Dominic’s first comment, to show that (x, y) is above or below the 1: 1 line, and how large is x / y or y / x - the blue triangle if x / y> 1, red circle otherwise (no melt needed in this case):

 test$shape = as.factor(with(test, ifelse(x/y>1, 25, 21))) test$ratio = with(test, ifelse(x/y>1, x/y, y/x)) ggplot(test, aes(x=siteloc, y=modeltype, size=ratio, colour=shape, shape=shape)) + geom_point() + facet_grid(mth~yr) + scale_size_area(max_size=40) + scale_shape(solid=FALSE) + theme_bw() 

enter image description here

+3
source

Source: https://habr.com/ru/post/1493866/


All Articles