Я хочу создать готовые к публикации аннотированные лесные участки для сравнения различных моделей. Как сделать так, чтобы метки располагались над соответствующими планками ошибок и не перекрывались друг с другом?
Я также обратился за помощью к сообществу RStudio 9 марта 2022 г. Вот ссылка на оригинальный пост.
Спасибо за помощь!
Вот что я пробовал:
library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
#> Warning: package 'tidyr' was built under R version 4.1.2
#> Warning: package 'readr' was built under R version 4.1.2
#> Warning: package 'dplyr' was built under R version 4.1.2
library(knitr)
#> Warning: package 'knitr' was built under R version 4.1.2
data <- tibble::tribble(
~term, ~n_obs, ~estimate, ~conf.low, ~conf.high, ~ci, ~p.value, ~group,
"A", 101L, 0.62, 0.497390282, 0.797429694, "0.50 to 0.80", 0.000123131, "One",
"A", 65L, 0.57, 0.468908017, 0.693458768, "0.47 to 0.69", 1.83138e-08, "Two",
"A", 36L, 0.81, 0.623804568, 1.064787867, "0.62 to 1.06", 0.133680272, "Three",
"B", 26L, 0.87, 0.665400224, 1.1489204, "0.67 to 1.15", 0.335220534, "One",
"B", 16L, 1.02, 0.755403541, 1.388386542, "0.76 to 1.39", 0.878076678, "Two",
"B", 10L, 0.29, 0.091804704, 0.978289216, "0.09 to 0.98", 0.045898245, "Three",
"C", 143L, 0.90, 0.749027775, 1.089930323, "0.75 to 1.09", 0.289131987, "One",
"C", 82L, 1.02, 0.82229374, 1.286815562, "0.82 to 1.29", 0.804649191, "Two",
"C", 61L, 0.61, 0.359730119, 1.036462037, "0.36 to 1.04", 0.06765433, "Three"
)
data %>% kable()
срок | n_obs | оценивать | conf.low | конф.высокая | си | р.значение | группа |
---|---|---|---|---|---|---|---|
А | 101 | 0,62 | 0,4973903 | 0,7974297 | от 0,50 до 0,80 | 0,0001231 | Один |
А | 65 | 0,57 | 0,4689080 | 0,6934588 | от 0,47 до 0,69 | 0.0000000 | Два |
А | 36 | 0,81 | 0,6238046 | 1.0647879 | от 0,62 до 1,06 | 0,1336803 | Три |
Б | 26 | 0,87 | 0,6654002 | 1.1489204 | от 0,67 до 1,15 | 0,3352205 | Один |
Б | 16 | 1,02 | 0,7554035 | 1.3883865 | от 0,76 до 1,39 | 0,8780767 | Два |
Б | 10 | 0,29 | 0,0918047 | 0,9782892 | от 0,09 до 0,98 | 0,0458982 | Три |
С | 143 | 0,90 | 0,7490278 | 1.0899303 | от 0,75 до 1,09 | 0,2891320 | Один |
С | 82 | 1,02 | 0,8222937 | 1.2868156 | от 0,82 до 1,29 | 0,8046492 | Два |
С | 61 | 0,61 | 0,3597301 | 1.0364620 | от 0,36 до 1,04 | 0,0676543 | Три |
data %>% rowwise() %>%
## plot with variable on the y axis and estimate (OR) on the x axis
ggplot(aes(x = estimate, y = term, color = group), alpha = .7, width = .7) +
## show the estimate as a point
geom_point(position = position_dodge(.9), size = 1.5) +
## add in an error bar for the confidence intervals
geom_errorbar(aes(xmin = conf.low, xmax = conf.high), position = position_dodge(.9), size = 1) +
## add in labels
geom_text(aes( label = paste0(estimate, " (", ci, ") n = ", n_obs), x = estimate, y = term),color = "black", position = position_dodge(.9), show.legend = FALSE, check_overlap = FALSE) +
ggplot2::theme_bw() +
scale_colour_grey() +
## show where OR = 1 is for reference as a dashed line
geom_vline(xintercept = 1, linetype = "dashed") +
ylab("") +
xlab("") +
theme(legend.position = "top")
Created on 2022-03-19 by the reprex package (v2.0.1)
Вам нужно добавить группирующую переменную к текстовому слою, поскольку вы удаляете его переменную уклонения, указав color = 'black'
вне aes
.
Кроме того, вы можете использовать vjust
, чтобы переместить текст над полосами:
data %>% rowwise() %>%
ggplot(aes(x = estimate, y = term, color = group), alpha = .7, width = .7) +
geom_point(position = position_dodge(.9), size = 1.5) +
geom_errorbar(aes(xmin = conf.low, xmax = conf.high),
position = position_dodge(.9), size = 1) +
geom_text(aes( label = paste0(estimate, " (", ci, ") n = ", n_obs),
x = estimate, y = term, group = group), color = "black",
position = position_dodge(0.9), vjust = -0.4,
show.legend = FALSE, check_overlap = FALSE) +
ggplot2::theme_bw() +
scale_colour_grey() +
geom_vline(xintercept = 1, linetype = "dashed") +
ylab("") +
xlab("") +
theme(legend.position = "top")
Возможно,
vjust = -1
вgeom_text
делает то, что вам нужно.