R ggplot2 Dual y-axis facet wrap, one histogram and other line -
i have 2 facet wrapped plots, p1
, p2
p1
p2
as can see, x-axis values line both plots, y-axis values differ quite drastically. overlay p2 onto p1, keeping p1 y axis on left , creating p2 y-axis on right.
this have right now, unsure of how correctly combine grobs p1 , p2.
library(ggplot2) library(gtable) library(grid) themer <- theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), panel.margin = unit(0, "lines"), strip.background = element_rect(fill="#f8f8f8")) p2 <- ggplot(normaldens, aes(y=density,x=predicted)) + geom_line(color="red") + facet_wrap(~ motif) + labs(title=paste("methylation score:",motif_f[j]),x="methylation score",y="density") + themer p1 <- ggplot(dat, aes(x=score)) + geom_histogram( binwidth = bin_width,col="red",fill="blue",alpha=0.2) + facet_wrap(~ motif) + labs(title=paste("methylation score:",motif_f[j]),x="methylation score",y="counts") + themer ###### combine grobs ####### g1 <- ggplot_gtable(ggplot_build(p1)) g2 <- ggplot_gtable(ggplot_build(p2)) combo_grob <- g2 pos <- length(combo_grob) - 1 combo_grob$grobs[[pos]] <- cbind(g1$grobs[[pos]], g2$grobs[[pos]], size = 'first') panel_num <- length(unique(df1$z)) (i in seq(panel_num)) { # grid.ls(g1$grobs[[i + 1]]) panel_grob <- getgrob(g1$grobs[[i + 1]], 'geom_point.points', grep = true, global = true) combo_grob$grobs[[i + 1]] <- addgrob(combo_grob$grobs[[i + 1]], panel_grob) } pos_a <- grep('axis_l', names(g1$grobs)) axis <- g1$grobs[pos_a] (i in seq(along = axis)) { if (i %in% c(2, 4)) { pp <- c(subset(g1$layout, name == paste0('panel-', i), se = t:r)) ax <- axis[[1]]$children[[2]] ax$widths <- rev(ax$widths) ax$grobs <- rev(ax$grobs) ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.5, "cm") ax$grobs[[2]]$x <- ax$grobs[[2]]$x - unit(1, "npc") + unit(0.8, "cm") combo_grob <- gtable_add_cols(combo_grob, g2$widths[g2$layout[pos_a[i],]$l], length(combo_grob$widths) - 1) combo_grob <- gtable_add_grob(combo_grob, ax, pp$t, length(combo_grob$widths) - 1, pp$b) } } pp <- c(subset(g1$layout, name == 'ylab', se = t:r)) ia <- which(g1$layout$name == "ylab") ga <- g1$grobs[[ia]] ga$rot <- 270 ga$x <- ga$x - unit(1, "npc") + unit(1.5, "cm") combo_grob <- gtable_add_cols(combo_grob, g2$widths[g2$layout[ia,]$l], length(combo_grob$widths) - 1) combo_grob <- gtable_add_grob(combo_grob, ga, pp$t, length(combo_grob$widths) - 1, pp$b) combo_grob$layout$clip <- "off" grid.draw(combo_grob)
and error, know has way i'm combining 2 gtables.
error in glist(list(x = 0.5, y = 0.5, width = 1, height = 1, = "centre", : 'grobs' allowed in "glist"
i don't think can second y-axis within ggplot2
, plotting both density , histogram in single plot , using bar labeling counts (instead of trying hack second y-axis). here's example (using built-in iris
dataset):
first, we'll calculate maximum values of density , count , use these create scale factors we'll use programmatically ensure histogram , density plot have same vertical scale.
library(dplyr) # find maximum value of density densmax = iris %>% group_by(species) %>% summarise(dens = max(density(sepal.length)[["y"]])) %>% filter(dens == max(dens)) # find maximum value of bin count countmax = iris %>% group_by(species, bins=cut(sepal.length, seq(floor(min(sepal.length)), ceiling(max(sepal.length)), 0.25), right=false)) %>% summarise(count=n()) %>% ungroup() %>% filter(count==max(count))
now scale histogram bars size of density plot. sf
scale factor:
ggplot(iris, aes(x=sepal.length, sf = countmax$count/densmax$dens)) + geom_histogram(fill=hcl(195,100,65), colour="grey50", binwidth=0.25) + geom_density(colour="red", aes(y=..density.. * sf)) + facet_wrap(~ species) + themer
alternatively, go in other direction, , scale density plot histogram:
# scale histogram bars size of density plot ggplot(iris, aes(x=sepal.length, sf = densmax$dens/countmax$count)) + geom_histogram(aes(y=..count..*sf), fill=hcl(195,100,65), colour="grey50", binwidth=0.25) + stat_bin(aes(label=..count.., y=..count..*0.5*sf), geom="text", size=4, color="white", binwidth=0.25) + geom_density(colour="red") + facet_wrap(~ species) + themer + labs(y="density")
Comments
Post a Comment