Home > OS >  Changing a side by side bar graph with centre
Changing a side by side bar graph with centre

Time:03-03

How can i change this code so that the Continent labels appear in the centre with one variable to the left and one to the right. Can this be done in one or would 2 plots have to be combined?

life_bar %>%
  ggplot()   
  geom_col(aes(Continent, mn), fill = 'blue', colour = 'black')  
  geom_col(data = air_pollution_bar, aes(Continent, mn), fill = 'red')   
  scale_x_discrete(limits = rev)   
  coord_flip()

data;

 > life_bar
 # A tibble: 6 × 2
   Continent                mn
   <chr>                 <dbl>
 1 Africa                 62.7
 2 Americas               74.8
 3 Eastern Mediterranean  71.3
 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:

library(grid)
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.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 <- air_pollution_bar %>%
  ggplot()   
  geom_col(data = air_pollution_bar, aes(Continent, mn), fill = 'red')  
  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))

CodePudding user response:

Create a new column on each data set telling the nature of the data, bind the results by row and plot.

library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

bind_rows(
  air_pollution_bar %>% mutate(data = 'air polution'),
  life_bar %>% mutate(data = 'life')
) %>%
  ggplot(aes(Continent, mn, fill = data))   
  geom_col(position = position_dodge())  
  scale_x_discrete(limits = rev)   
  scale_fill_manual(values = c(`air polution` = "red", `life` = "blue"))  
  coord_flip()

Created on 2022-03-02 by the reprex package (v2.0.1)


Edit

For a side by side bar plot, maybe something like the following.
See also this SO post.

library(ggplot2)
library(cowplot)

g1 <- ggplot(air_pollution_bar, aes(Continent, mn))   
  geom_col(position = position_dodge(), fill = "red")  
  scale_y_reverse()  
  ylab("")  
  coord_flip()  
  ggtitle("Air polution")  
  theme(
    plot.title = element_text(hjust = 0.5),
    legend.position = "left",
    axis.title.y = element_blank(),
    axis.text.y = element_text(hjust=1, vjust=0.5)
  )

g2 <- ggplot(life_bar, aes(Continent, mn))   
  geom_col(position = position_dodge(), fill = "blue")  
  ggtitle("Life")  
  coord_flip()  
  theme(
    plot.title = element_text(hjust = 0.5),
    legend.position = "left",
    axis.title.y = element_blank(),
    axis.text.y = element_blank()
  )

plot_grid(g1, g2)

Created on 2022-03-02 by the reprex package (v2.0.1)


Data

life_bar <- read.table(text = "
   Continent                mn
 1 Africa                 62.7
 2 Americas               74.8
 3 'Eastern Mediterranean'  71.3
 4 Europe                 78.1
 5 'South-East Asia'        71.6
 6 'Western Pacific'        74.3
", header = TRUE)

air_pollution_bar <- read.table(text = "
Continent                mn
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
", header = TRUE)

Created on 2022-03-02 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related