I need some help for a technical manipulation on R plz.
My problem : I have some observation data of bird by presence/absence in differents habitat types. I want know the sucess ratio of observation in these differents habitats according to their surface range :
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"
)
)
Here you have just data for 'habitat_bush" but in have 10 more time habitats.
Help by a colleague, we have made this function to make a ggplot of the ratio success of observation under differents area size of 'habitat_bush" :
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)
It's work very well. But the observation can depend of effort put by technicien to looking for the presence of bird. So, I have data like that :
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)
)
My R skills stop here. I want have the same previously graph but subdivided by effort_type for each modalities of habitats types, in the same graphical (like multipanel graphical). In other word I want 5 sub-graph of previous graph with 1 barplot by efforts modalities. But I have lot of data, so I would like put this processu into a function like :
plot_forest_test_2(data = data_observation, habitat_type = habitat_bush, effort = Q_effort, colour = obs)
Can you help me please ? Thanks for your help !
cdlt
