Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to put two variables into one ggplot

Dataset: data

Region Forest Lakes
North 30 40
West 40 100
South 12 30
Central 44 200
data %>%
  ggplot(aes(x = Region, y = Forest)) +
  geom_col() +
  facet_wrap(~Lakes)  + ggtitle("Lakes and Forest in different Regions")

Unfortunately this is not the right thing.

I would like to show for each region how much lakes and forests they have and then the nearest region in x-axis. How does it work?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Reshape to ‘long’ format with pivot_longer and then use geom_col with position='dodge'

library(dplyr)
library(tidyr)
library(ggplot2)
data %>% 
  pivot_longer(cols = Forest:Lakes) %>%
  ggplot(aes(x = Region, y = value, fill = name)) + 
  geom_col(position = 'dodge')  + 
  ggtitle("Lakes and Forest in different Regions")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading