r - Two vertical bar charts with shared x-axis in ggplot2 -


i make graphs similar this

how can make graphs using r?

i have read two horizontal bar charts shared axis in ggplot2 (similar population pyramid) , can make horizontial bar charts using following r code:

  g.mid<-ggplot(tp07,aes(x=1,y=sch))+geom_text(aes(label=sch))+ geom_segment(aes(x=0.94,xend=0.96,yend=sch))+ geom_segment(aes(x=1.04,xend=1.065,yend=sch))+ ggtitle("")+    ylab(null)+    scale_x_continuous(expand=c(0,0),limits=c(0.94,1.065))+    theme(axis.title=element_blank(),          panel.grid=element_blank(),          axis.text.y=element_blank(),          axis.ticks.y=element_blank(),        panel.background=element_blank(),          axis.text.x=element_text(color=na),          axis.ticks.x=element_line(color=na),          plot.margin = unit(c(1,-1,1,-1), "mm"))  g1 <- ggplot(data = tp07, aes(x = sch, y = ans0)) +  geom_bar(stat = "identity") + ggtitle("number of student never or seldom have breakfast") +  theme(axis.title.x = element_blank(),           axis.title.y = element_blank(),           axis.text.y = element_blank(),           axis.ticks.y = element_blank(),           plot.margin = unit(c(1,-1,1,0), "mm")) +    scale_y_reverse() + coord_flip()  g2 <- ggplot(data = tp07, aes(x = sch, y = ans7)) +xlab(null)+ geom_bar(stat = "identity") + ggtitle("no. of students have breakfsat 7 days week") +  theme(axis.title.x = element_blank(), axis.title.y = element_blank(),           axis.text.y = element_blank(), axis.ticks.y = element_blank(),          plot.margin = unit(c(1,0,1,-1), "mm")) +  coord_flip()  library(gridextra) gg1 <- ggplot_gtable(ggplot_build(g1)) gg2 <- ggplot_gtable(ggplot_build(g2)) gg.mid <- ggplot_gtable(ggplot_build(g.mid))  grid.arrange(gg1,gg.mid,gg2,ncol=3,widths=c(4/9,1/9,4/9)) 

how can change horizontal vertical? , keep x-axis in middle part of graph separate 2 variables.

assuming tp07 following:

tp07 <- structure(list(sch = structure(c(1l, 2l, 3l, 4l, 4l), .label = c("sch_a",  "sch_b", "sch_c", "sch_d"), class = "factor"), ans0 = c(1, 2,  3, 4, 1), ans7 = c(3, 2, 3, 4, 1)), .names = c("sch", "ans0",  "ans7"), row.names = c(na, -5l), class = "data.frame") 

the code below job:

g.mid_ <- ggplot(tp07,aes(x=sch,y=1))+geom_text(aes(label=sch))+     geom_segment(aes(y=0.94,yend=0.96,xend=sch))+     geom_segment(aes(y=1.04,yend=1.065,xend=sch))+     ggtitle("") +     xlab(null) +     scale_y_continuous(expand=c(0,0),limits=c(0.94,1.065))+     theme(axis.title=element_blank(),           panel.grid=element_blank(),           axis.text.y=element_blank(),           axis.ticks.y=element_blank(),           panel.background=element_blank(),           axis.text.x=element_text(color=na),           axis.ticks.x=element_line(color=na),           plot.margin = unit(c(1,-1,1,-1), "mm"))   g1_ <- ggplot(data = tp07, aes(x = sch, y = ans0)) +     geom_bar(stat = "identity", position = "identity") +     theme(axis.title.x = element_blank(),           axis.text.x = element_blank(),            axis.ticks.x = element_blank(),            plot.margin = unit(c(1,-1,1,0), "mm")) +     scale_y_reverse()   g2_ <- ggplot(data = tp07, aes(x = sch, y = ans7)) +xlab(null)+     geom_bar(stat = "identity") +     theme(axis.title.x = element_blank(),            axis.text.x = element_blank(),            axis.ticks.x = element_blank(),           plot.margin = unit(c(1,0,1,-1), "mm"))    gg1 <- ggplot_gtable(ggplot_build(g1_)) gg2 <- ggplot_gtable(ggplot_build(g2_)) gg.mid <- ggplot_gtable(ggplot_build(g.mid_))  grid.arrange(gg2, gg.mid,gg1,nrow=3, heights=c(4/10,2/10,4/10)) 

the output is:

enter image description here


Comments

Popular posts from this blog

php - Auto increment employee ID -

php - isset function not working properly -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -