Homer Jay Simpson
Homer Jay Simpson

Reputation: 1224

Stacked gglikert plot in R with unused argument

I have a data frame called df that looks like this :

stress_levels <- c("unbearable", "severe", "medium", "little", "stressless")
df <- tibble(
  var = sample(c("usa", "uk","france","germany"), 50, replace = TRUE),
  val1 = factor(sample(stress_levels, 50, replace = TRUE), levels = stress_levels)
)
df


 A tibble: 50 × 2
   var     val1      
   <chr>   <fct>     
 1 usa     little    
 2 usa     stressless
 3 usa     little    
 4 usa     severe    
 5 uk      severe    
 6 germany medium    
 7 uk      unbearable
 8 uk      stressless
 9 usa     severe    
10 germany severe    
# ℹ 40 more rows
# ℹ Use `print(n = ...)` to see more rows

I want to create a stacked gglikert plot and following the site here it says that cutoff argument set to 5 does the job. But when I ran it from my end I receive an error :

library(tidyverse)
library(ggstats)
set.seed(42)
 
 
stress_levels <- c("unbearable", "severe", "medium", "little", "stressless")
df <- tibble(
   var = sample(c("usa", "uk","france","germany"), 50, replace = TRUE),
   val1 = factor(sample(stress_levels, 50, replace = TRUE), levels = stress_levels)
 )
> df
# A tibble: 50 × 2
   var     val1      
   <chr>   <fct>     
 1 usa     little    
 2 usa     stressless
 3 usa     little    
 4 usa     severe    
 5 uk      severe    
 6 germany medium    
 7 uk      unbearable
 8 uk      stressless
 9 usa     severe    
10 germany severe    
# ℹ 40 more rows
# ℹ Use `print(n = ...)` to see more rows


df%>%mutate(id=row_number()) %>%
  pivot_longer(-c(id,  var), names_to="group")%>%
  pivot_wider(names_from= var) %>%
  select(-c(id,group))%>%
  ggstats::gglikert(., cutoff = 5)
Error in ggstats::gglikert(., cutoff = 5) : unused argument (cutoff = 5)

Why ?

Also in a stacked plot how can I present the a bar plot near to the gglikert plot with the count of countries in the same order (sorting) as the gglikert plot, in order to horizontally match the same country ?

Upvotes: 2

Views: 94

Answers (1)

Michiel Duvekot
Michiel Duvekot

Reputation: 1881

To sort the y-axis of a bar plot in the same order as in the gglikert plot, get the factor levels from the plot, (lets say we called it p1) with levels(p1[["data"]][[".question"]])

That would look like this:

library(tidyverse)
library(patchwork)

stress_levels <- c("unbearable", "severe", "medium", "little", "stressless")
df <- tibble(
  var = sample(c("usa", "uk","france","germany"), 50, replace = TRUE),
  val1 = factor(sample(stress_levels, 50, replace = TRUE), levels = stress_levels)
)

p1 <- df %>%
  mutate(id=row_number()) %>%
  pivot_longer(-c(id,  var), names_to="group") %>%
  pivot_wider(names_from= var) %>%
  select(-c(id,group)) %>%
  ggstats::gglikert(., cutoff = 5)

p2 <- df %>% 
  mutate(var = factor(var, levels = levels(p1[["data"]][[".question"]]))) %>% 
  ggplot()+
  geom_bar(aes(y = var))+
  theme_bw()

p1 + p2

Upvotes: 1

Related Questions