英文:
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 <- c("18-30", "30-45", "45-60", "60-75", "75-90", "90+")
df_hosp_adm_can <- 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 <- 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 <- 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(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 <- 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 <- 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)
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论