R语言 绘制线图和点图,其中x轴为分类变量,两个变量用于定义组

oogrdqng  于 2023-02-06  发布在  其他
关注(0)|答案(1)|浏览(176)

因此,我希望得到一个图,其中x轴上有两个类别,线和数据点的颜色由一个变量定义,点的形状由第二个变量定义

effectBaseline <- ggplot(
    data = bdataPEV, 
    aes(x=variable, y=value, group=Electrode, color=subject)) +
geom_line()+
geom_jitter(aes(x=variable,
        y = value,
        shape = Electrode,
        color=subject),
    size=2,
    show.legend=TRUE,
    width = 0.1)+
labs(x = "",
    y="PEV (dps)",
title = "Effect of baseline",
subtitle= "PEV values at UCL and T") +
theme_classic() +
theme(axis.title=element_text(size=8,face="bold"),
    axis.text.x = element_text(face="bold", size=8, angle=0),
    axis.text.y = element_text(face="bold",size=10, angle=90),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank()) +
geom_hline(yintercept = 0)+
scale_x_discrete(labels=c("no Baseline","with Baseline"))

示例数据:

structure(list(subject = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("1", 
"2", "3", "6"), class = "factor"), Electrode = c("LAN", "SAN", 
"PAN", "LAN", "SAN", "PAN", "LAN", "SAN", "PAN", "LAN", "SAN", 
"PAN", "LAN", "SAN", "PAN", "LAN", "SAN", "PAN", "LAN", "SAN", 
"PAN", "LAN", "SAN", "PAN", "LAN", "SAN", "PAN", "LAN", "SAN", 
"PAN", "LAN", "SAN", "PAN", "LAN", "SAN", "PAN"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), .Label = c("PEVsum_block_noB", "PEVsum_block_B", 
"PEVsum_block_B_norm", "PEVsum_block_B_inhibition", "PEVsum_blok_B_inhibition_norm"
), class = "factor"), value = c(26.5655048141819, 24.25, 4.30277563773199, 
158.352853610442, 100.585046948683, 26.0372066865141, 147.516666666667, 
156.7275, 65.9128571428571, 1.7378211394883, 1.9853826151828, 
2.96232650874689, 4.24264068711928, 14, 1, 141.354165131417, 
90.2108640907513, 5, 130.96, 137.72, 23.32, 2.4099999998795, 
1.86214499960808, 2.96192610446308, -2.82842712474619, 0, -1, 
-15.0332963783729, -9.4339811320566, -9.05538513813742, -31.13, 
-28.57, -30.27, 0, 0, -0.679999999932)), row.names = c(1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 37L, 38L, 39L, 40L, 41L, 
42L, 43L, 44L, 45L, 46L, 47L, 48L), class = "data.frame")
rdlzhqv9

rdlzhqv91#

我在图中修改了两点:
1.我使用interaction按受试者和电极对线进行分组。
1.我将直线和点设置为具有相同的位置调整。更多信息请参见此问题:How to jitter both geom_line and geom_point by the same magnitude?

pos <- position_dodge(width = 0.1)

ggplot(data = bdataPEV,
       aes(
           x = variable,
           y = value,
           color = subject,
           shape = Electrode,
           group = interaction(subject, Electrode)
       )) +
    geom_line(position = pos) +
    geom_point(
        size = 2,
        show.legend = TRUE,
        position = pos
    ) +
    labs(
        x = "",
        y = "PEV (dps)",
        title = "Effect of baseline",
        subtitle = "PEV values at UCL and T"
    ) +
    theme_classic() +
    theme(
        axis.title = element_text(size = 8, face = "bold"),
        axis.text.x = element_text(face = "bold", size = 8, angle = 0),
        axis.text.y = element_text(face = "bold", size = 10, angle = 90),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank()
    ) +
    geom_hline(yintercept = 0) +
    scale_x_discrete(labels = c("no Baseline", "with Baseline"))

相关问题