[英]change value of different columns in a tibble based on cutoffs from a different tibble
[英]Pull title and subtitle from 2 different columns in a nested tibble
我想使用兩個常規字符列為嵌套的小標題中的一系列圖形創建標題和字幕。 我可以使用.x和.y功能在標題中添加一列。 例如:
library(tidyverse)
df <- mtcars %>%
filter(carb %in% c(1, 2, 4)) %>%
mutate(carb = as.character(carb), gear = as.character(gear))
df_with_plots <- df %>%
group_by(carb, gear) %>%
nest() %>%
mutate(plot = map(data, function(.x) {
.x %>%
ggplot() +
geom_histogram(aes(mpg))
}))
df_with_plots
carb gear data plot
<chr> <chr> <list> <list>
1 4 4 <tibble [4 × 9]> <gg>
2 1 4 <tibble [4 × 9]> <gg>
3 1 3 <tibble [3 × 9]> <gg>
4 2 3 <tibble [4 × 9]> <gg>
5 4 3 <tibble [5 × 9]> <gg>
6 2 4 <tibble [4 × 9]> <gg>
7 2 5 <tibble [2 × 9]> <gg>
8 4 5 <tibble [1 × 9]> <gg>
figs <- map2(df_with_plots$plot, df_with_plots$gear, ~ .x + labs(title = paste("Number of gears", .y)))
figs[[1]]
這將產生一系列圖形,在標題中帶有“ Number of gears.y”的一系列圖形。 如何在標題中同時獲取齒輪和碳水化合物(前兩列)?
這是我之前寫過的東西,數據也是嵌套的數據幀。 我沒有電腦,所以無法創建您的示例。
plots_bar <- plots_bar_data %>%
mutate(plots = map2(data, Source, function(.x, .y){ .x %>%
ggplot(aes(x= .x$n, y=.x$nn)) +
geom_bar(stat="identity", fill= farver[2]) +
geom_text(aes(label=.x$nn), vjust = -.5) +
scale_y_continuous(breaks = seq(0, 45, 5)) +
scale_x_continuous(breaks = seq(0, 24, 2)) +
labs(y = "Antal dage",
x="Antal scanninger",
title = paste0(.y, ": Antal dage med antal scanninger")) +
my_theme
}))
# for(i in 1:4){
# print(plots_bar$plots[[i]])
# }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.