I made a plot with two horizontal bar charts with shared axis in ggplot2 (thanks to this post). This plot shows life expctancy in brazilian states on the left and the difference between 2013 and 1980 on the right.
Here is the Data
esperanca_estados <- read.table(text = "
ID Estado Total Diferenca
23 SC 78.1 11.5
28 DF 77.3 10.5
21 SP 77.2 11.3
19 ES 77.1 12.2
24 RS 76.9 9.1
18 MG 76.4 12.9
22 PR 76.2 12.1
20 RJ 75.2 11.1
12 RN 75.0 16.8
1 BR 74.9 12.4
25 MS 74.7 10.9
27 GO 73.7 11.4
26 MT 73.5 13.2
11 CE 73.2 14.2
7 AP 73.1 13.0
3 AC 72.9 12.6
17 BA 72.7 13.0
14 PE 72.6 15.9
8 TO 72.5 0.0
13 PB 72.3 15.3
16 SE 71.9 11.7
6 PA 71.5 10.6
4 AM 71.2 10.5
2 RO 70.7 10.8
5 RR 70.6 11.5
10 PI 70.5 11.9
15 AL 70.4 14.7
9 MA 69.7 12.2
", sep = "", header = TRUE)
I'm using this code:
library(ggplot2)
library(gridExtra)
plot_mid <- ggplot(esperanca_estados, aes(x=1,y=Estado))+
geom_text(aes(label=Estado), size=3)+
geom_segment(aes(x=0.94,xend=0.96,yend=Estado))+
geom_segment(aes(x=1.04,xend=1.065,yend=Estado))+
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"))
plot1 <- ggplot(esperanca_estados, aes(x=Estado, y=Total))+
geom_bar(stat = "identity", fill="cornflowerblue") +
ggtitle("Esperança de Vida ao Nascer") +
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"),
plot.title=element_text(size = 10))+
scale_y_reverse() + coord_flip()
plot2 <- ggplot(esperanca_estados, aes(x=Estado, y=Diferenca))+
geom_bar(stat = "identity", fill="cornflowerblue")+
ggtitle("Diferença 2013-1980")+
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"), plot.title=element_text(size = 10)) +
coord_flip()
plot_esperanca <- grid.arrange(arrangeGrob(plot1,plot_mid,plot2,
widths=c(4/9,1/9,4/9), ncol=3),
main=textGrob("Gráfico 1: Esperança de Vida ao Nascer – Estados",
gp=gpar(fontsize=12, font=2)))
And the plot the I get is similar to this one: Two horizontal bar charts with shared axis in ggplot2 (similar to population pyramid)
The Y- axis in the left plot goes from 0 to 80), but I want to limit it from 40 to 80. None of the options that I tryed have worked:
When I use ylim(c(40,80)) at the end of the code for plot1, I get a Y-axis that goes from 40 to 80, but is not reversed and no bars appears in the plot. If I use ylim(c(80,40)), I get almost the same, no bars at the plot, but the Y-axis is in the correct order.
If I use "scale_y_continuous(limits=c(40, 80))" before "scale_y_reverse()", nothing happens compared to the first plot, and I get the Warning message:
In loop_apply(n, do.ply) : Stacking not well defined when ymin != 0
Someone knows how can I limit the Y-axis in this left plot?
Thanks, Rafael.