r - discrete and continuous scales on same axis in facet_grid -
i want utilize facet_grid
in ggplot2
produce 2 graphs continuous , discrete scales on same axis - vector fac
has numeric part , factor part.
toy=data.frame(type=xc("f f f f i i"),fac=c(letters[1:4],1:3,40)) ggplot(toy,aes(x=ifelse(type=="f",fac,as.numeric((fac)))))+ geom_bar()+facet_grid(.~type,scales="free")
but doesn't work because ggplot
forces both scales discrete.
as can see equal spacing of 1,2,3,40 on right-hand plot.
two types of info in 1 column not best way represent data.
changed sample info column fac
improve representation.
toy=data.frame(type=c("f", "f","f", "f", "i", "i", "i", "i"),fac=c(letters[1:4],1,5,7,40))
to unequal spaces numeric values can create 2 geom_bar()
call, each separate type (using subset=
). letters utilize info numeric info convert fac
values character , numeric. facet_grid()
ensure have 2 plots. scale_x_discrete()
work both facets - have provide desired breaks=
values.
library(ggplot2) library(plyr) ggplot(data=toy)+ geom_bar(subset=.(type=="f"),aes(x=fac),width=0.2)+ geom_bar(subset=.(type=="i"),aes(x=as.numeric(as.character(fac))))+ facet_grid(.~type,scales="free") + scale_x_discrete(breaks=c("a","b","c","d",1,5,7,40))
r ggplot2 grid-layout facet
No comments:
Post a Comment