在同一图上绘制两条带置信区间的线。

huangapple go评论62阅读模式
英文:

R drawing 2 lines with confidence interval on the same graph

问题

你可以尝试将数据框的列名修正为正确的语法,同时还需要添加 aes() 函数中的映射参数。下面是修正后的代码片段:

df_hosp_adm_non_can <- data.frame(
  age_group = age_group,
  Number_of_hospitalisation = c(3.1, 3.2, 2.5, 3.3, 4.8, 5.0),
  range_days = c(5.8, 10.9, 14.6, 16.1, 21.2, 14.3)
)

# 在 ggplot 中添加 aes 映射参数
hosp_adm <- df_hosp_adm_can %>%
  mutate(Group = "Case") %>%
  bind_rows(df_hosp_adm_non_can %>% mutate(Group = "Control")) %>%
  mutate(Group = Group %>% fct_inorder())

hosp_adm %>% ggplot(aes(x = age_group, y = Number_of_hospitalisation,
  fill = Group, color = Group
)) +
  geom_line() +
  geom_ribbon(aes(
    ymin = Number_of_hospitalisation - range_days,
    ymax = Number_of_hospitalisation + range_days
  ), alpha = 0.2)

这应该可以正常绘制包含年龄组的图表,没有“aes is missing”错误。

英文:

I am trying to draw two lines on the same graph with odds ratio and confidence interval. X axis is age group and y axis is odds ratio. There are two groups: cases and control

This is what I tried so far

age_group &lt;- c(&quot;18-30&quot;, &quot;30-45&quot;, &quot;45-60&quot;, &quot;60-75&quot;, &quot;75-90&quot;, &quot;90+&quot;)

df_hosp_adm_can &lt;- data.frame(
  age_group = 1:length(age_group),
  Number_of_hospitalisation = c(3.1, 11.9, 9.6, 9, 93, 8.9),
  range_days = c(4.3, 19.6, 29.5, 24.3, 25.3, 25.2)
)

df_hosp_adm_non_can &lt;- data.frame(
  age_group = 1:length(age_group),
  Number_of_hospitalisation =
    c(3.1, 3.2, 2.5, 3.3, 4.8, 5.0),
  range_days =
    c(5.8, 10.9, 14.6, 16.1, 21.2, 14.3)
)

hosp_adm &lt;- df_hosp_adm_can %&gt;%
  mutate(Group = &quot;Case&quot;) %&gt;%
  bind_rows(df_hosp_adm_non_can %&gt;% mutate(Group = &quot;Control&quot;)) %&gt;%
  mutate(Group = Group %&gt;% fct_inorder())

hosp_adm %&gt;% ggplot(aes(age_group, Number_of_hospitalisation,
  fill = Group, color = Group
)) +
  geom_line() +
  geom_ribbon(aes(
    ymin = Number_of_hospitalisation - range_days,
    ymax = Number_of_hospitalisation + range_days, color = NULL
  ), alpha = 0.2)

This produces this graph with 1, 2, 3, 4, 5, 6 as x axis instead of the age group. When I try to change the code to this for both dataframes:

df_hosp_adm_non_can &lt;- data.frame(age_group), 
                              Number_of_hospitalisation = 
       c(3.1,3.2,2.5,3.3,4.8,5.0), 
                              range_days = 
      c(5.8,10.9,14.6,16.1,21.2,14.3)
)

I get the error "aes is missing". What am I doing wrong

答案1

得分: 0

Using data.frame(age_group, ...) 是正确的方式。但在这种情况下,您必须添加 group 美学,即将 group = Group 添加为 age_group 是离散或分类变量(也许这就是您所说的“aes 缺失”的意思?):

library(tidyverse)

age_group <- c("18-30", "30-45", "45-60", "60-75", "75-90", "90+")

df_hosp_adm_can <- data.frame(
  age_group,
  Number_of_hospitalisation = c(3.1, 11.9, 9.6, 9, 93, 8.9),
  range_days = c(4.3, 19.6, 29.5, 24.3, 25.3, 25.2)
)

df_hosp_adm_non_can <- data.frame(
  age_group,
  Number_of_hospitalisation = c(3.1, 3.2, 2.5, 3.3, 4.8, 5.0),
  range_days = c(5.8, 10.9, 14.6, 16.1, 21.2, 14.3)
)

hosp_adm <- list(
  Case = df_hosp_adm_can,
  Control = df_hosp_adm_non_can
) %>%
  bind_rows(.id = "Group") %>%
  mutate(Group = fct_inorder(Group))

hosp_adm %>%
  ggplot(aes(age_group, Number_of_hospitalisation,
    fill = Group, color = Group, group = Group
  )) +
  geom_line() +
  geom_ribbon(aes(
    ymin = Number_of_hospitalisation - range_days,
    ymax = Number_of_hospitalisation + range_days, color = NULL
  ), alpha = 0.2)

在同一图上绘制两条带置信区间的线。

英文:

Using data.frame(age_group, ...) is the way to go. But in that case you have to add the group aesthetic, i.e. add group = Group as age_group is a discrete or categorical variable (Perhaps that's what you meant by "aes is missing"?):

library(tidyverse)

age_group &lt;- c(&quot;18-30&quot;, &quot;30-45&quot;, &quot;45-60&quot;, &quot;60-75&quot;, &quot;75-90&quot;, &quot;90+&quot;)

df_hosp_adm_can &lt;- data.frame(
  age_group,
  Number_of_hospitalisation = c(3.1, 11.9, 9.6, 9, 93, 8.9),
  range_days = c(4.3, 19.6, 29.5, 24.3, 25.3, 25.2)
)

df_hosp_adm_non_can &lt;- data.frame(
  age_group,
  Number_of_hospitalisation = c(3.1, 3.2, 2.5, 3.3, 4.8, 5.0),
  range_days = c(5.8, 10.9, 14.6, 16.1, 21.2, 14.3)
)

hosp_adm &lt;- list(
  Case = df_hosp_adm_can,
  Control = df_hosp_adm_non_can
) |&gt;
  bind_rows(.id = &quot;Group&quot;) |&gt;
  mutate(Group = fct_inorder(Group))

hosp_adm %&gt;%
  ggplot(aes(age_group, Number_of_hospitalisation,
    fill = Group, color = Group, group = Group
  )) +
  geom_line() +
  geom_ribbon(aes(
    ymin = Number_of_hospitalisation - range_days,
    ymax = Number_of_hospitalisation + range_days, color = NULL
  ), alpha = 0.2)

在同一图上绘制两条带置信区间的线。<!-- -->

huangapple
  • 本文由 发表于 2023年5月17日 22:57:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定