r - Removing empty group spaces in barcharts of ggplot2/lattice -
this follows on q&a regarding plotting defined groups using barchart in lattice. following solution little exercise, i've realised r plots data appears in dataframe , leaves spaces between each bar, when next row of data allocated space on barchart. if @ plot you'll understand mean:
> data.frame(soexample2) study.id diagnosis level 1 1 cancer 1040.58 2 2 cancer 810.92 3 3 cancer 2087.80 4 4 cancer 3959.02 5 5 cancer 3648.48 6 6 cancer 1191.74 7 7 cancer 1156.90 8 8 cancer 2705.56 9 9 cancer 827.26 10 10 cancer 867.16 11 11 cancer 575.10 12 12 cancer 699.85 13 13 cancer 1121.86 14 14 cancer 1830.62 15 15 cancer 4203.01 16 16 cancer 874.59 17 17 cancer 1037.20 18 18 cancer 1398.56 19 19 cancer 910.49 20 20 cancer 725.60 21 21 cancer 894.05 22 22 cancer 1489.25 23 23 cancer 1518.76 24 24 cancer 821.60 25 25 cancer 530.35 26 26 cancer 1191.80 27 27 cancer 1920.12 28 28 cancer 1330.60 29 29 cancer 835.95 30 30 cancer 525.22 require(lattice) soexample2$group<-ifelse(level>median(level),1,0) barchart(level~factor(group), data=soexample2, groups=study.id)
this gives me following barchart:
similarly, can same in ggplot2
, same thing occurs:
require(ggplot2) g1<-ggplot(soexample2, aes(x=group, y=level)) g1+geom_histogram(stat="identity")+facet_grid(.~group)+labs(x="group")+labs(y="levels")
so, ideas on how remove these empty spaces?
cheers,
oliver
you can use study.id
x values , convert them factor ensure values aren't interpreted numeric. ggplot2
solution inside facet_grid() add argument scale="free"
remove empty levels.
ggplot(soexample2,aes(factor(study.id),y=level))+ geom_bar(stat="identity")+ facet_grid(.~group,scales="free")
Comments
Post a Comment