目前我希望将条形图和折线图堆叠在一起.

条形图显示了不同年龄组(标记为KS 1、KS 2、KS 3、20—39等)的参与者在测试(1—100)中的表现.这意味着y轴是他们的得分(变量称为"Score"并设置为因子),x轴是他们的年龄类别(变量称为"Score Group"并设置为因子).

我希望在这个条形图的顶部直接显示一个折线图,它显示了每个年龄段的测试分数.我有另一个变量,说明他们的具体年龄,这被称为"特定年龄",并设置为数字.因此,折线图在y轴上有Score,在x轴上有Score.

下面是我的条形图代码,但我不确定如何实现该线形图:

library(ggplot2)

TestAgeGraph <- ggplot2::ggplot(df, aes(x = AgeGroup, y = Score, fill = AgeGroup)) +
  stat_summary(fun = "mean", geom = "bar") +
  labs (x = "Age Group", y = "Test Score", title = "Average Stage Group Across Age Group") +
  theme_light() + geom_point(position = position_jitter(width = 0.1), color = "black")

TestAgeGraph + theme (axis.text.x = element_text(size = 12),
                             axis.title.x = element_text(size = 16),
                             axis.title.y = element_text(size = 16),
                             plot.title = element_text(size = 20),
                             legend.text = element_text(size = 12))

我try 添加以下几行:

geom_line(aes(x = AgeSpecific, y = Score), color = "black")

这看起来很有希望,但图中最左边的条形图被压扁了,如下图所示,x轴标记不佳.

Image of the Graph Output

Minimial Reproducible Example (MRE)

structure(list(ID = 1:24, AgeSpecific = c(67, 5, 18, 14, 17, 
43, 14, 9, 11, 8, 19, 5, 25, 55, 45, 74, 12, 47, 48, 14, 18, 
15, 28, 28), AgeGroup = structure(c(9L, 1L, 5L, 4L, 5L, 7L, 4L, 
2L, 3L, 2L, 5L, 1L, 6L, 8L, 7L, 9L, 3L, 8L, 8L, 4L, 5L, 4L, 6L, 
6L), levels = c("KS1", "KS2", "KS3", "KS4", "KS5", "20-29", "30-45", 
"46-59", "60+"), class = "factor"), Score = c(74, 66, 75, 74, 
72, 81, 68, 56, 67, 78, 75, 92, 77, 78, 66, 51, 64, 73, 74, 73, 
75, 72, 73, 80)), row.names = c(NA, -24L), class = "data.frame")

推荐答案

我有几个选项可以可视化您的数据.但只要你想要显示个人的观察值或每个年龄的平均值,你就必须将你的AgeGroup-factor转换成一个数字.作为一种方法,我在下面的代码中使用geom_rect来显示每个年龄组的平均值,而对于线和点,我也 Select 了平均值.从ggplot2的Angular 来看,这很容易实现,但需要一些数据争论步骤来将年龄段转换为数字范围.还要注意,对于KS个组,我只使用了minmax来设置年龄范围.

library(ggplot2)
library(dplyr, warn = FALSE)
library(tidyr)

dat <- df |>
  tidyr::separate_wider_delim(AgeGroup,
    delim = "-", too_few = "align_start",
    names = c("low", "high"),
    cols_remove = FALSE
  ) |>
  mutate(
    across(c(low, high), readr::parse_number),
  ) |>
  mutate(
    low = if_else(grepl("^KS", AgeGroup), min(AgeSpecific), low),
    high = if_else(grepl("^KS", AgeGroup), max(AgeSpecific), high),
    .by = AgeGroup
  ) |>
  tidyr::replace_na(list(high = 100))

dat_rect <- dat |>
  summarise(
    xmin = unique(low),
    xmax = unique(high),
    ymax = mean(Score),
    .by = AgeGroup
  )

ggplot(df, aes(x = AgeSpecific, y = Score)) +
  geom_rect(
    data = dat_rect,
    aes(xmin = xmin, xmax = xmax, ymin = 0, ymax = ymax, fill = AgeGroup),
    inherit.aes = FALSE
  ) +
  geom_point(color = "black", stat = "summary", fun = mean) +
  geom_line(color = "black", stat = "summary", fun = mean) +
  labs(
    x = "Age Group", y = "Test Score",
    title = "Average Stage Group Across Age Group"
  ) +
  theme_light() +
  theme(
    axis.text.x = element_text(size = 12),
    axis.title.x = element_text(size = 16),
    axis.title.y = element_text(size = 16),
    plot.title = element_text(size = 20),
    legend.text = element_text(size = 12)
  )

R相关问答推荐

高质量地将R格式的图表从Word中输出

删除facet_wrap标签之间的水平线

基于现有类创建类的打印方法(即,打印tibles更长时间)

为什么观察不会被无功值变化触发?

如何将dygraph调用到R Markdown作为一个shiny 的react 对象的参数?

如何在emmeans中计算连续变量的对比度

lightgbm发动机在tidymmodels中的L1正则化""

计算具有奇数日期的运行金额

计算满足R中条件的连续列

使用带有OR条件的grepl过滤字符串

从服务器在Shiny中一起渲染图标和文本

派生程序包| ;无法检索';return()';的正文

ggplot R:X,Y,Z使用固定/等距的X,Y坐标绘制六边形热图

多元正态分布的计算

是否可以将线性模型的p值添加到tbl_summary中

Rmarkdown::Render vs Source()

我已经运行了几个月的代码的`Palette()`中出现了新的gglot错误

如何将字符类对象中的数据转换为R中的字符串

子样本间系数检验的比较

从多行中 Select 最小值