Home > Software design >  Is there a way to fit this density plot into the frame better?
Is there a way to fit this density plot into the frame better?

Time:02-25

Dput

Here is the dput for my data:

work <- structure(list(Mins_Work = c(435L, 350L, 145L, 135L, 15L, 60L, 
                                     60L, 390L, 395L, 395L, 315L, 80L, 580L, 175L, 545L, 230L, 435L, 
                                     370L, 255L, 515L, 330L, 65L, 115L, 550L, 420L, 45L, 266L, 196L, 
                                     198L, 220L, 17L, 382L, 0L, 180L, 343L, 207L, 263L, 332L, 0L, 
                                     0L, 259L, 417L, 282L, 685L, 517L, 111L, 64L, 466L, 499L, 460L, 
                                     269L, 300L, 427L, 301L, 436L, 342L, 229L, 379L, 102L, 146L, NA, 
                                     94L, 345L, 73L, 204L, 512L, 113L, 135L, 458L, 493L, 552L, 108L, 
                                     335L, 395L, 508L, 546L, 396L, 159L, 325L, 747L, 650L, 377L, 461L, 
                                     669L, 186L, 220L, 410L, 708L, 409L, 515L, 413L, 166L, 451L, 660L, 
                                     177L, 192L, 191L, 461L, 637L, 297L, 601L, 586L, 270L, 479L, 0L, 
                                     480L, 397L, 174L, 111L, 0L, 610L, 332L, 345L, 423L, 160L, 611L, 
                                     0L, 345L, 550L, 324L, 427L, 505L, 632L, 560L, 230L, 495L, 235L, 
                                     522L, 654L, 465L, 377L, 260L, 572L, 612L, 594L, 624L, 237L, 0L, 
                                     38L, 409L, 634L, 292L, 706L, 399L, 568L, 0L, 694L, 298L, 616L, 
                                     553L, 581L, 423L, 636L, 623L, 338L, 345L, 521L, 438L, 504L, 600L, 
                                     616L, 656L, 285L, 474L, 688L, 278L, 383L, 535L, 363L, 470L, 457L, 
                                     303L, 123L, 363L, 329L, 513L, 524L, NA), Month_Name = c("September", 
                                                                                             "September", "September", "September", "September", "September", 
                                                                                             "September", "September", "September", "September", "September", 
                                                                                             "September", "September", "September", "September", "September", 
                                                                                             "September", "September", "September", "September", "September", 
                                                                                             "September", "September", "September", "September", "September", 
                                                                                             "September", "September", "September", "September", "October", 
                                                                                             "October", "October", "October", "October", "October", "October", 
                                                                                             "October", "October", "October", "October", "October", "October", 
                                                                                             "October", "October", "October", "October", "October", "October", 
                                                                                             "October", "October", "October", "October", "October", "October", 
                                                                                             "October", "October", "October", "October", "October", "October", 
                                                                                             "November", "November", "November", "November", "November", "November", 
                                                                                             "November", "November", "November", "November", "November", "November", 
                                                                                             "November", "November", "November", "November", "November", "November", 
                                                                                             "November", "November", "November", "November", "November", "November", 
                                                                                             "November", "November", "November", "November", "November", "November", 
                                                                                             "December", "December", "December", "December", "December", "December", 
                                                                                             "December", "December", "December", "December", "December", "December", 
                                                                                             "December", "December", "December", "December", "December", "December", 
                                                                                             "December", "December", "December", "December", "December", "December", 
                                                                                             "December", "December", "December", "December", "December", "December", 
                                                                                             "December", "January", "January", "January", "January", "January", 
                                                                                             "January", "January", "January", "January", "January", "January", 
                                                                                             "January", "January", "January", "January", "January", "January", 
                                                                                             "January", "January", "January", "January", "January", "January", 
                                                                                             "January", "January", "January", "January", "January", "January", 
                                                                                             "January", "January", "February", "February", "February", "February", 
                                                                                             "February", "February", "February", "February", "February", "February", 
                                                                                             "February", "February", "February", "February", "February", "February", 
                                                                                             "February", "February", "February", "February", "February", "February", 
                                                                                             "February", "February", "")), class = "data.frame", row.names = c(NA, 
                                                                                                                                                               -178L))

Issue

So far I have crafted this code to make a ridge density plot:

# Load libraries:
library(tidyverse)
library(ggridges)
library(patchwork)
library(viridis)
library(hrbrthemes)
library(gapminder)
theme_set(theme_bw())

# Filter data and create plot:
work %>% 
  filter(Month_Name %in% c("September",
                           "October",
                           "November",
                           "December",
                           "January",
                           "February")) %>% 
  ggplot(aes(x=Mins_Work,
           y=Month_Name,
           fill = ..x..)) 
  geom_density_ridges_gradient(scale = 3,
                               rel_min_height = 0.01,
                               alpha = .5) 
  scale_fill_viridis(option = "B") 
  labs(title = "Distribution of Recorded Work by Month",
       x="Minutes of Recorded Work",
       y="Month")

However, while the plot looks okay for the most part, the top portion seems to be cut off by the frame a bit:

enter image description here

Is there a way to adjust the plot so it fits better or isn't cut off in this way? Or is there a limit to how many density plots you can fit into one frame?

CodePudding user response:

Increase the space between the plot and the axis using scale_y_discrete(expand = expansion(mult = c(0.05, 0.7))). The second element to the mult argument controls the upper limits: set at a high value to illustrate the point - set to suit your aesthetic requirements.

theme_set(theme_bw())

# Filter data and create plot:
work %>% 
  filter(Month_Name %in% c("September",
                           "October",
                           "November",
                           "December",
                           "January",
                           "February")) %>% 
  ggplot(aes(x=Mins_Work,
             y=Month_Name,
             fill = ..x..)) 
  geom_density_ridges_gradient(scale = 3,
                               rel_min_height = 0.01,
                               alpha = .5) 
  scale_fill_viridis(option = "B") 
  scale_y_discrete(expand = expansion(mult = c(0.05, 0.7))) 
  labs(title = "Distribution of Recorded Work by Month",
       x="Minutes of Recorded Work",
       y="Month")

Created on 2022-02-24 by the reprex package (v2.0.1)

  • Related