有没有办法更好地将此密度图放入框架中?
Is there a way to fit this density plot into the frame better?
Dput
这是我的数据输入:
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))
问题
到目前为止,我已经编写了这段代码来制作脊密度图:
# 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")
然而,虽然大部分情节看起来还不错,但顶部似乎被框架切掉了一点:
有没有办法调整情节以使其更适合或不以这种方式被切断?或者您可以在一帧中放入多少个密度图有限制吗?
使用 scale_y_discrete(expand = expansion(mult = c(0.05, 0.7)))
增加图和轴之间的 space。 mult
参数的第二个元素控制上限:设置一个高值来说明这一点 - 设置以满足您的审美要求。
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")
由 reprex package (v2.0.1)
于 2022-02-24 创建
Dput
这是我的数据输入:
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))
问题
到目前为止,我已经编写了这段代码来制作脊密度图:
# 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")
然而,虽然大部分情节看起来还不错,但顶部似乎被框架切掉了一点:
有没有办法调整情节以使其更适合或不以这种方式被切断?或者您可以在一帧中放入多少个密度图有限制吗?
使用 scale_y_discrete(expand = expansion(mult = c(0.05, 0.7)))
增加图和轴之间的 space。 mult
参数的第二个元素控制上限:设置一个高值来说明这一点 - 设置以满足您的审美要求。
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")
由 reprex package (v2.0.1)
于 2022-02-24 创建