I would like to make graphs similar to this
如何使用R?
制作这样的图形我看过Two horizontal bar charts with shared axis in ggplot2 (similar to population pyramid) 他可以使用以下R代码制作horizontial bar charts:
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 a 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))
如何将其从水平更改为垂直? 我想将x轴保持在图表的中间部分,将两个变量分开。
答案 0 :(得分:1)
假设tp07
如下:
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")
下面的代码完成了这项工作:
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))
输出结果为: