I have a back to back barplot, does anyone know how i can change the code to add a overall title and label both X axis (PM2.5 and life expectancy? Also if possible i wish to remove the lines where the continents are labeled and they overlap the Eastern Mediterranean section. Thanks. ` Barplot code and data used below if anyone can help.
g.mid<-ggplot(life_bar,aes(x=1,y=Continent)) geom_text(aes(label=Continent))
geom_segment(aes(x=0.94,xend=0.96,yend=Continent))
geom_segment(aes(x=1.04,xend=1.065,yend=Continent))
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 <- life_bar %>%
ggplot()
geom_col(data = air_pollution_bar, aes(Continent, mn), fill = 'red')
scale_x_discrete(limits = rev)
theme(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 <- air_pollution_bar %>%
ggplot()
geom_col(data = air_pollution_bar, aes(Continent, mn), fill = 'red')
theme(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()
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))
> life_bar
# A tibble: 6 × 2
Continent mn
<chr> <dbl>
1 Africa 62.7
2 Americas 74.8
3 Eastern Mediterranean 71.5
4 Europe 78.1
5 South-East Asia 71.6
6 Western Pacific 74.3
> air_pollution_bar
# A tibble: 6 × 2
Continent mn
<chr> <dbl>
1 Africa 41.1
2 Americas 19.5
3 Eastern Mediterranean 47.0
4 Europe 15.6
5 South-East Asia 37.7
6 Western Pacific 16.4
CodePudding user response:
You can use this code:
life_bar <- data.frame(Continent = c("Africa", "Americas", "Eastern Mid", "Europe", "South-East Asia", "Western Pacific"), mn = c(62.7, 74.8, 71.3, 78.1, 71.6, 74.3))
air_pollution_bar <- data.frame(Continent = c("Africa", "Americas", "Eastern Mid", "Europe", "South-East Asia", "Western Pacific"), mn = c(41.1, 19.5, 47.0, 15.6, 37.7, 16.4))
g.mid<-ggplot(life_bar,aes(x=1,y=Continent)) geom_text(aes(label=Continent))
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 <- life_bar %>%
ggplot()
geom_col(data = life_bar, aes(Continent, mn), fill = 'red')
scale_x_discrete(limits = rev)
theme(axis.title.y = element_blank(),
axis.title.x = element_text(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = unit(c(1,-1,1,0), "mm"))
scale_y_reverse() coord_flip() ylab("life expectancy")
g2 <- air_pollution_bar %>%
ggplot()
geom_col(data = air_pollution_bar, aes(Continent, mn), fill = 'red')
theme(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() ylab("2.5PM")
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), top = "Your title")
Output: