R语言 如何使用ggplot2向多个组的配对观测添加线?

67up9zun  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(186)

我对样本进行了不同的处理,然后在两个不同的温度下进行了测量。现在,我想使用ggplot2在属于一个样本的两个点之间添加直线,以可视化每个样本的成对观测结果。然而,当我尝试这样做时,直线没有连接两个点,而是在图中显示为垂直线。编辑:关于重复项,我认为与我的数据的主要区别在于变量“fishid”中有两个以上的类别。
这是我的数据:

dput(s2n)
structure(list(fishid = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 
4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 80L, 80L, 81L, 81L, 82L, 
82L, 83L, 83L, 84L, 84L, 85L, 85L, 86L, 86L), .Label = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", 
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "47", 
"48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", 
"59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", 
"70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", 
"81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", 
"92", "93", "94", "95", "96", "97", "98", "99", "100", "101", 
"102", "103", "104", "105", "107", "108", "109", "110", "111", 
"112", "113", "114"), class = "factor"), mtemp = c("23", "28", 
"23", "28", "23", "28", "23", "28", "23", "28", "23", "28", "23", 
"28", "23", "28", "23", "28", "23", "28", "23", "28", "23", "28", 
"23", "28", "23", "28", "23", "28"), treat = c("2n 28 28 n", 
"2n 28 28 n", "2n 28 28 n", "2n 28 28 n", "2n 28 28 n", "2n 28 28 n", 
"2n 28 28 n", "2n 28 28 n", "2n 28 28 n", "2n 28 28 n", "2n 28 28 n", 
"2n 28 28 n", "2n 28 28 n", "2n 28 28 n", "2n 28 28 n", "2n 28 28 n", 
"2n 23 23 n", "2n 23 23 n", "2n 23 23 n", "2n 23 23 n", "2n 23 23 n", 
"2n 23 23 n", "2n 23 23 n", "2n 23 23 n", "2n 23 23 n", "2n 23 23 n", 
"2n 23 23 n", "2n 23 23 n", "2n 23 23 n", "2n 23 23 n"), N = c(4, 
3, 4, 4, 3, 3, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 3, 1, 4, 4, 3, 4, 
3, 4, 3, 4, 1, 2, 1, 4), mo2_ms = c(0.0305521553416736, 0.145682463746226, 
0.0396710467863138, 0.0954773661923973, 0.0423458775047231, 0.074814122089128, 
0.0340000306491368, 0.164506483241493, 0.0199183915444751, 0.0141578076646164, 
0.0352100227426512, 0.140223575206394, 0.0867953316734817, 0.181380336846186, 
0.0814477532477152, 0.245441716187935, 0.0753519288551872, 0.192765607492084, 
0.112553420128519, 0.154874683462045, 0.096957746918988, 0.174162792054346, 
0.0572348282730377, 0.198498172699998, 0.0969845912494747, 0.0883266097299638, 
0.00529159243126301, 0.048338396851318, 0.0154151384427716, 0.165540613767348
), sd = c(0.0225825414793994, 0.040281910779122, 0.0353185196092258, 
0.0524668087896432, 0.0229625978946772, 0.00715984733525644, 
0.0213724448650181, 0.0416621727661058, 0.00851354415170029, 
0.00409799422855184, 0.0147318782789288, 0.0800135890495444, 
0.0207362723426385, 0.0171873141998392, 0.0526956334274908, 0.0569951399710065, 
0.0352363626542992, NA, 0.0752014409258304, 0.018373804187377, 
0.0294532805712796, 0.0188553911271472, 0.0414618539087027, 0.0294471142185835, 
0.0120861972052966, 0.0268913613699161, NA, 0.0614419583639327, 
NA, 0.0919378871844233), se = c(0.0112912707396997, 0.0232567720317986, 
0.0176592598046129, 0.0262334043948216, 0.013257462075785, 0.00413373978636693, 
0.0106862224325091, 0.0208310863830529, 0.00425677207585014, 
0.00236597807099194, 0.00736593913946441, 0.0400067945247722, 
0.0103681361713193, 0.00859365709991959, 0.0263478167137454, 
0.0284975699855032, 0.0203437234637229, NA, 0.0376007204629152, 
0.00918690209368851, 0.0170048594663459, 0.00942769556357362, 
0.0239380125152904, 0.0147235571092918, 0.00697796920995688, 
0.0134456806849581, NA, 0.0434460254085183, NA, 0.0459689435922116
), ci = c(0.0359338628475153, 0.100065813667779, 0.0561996461192354, 
0.0834864008913134, 0.0570422553899265, 0.0177860467758856, 0.0340083290977783, 
0.0662938138947925, 0.0135469485652242, 0.0101799820056808, 0.0234417057993332, 
0.127319475421606, 0.0329960366510934, 0.0273488522865142, 0.0838505119529514, 
0.0906919863099286, 0.0875319772944557, NaN, 0.11966227391321, 
0.0292368226250621, 0.073166005001879, 0.0300031349136345, 0.102996954893691, 
0.0468569299230992, 0.0300237782693287, 0.0427901568178693, NaN, 
0.552034093813681, NaN, 0.146293694692829)), row.names = c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 
160L, 161L, 162L, 163L), class = "data.frame")

