Home > Blockchain >  Create a Boxplot using ggplot showing a visualization from two data frames
Create a Boxplot using ggplot showing a visualization from two data frames

Time:11-29

Basically I have the following tables

Ost_Data which looks kind of like this

Stadt Langzeitarbeitslose
Hamburg 33
Koln 21

and West_Data which looks like this

Stadt Langzeitarbeitslose
Berlin 34
Frankfurt 55

now I want to plot them in a ggplot Boxplot where the x axis shows the 2 different data tables

 Ost_Data

and

West_data 

and where on the y-axis the

Langzeitarbeitslose

is shown.

Essentially I want to compare the

Langzeitarbeitslose

value from each table against eachother

I tried

ggplot(data = Ost_data, West_data)   
  geom_boxplot(aes(x = ??, y = Langzeitarbeitslose))   
  labs(x = "Ost und Westdeutschland", y = "Langzeitarbeitslosenquote")

But that doesnt work as I don't specify that I need both tables and because I only Want to show the 2 Table names being Ost_data and West_data on the X-Axis and not a value out of the data tables.

This is how it should look like in general, even though the values here are wrong. On the X-axis I only want the naes of Ost_data and West_data and on the Y-Axis the actual values of Langzeitarbeitslose:

CodePudding user response:

One option would be to add an identifier to your datasets and row bind them which could be achieved in one step using e.g. dplyr::bind_rows. The identifier column could then be mapped on x:

Ost_data <- data.frame(
  Stadt = c("Hamburg", "Koln"),
  Langzeitarbeitslose = c(33L, 21L)
)

West_data <- data.frame(
  Stadt = c("Berlin", "Frankfurt"),
  Langzeitarbeitslose = c(34L, 55L)
)

library(ggplot2)

dat <- dplyr::bind_rows(list(Ost = Ost_data, West = West_data), .id = "id")

ggplot(data = dat)  
  geom_boxplot(aes(x = id, y = Langzeitarbeitslose))  
  labs(x = "Ost und Westdeutschland", y = "Langzeitarbeitslosenquote")

  • Related