![](/img/trans.png)
[英]Pass multiple calling arguments to a formal argument in dplyr custom function without using "..."
[英]Custom dplyr function and multiple ggplot of multiple argument
對於R plz的技術操作,我需要一些幫助。
我的問題:我有一些關於鳥類棲息地的觀察數據,它們是否存在於不同的棲息地類型中。 我想知道根據它們的表面范圍在這些不同生境中觀察的成功率:
data_observation <- data.frame(
habitat_bush = c(
0, 0, 0, 0, 10,
10, 30, 30, 30, 45,
65, 65, 65, 80, 80,
80, 90, 95, 100
),
obs = c(
"yes", "no", "no", "no", "yes",
"no", "no", "yes", "no", "yes",
"yes", "no", "yes", "no", "yes",
"yes", "yes", "yes", "yes"
)
)
在這里,您只有'habitat_bush'的數據,但還有10個以上的時間棲息地。
在同事的幫助下,我們使此功能可以對“ habitat_bush”的不同面積大小下的觀察成功率進行ggplot繪制:
library(dplyr)
library(ggplot2)
library(scales)
plot_forest_test <- function(data = NULL, habitat_type = NULL, colour = NULL) {
x <- enquo(habitat_type)
fill <- enquo(colour)
ggdata <- data %>%
select(x = !!x, fill = !!fill) %>%
mutate(
group = case_when(
x == 0 ~ "[0]",
x > 0.0001 & x < 10.0001 ~ "]0-10]",
x > 10.0001 & x < 25.0001 ~ "]10-25]",
x > 25.0001 & x < 50.0001 ~ "]25-50]",
x > 50.0001 & x < 75.0001 ~ "]50-75]",
x > 75.0001 ~ "]75- 100]"
)
) %>%
select(-x) %>%
group_by(group, fill) %>%
count() %>%
group_by(group) %>%
group_modify(~ mutate(.data = .x, freq = n / sum(n)))
ggplot(data = ggdata, mapping = aes(x = group, y = freq, fill = fill)) +
geom_bar(stat = "identity") +
scale_fill_brewer(palette = "Greens") +
scale_y_continuous(labels = scales::percent) +
theme_minimal() +
labs(x = expr(!!x), fill = expr(!!fill))
}
plot_forest_test(data = data_observation, habitat_type = habitat_bush, colour = obs)
工作很好 但是觀察可以依靠技術人員為尋找鳥類的存在而付出的努力。 所以,我有這樣的數據:
data_observation_2 <- data.frame(
superficie_essence = c(
0, 0, 0, 0, 10,
10, 30, 30, 30, 45,
65, 65, 65, 80, 80,
80, 90, 95, 100
),
obs = c(
"yes", "no", "no", "no", "yes",
"no", "no", "yes", "no", "yes",
"yes", "no", "yes", "no", "yes",
"yes", "yes", "yes", "yes"
),
effort = c(low, low, mid-low, mid-low, low, mid-low, mid-low,
mid-high, mid-high, high, mid-low, mid-low, mid-high, mid-low, mid-high, high, high, mid-high, high)
)
我的R技能到此為止。 我想要具有相同的先前圖形,但按同一圖形(如多面板圖形)中的每種棲息地類型的方式按effort_type細分。 換句話說,我想通過努力方式將前一個圖的5個子圖與1個小圖一起繪制。 但是我有很多數據,所以我想將這個processu放到像這樣的函數中:
plot_forest_test_2(data = data_observation, habitat_type = habitat_bush, effort = Q_effort, colour = obs)
你能幫我嗎 ? 謝謝你的幫助 !
cdlt
不確定性不是我的專長,尤其是當它們可能會丟失但可以嘗試一下時。 我為facet_wrap()
面項創建了一個新列,然后添加facet_wrap()
。 您也可以使用facet_grid()
。 希望能幫助到你。
plot_forest_test <- function(data = NULL, habitat_type = NULL, colour = NULL, facet = NULL) {
x <- enquo(habitat_type)
fill <- enquo(colour)
# this is new ####################
facet <- enquo(facet)
has_facet <- quo_name(facet) != "NULL"
df <-
data %>%
mutate(
x = !!x,
fill = !!fill,
facet = ""
)
if (has_facet) {
df <-
df %>%
mutate(facet = !!facet)
}
##################################
ggdata <-
df %>%
mutate(
group = case_when(
x == 0 ~ "[0]",
x > 0.0001 & x < 10.0001 ~ "]0-10]",
x > 10.0001 & x < 25.0001 ~ "]10-25]",
x > 25.0001 & x < 50.0001 ~ "]25-50]",
x > 50.0001 & x < 75.0001 ~ "]50-75]",
x > 75.0001 ~ "]75- 100]"
)
) %>%
select(-x) %>%
# adding facet here
group_by(group, fill, facet) %>%
count() %>%
group_by(group, facet) %>%
arrange(desc(fill)) %>%
mutate(
freq = n/sum(n),
# these steps set up the label placement
running_freq = cumsum(freq),
prev_freq = lag(running_freq, default = 0),
label_y = (prev_freq + running_freq)/2 ,
label_n = paste0("n = ", sum(n))
) %>%
ungroup()
# create plot w/o facet
p <-
ggplot(data = ggdata, mapping = aes(x = group, y = freq, fill = fill)) +
geom_bar(stat = "identity") +
geom_hline(yintercept = 0) +
geom_text(aes(y = -0.05, label = label_n), size = 3.5) +
#geom_text(aes(y = label_y, label = n)) +
scale_fill_brewer(palette = "Greens") +
scale_y_continuous(labels = scales::percent) +
theme(
panel.background = element_rect(fill = "white"),
panel.border = element_rect(color = "grey90", fill = NA)
) +
labs(x = expr(!!x), fill = expr(!!fill))
# add in if facet was mentioned
if (has_facet) {
p <-
p +
facet_grid(~facet)
}
# return final plot
p
}
我要對data_observation_2
進行編輯,因為字符串不在引號中,並且某些值在連字符周圍有空格,而其他值則沒有。 我使它們全都沒有空間
data_observation_2 <- data.frame(
superficie_essence = c(
0, 0, 0, 0, 10,
10, 30, 30, 30, 45,
65, 65, 65, 80, 80,
80, 90, 95, 100
),
obs = c(
"yes", "no", "no", "no", "yes",
"no", "no", "yes", "no", "yes",
"yes", "no", "yes", "no", "yes",
"yes", "yes", "yes", "yes"
),
effort = c(
"low", "low", "mid-low", "mid-low", "low", "mid-low", "mid-low",
"mid-high", "mid-high", "high", "mid-low", "mid-low",
"mid-high", "mid-low", "mid-high", "high", "high", "mid-high", "high"
)
)
)
最后的結果。 我使用fct_relevel()
將它們按順序排列。
plot_forest_test(
data = data_observation,
habitat_type = habitat_bush,
colour = obs
)
data_observation_2 %>%
mutate(effort = fct_relevel(effort, "low", "mid-low", "mid-high", "high")) %>%
plot_forest_test(
habitat_type = superficie_essence,
colour = obs,
facet = effort
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.