以下是我创建的不带连接线的图的代码:

p1 <- ggplot(data = s2n, aes(x = treat, y = mo2_ms, fill = mtemp)) +
  geom_boxplot() +
  geom_point(position=position_jitterdodge(jitter.width = 0, jitter.height = 0)) # I am using the position_jitterdodge to actually get the points to align to the middel of the boxplots
  
p1

这将生成以下图:

我想创建一个这样的图,但在同一个图中使用两种处理:

我尝试使用此代码在同一个图中添加两个处理的连接线:

p3 <- ggplot(data = s2n, aes(x = treat, y = mo2_ms, fill = mtemp)) +
  geom_boxplot() +
  geom_point(position=position_jitterdodge(jitter.width = 0, jitter.height = 0)) +
  geom_line(aes(group=fishid))
p3

以下是结果图:

当我尝试在geom_line参数中使用相同的position_jitterdodge时,我得到以下结果:

p4 <- ggplot(data = s2n, aes(x = treat, y = mo2_ms, fill = mtemp)) +
  geom_boxplot() +
  geom_point(position=position_jitterdodge(jitter.width = 0, jitter.height = 0)) +
  geom_line(position=position_jitterdodge(jitter.width = 0, jitter.height = 0), aes(group=fishid))
p4

导致此图:

有人对如何用直线连接plot4中的点有什么建议吗?

bq3bfh9z

bq3bfh9z1#

这是一个有点狡猾...:)
首先,如果指定width和height = 0,我就不会使用jitter。
图1:但是我想,主要的问题是线条减淡效果不好。我会根据你的变量mtemp手动减淡。
图2和图3:至于我的另一个建议--我真的很想鼓励你重新思考你的可视化,也许可以考虑遵循链接中的建议。下面是两种制作散点图或箱线图的方法。

library(ggplot2)
library(tidyr)
library(dplyr)

## saving this as an expression in order to save typing
expr <- expr(ifelse(mtemp == 23, newtreat - .25, newtreat + .25))

s2n %>%
  mutate(newtreat = as.integer(factor(treat))) %>%
  ggplot(aes(x = !!expr, y = mo2_ms)) +
  ## you will need to explicitly define the boxplot group
  geom_boxplot(aes(group = !!expr)) +
  ## don't do anything dodgy
  geom_point() +
  geom_line(aes(group = fishid))

创建于2023年2月15日,使用reprex v2.0.2

s2n %>%
 select(fishid, treat, mtemp, mo2_ms) %>%
 pivot_wider(names_from = "mtemp", values_from = "mo2_ms", names_prefix = "m") %>%
 ggplot(aes(m23, m28)) +
 geom_point() +
## I like to add a line of equality which you might label for more clarity
 geomtextpath::geom_textabline(intercept = 0, slope = 1, lty = 2, linewidth = .1, 
                               label = "Better\nEqual\nWorse") +
 facet_grid(~treat)

s2n %>%
select(fishid, treat, mtemp, mo2_ms) %>%
pivot_wider(names_from = "mtemp", values_from = "mo2_ms", names_prefix = "m") %>%
ggplot(aes(x = "", y = m28-m23)) +
geom_boxplot(aes(fill = treat))

相关问题