'How to make vertical barplots facing each other in R
I am wondering if anyone could help me making a barplot similar to the one from this 
(source: nature.com)
I have been working with R but have no clue on how to make two bar plots face each other, and could not find an answer elsewhere. Any advice would be greatly appreciated.
If someone wants to try, here is some made up data.
mock_data_up <- data.frame(labels = c("label1", "label2", "label3", "label4"), values = c(0.5, 1, 0.20, 0.5))
mock_data_down <- data.frame(labels = c("labelA", "labelB", "labelC"), values = c(1, 1, 0.5))
Thank you in advance!
Solution 1:[1]
You can arrange two plots side by side like this with cowplot::plot_grid. In order to plot the bars from right to left on the second chart you need to apply some transformations using scale_y_continuous and scale_x_discrete to both change the direction of the bars, and place the axis on the right hand side.
library(cowplot)
library(ggplot2)
mock_data_up <- data.frame(labels = c("label1", "label2", "label3", "label4"), values = c(0.5, 1, 0.20, 0.5))
mock_data_down <- data.frame(labels = c("labelA", "labelB", "labelC"), values = c(1, 1, 0.5))
mock_data_up
mock_data_down
p1 <- ggplot(mock_data_up) + geom_bar(aes(x=labels,y=values),stat='identity') + coord_flip()
p2 <- ggplot(mock_data_down) + geom_bar(aes(x=labels,y=values),stat = 'identity') + coord_flip() +
scale_x_discrete(position = 'top') +
scale_y_continuous(trans = 'reverse')
plot_grid(p1,p2)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris |

