Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to distinguish 4 factors in ggplot2?

Tags:

r

ggplot2

How does one distinguish 4 different factors (not using size)? Is it possible to use hollow and solid points 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 observations and y are modeling results and I want to compare different model versions across several factors. Thanks!

like image 689
Dominik Avatar asked Dec 20 '22 02:12

Dominik


1 Answers

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

Here 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)
like image 186
agstudy Avatar answered Jan 14 '23 12:01

agstudy