r - ggplot2: specifying different scales for rows in facet layout for bar plots -
my data visualized in package ggplot2
via bar plots several (~10) facets. want first split these facets in several rows. can use function facet_grid()
or facet_wrap()
this. in minimal example data here build 8 facets in 2 rows (4x2). need adjust scales different facets, namely: first row contains data on small scale, , in second row values bigger. need have same scale data in first row compare them along row, , scale second row.
here minimal example , possible solutions.
#loading necessary libraries , example data library(dplyr) library(tidyr) library(ggplot2) trial.facets<-read.csv(text="period,xx,yy a,2,3 b,1.5,2.5 c,3.2,0.5 d,2.5,1.5 e,11,13 f,16,14 g,8,5 h,5,4") #arranging data long format omission of "period" variable trial.facets.tidied<-trial.facets %>% gather(key=newvar,value=newvalue,-period)
and plotting itself:
#first variant ggplot(trial.facets.tidied,aes(x=newvar,y=newvalue,position="dodge"))+geom_bar(stat ="identity") +facet_grid(.~period) #second variant: ggplot(trial.facets.tidied,aes(x=newvar,y=newvalue,position="dodge"))+geom_bar(stat ="identity") +facet_wrap(~period,nrow=2,scales="free")
the results first , second variants follows:
in both examples have either free scales graphs, or fixed graphs. meanwhile first row (first 4 facets) needs scaled 5, , second row - 15.
as solution use facet_grid()
function can add fake variable "row" specifies, row should corresponding letter belong. new dataset, trial.facets.row (three lines shown only) follows:
period,xx,yy,row c,3.2,0.5,1 d,2.5,1.5,1 e,11,13,2
then can perform same rearrangement long format, omitting variables "period" , "row":
trial.facets.tidied.2<-trial.facets.row %>% gather(key=newvar,value=newvalue,-period,-row)
then arrange facets along variables "row" , "period" in hope use option scales="free_y"
adjust scales across rows:
ggplot(trial.facets.tidied.2,aes(x=newvar,y=newvalue,position="dodge"))+geom_bar(stat ="identity") +facet_grid(row~period,scales="free_y")
and - surprise: problem scales solved, however, 2 groups of empty bars, , whole data again stretched across long strip:
all discovered manual pages , handbooks (usually using mpg , mtcars dataset) not consider such situation of such unwanted or dummy data
i used combination of first method (facet_wrap
) & second method (leverage on dummy variable different rows):
# create fake variable "row" trial.facets.row <- trial.facets %>% mutate(row = ifelse(period %in% c("a", "b", "c", "d"), 1, 2)) # rearrange long format trial.facets.tidied.2<-trial.facets.row %>% gather(key=newvar,value=newvalue,-period,-row) # specify maximum height each row trial.facets.tidied.3<-trial.facets.tidied.2 %>% group_by(row) %>% mutate(max.height = max(newvalue)) %>% ungroup() ggplot(trial.facets.tidied.3, aes(x=newvar, y=newvalue,position="dodge"))+ geom_bar(stat = "identity") + geom_blank(aes(y=max.height)) + # add blank geom force facets on same row same height facet_wrap(~period,nrow=2,scales="free")
note: based on reproducible example, i'm assuming plots share common ymin @ 0. if that's not case, create dummy variable min.height & add geom_blank
ggplot.
Comments
Post a Comment