One ggplot with two different databases with different length

Hi, I am trying to put the info of two different database in one ggplot. In this case a violin_plot(). I would like to see something like this graph but with two graphics. One with the data of 2018 in the left and the other two violins from 2019 in the right. Do you think is possible if the data have different length?

My data:

library(tidyverse)
my_data_2018 <- tibble(Var_1 = c(900, 1500, 350, 1200, 750, 100,125,250,300),
                  Gender = c("W", "W", "W", "M", "M", "W", "W", "M", "W"),
                  my_weights = c(2.2, 3.1, 8.2, 4.2, 5.3, 6.8, 12, 25, 1))

my_data_2019 <- tibble(Var_2 = c(32, 21, 21, 900, 1500, 350, 1200, 750, 100,125,250,300),
                   Gender_2 = c("W", "W", "W","W", "W", "W", "M", "M", "W", "W", "M", "W"),
                   my_weights_2 = c(2.2, 3.1, 8.2, 2.2, 3.1, 8.2, 4.2, 5.3, 6.8, 12, 25, 1))

The output with one graph:

my_data_2018 %>%
  ggplot(aes(x = 0 , y = Var_1, weight = my_weights, fill = Gender))+
  geom_violin(color = "black", scale = "count") 

Rplot01

I just stuck trying to introduce the data of 2019 in the same plot

Here's one way to do it (if I understand you correctly):

#This is your data, unchanged
library(tidyverse)
my_data_2018 <- tibble(Var_1 = c(900, 1500, 350, 1200, 750, 100,125,250,300),
                       Gender = c("W", "W", "W", "M", "M", "W", "W", "M", "W"),
                       my_weights = c(2.2, 3.1, 8.2, 4.2, 5.3, 6.8, 12, 25, 1))

my_data_2019 <- tibble(Var_2 = c(32, 21, 21, 900, 1500, 350, 1200, 750, 100,125,250,300),
                       Gender_2 = c("W", "W", "W","W", "W", "W", "M", "M", "W", "W", "M", "W"),
                       my_weights_2 = c(2.2, 3.1, 8.2, 2.2, 3.1, 8.2, 4.2, 5.3, 6.8, 12, 25, 1))

#We need the variables to have the same names, so we create a new 2019 DF
new_my_data_2019 <- my_data_2019 %>% 
  rename(Var_1 = Var_2,
         Gender = Gender_2,
         my_weights = my_weights_2)

# Then we combine the data
two_year_data <- bind_rows("2018" = my_data_2018, "2019" = new_my_data_2019, .id = "year")

#And last but not least we plot it, "faceting" by year
two_year_data %>% 
ggplot(aes(x = 0 , y = Var_1, weight = my_weights, fill = Gender))+
  geom_violin(color = "black", scale = "count") +
  facet_grid(cols = vars(year))

3 Likes

Thanks! It works like a charm :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.