R 为图形的特定部分添加几何图形,而不是为所有绘图添加几何图形
我有一张像这样的图 使用此代码R 为图形的特定部分添加几何图形,而不是为所有绘图添加几何图形,r,ggplot2,R,Ggplot2,我有一张像这样的图 使用此代码 ggplot(SurJagPred$estimates, aes(x=Primary, y=estimate, col=Sex)) + geom_line(aes(), size = 1) + geom_point()+ geom_errorbar(aes(ymin = lcl, ymax = ucl ), alpha = 0.5) + geom_vline(xintercept = 2.5, linetype = "dotdash&
ggplot(SurJagPred$estimates, aes(x=Primary, y=estimate, col=Sex)) +
geom_line(aes(), size = 1) +
geom_point()+
geom_errorbar(aes(ymin = lcl, ymax = ucl ), alpha = 0.5) +
geom_vline(xintercept = 2.5, linetype = "dotdash") +
geom_vline(xintercept = 3.5, linetype = "dotdash") +
geom_vline(xintercept = 4.5, linetype = "dotdash") +
facet_wrap(~ grid)+
labs(title = "Survival of Small Mammals", x = "Primary Periods", y = "Survival Rate (95% CI)") +
theme_bw(base_size = 12)+ #text size
theme(panel.grid = element_blank())
treatment<- c(2.5, 3.5, 4.5 )
letter<- c("B", "C", "A")
treat<- data.frame(treatment, letter)
然而,点划线不应该到处重复。对于A图,其应仅位于4.5位置,B图应仅位于2.5位置,C图应仅位于3.5位置。我试图用这段代码创建一个具有这些细节的数据框架
ggplot(SurJagPred$estimates, aes(x=Primary, y=estimate, col=Sex)) +
geom_line(aes(), size = 1) +
geom_point()+
geom_errorbar(aes(ymin = lcl, ymax = ucl ), alpha = 0.5) +
geom_vline(xintercept = 2.5, linetype = "dotdash") +
geom_vline(xintercept = 3.5, linetype = "dotdash") +
geom_vline(xintercept = 4.5, linetype = "dotdash") +
facet_wrap(~ grid)+
labs(title = "Survival of Small Mammals", x = "Primary Periods", y = "Survival Rate (95% CI)") +
theme_bw(base_size = 12)+ #text size
theme(panel.grid = element_blank())
treatment<- c(2.5, 3.5, 4.5 )
letter<- c("B", "C", "A")
treat<- data.frame(treatment, letter)
尝试一下上面提到的@eipi10(在缺乏数据的情况下未进行测试)。另外,当您使用颜色变量时,行的数据应该包含该变量,但您可以使用
NA
定义该变量。代码如下:
library(ggplot2)
#Data
Primary<- c(2.5, 3.5, 4.5 )
grid<- c("B", "C", "A")
treat<- data.frame(grid, Primary,stringsAsFactors = F)
treat$col <- NA
#Plot
ggplot(SurJagPred$estimates, aes(x=Primary, y=estimate, col=Sex)) +
geom_line(aes(), size = 1) +
geom_point()+
geom_errorbar(aes(ymin = lcl, ymax = ucl ), alpha = 0.5) +
geom_vline(data=treat,aes(xintercept = Primary), linetype = "dotdash") +
facet_wrap(~ grid)+
labs(title = "Survival of Small Mammals", x = "Primary Periods",
y = "Survival Rate (95% CI)") +
theme_bw(base_size = 12)+ #text size
theme(panel.grid = element_blank())
库(ggplot2)
#资料
Primary首先,在treat
数据框中,列名应与绘图中使用的主数据框中的列名相对应,因此列名应为Primary
和grid
。然后做geom\u vline(data=treat,aes(xintercept=Primary),linetype=“dotdash”)
。好的,我按照你的步骤做了,它给出了相同的结果。我如何指定我只想要A在4.5,B在2.5,C在3.5我添加了其余的数据来帮助澄清,很抱歉没有发布它before@Jena我已使用您共享的数据添加了更新。我希望这有帮助@杰娜,这是我的荣幸!
#Data
df <- structure(list(vcv.index = c(6L, 7L, 8L, 9L, 10L, 21L, 22L, 23L,
24L, 25L, 41L, 42L, 43L, 44L, 45L, 196L, 197L, 198L, 199L, 200L,
211L, 212L, 213L, 214L, 215L, 226L, 227L, 228L, 229L, 230L),
model.index = c(6L, 7L, 8L, 9L, 10L, 21L, 22L, 23L, 24L,
25L, 41L, 42L, 43L, 44L, 45L, 196L, 197L, 198L, 199L, 200L,
211L, 212L, 213L, 214L, 215L, 226L, 227L, 228L, 229L, 230L
), par.index = c(16L, 17L, 18L, 19L, 20L, 61L, 62L, 63L,
64L, 65L, 121L, 122L, 123L, 124L, 125L, 586L, 587L, 588L,
589L, 590L, 631L, 632L, 633L, 634L, 635L, 676L, 677L, 678L,
679L, 680L), grid = c("A", "A", "A", "A", "A", "B", "B",
"B", "B", "B", "C", "C", "C", "C", "C", "A", "A", "A", "A",
"A", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C"), index = c(16L,
17L, 18L, 19L, 20L, 61L, 62L, 63L, 64L, 65L, 121L, 122L,
123L, 124L, 125L, 586L, 587L, 588L, 589L, 590L, 631L, 632L,
633L, 634L, 635L, 676L, 677L, 678L, 679L, 680L), estimate = c(0.8856129,
0.6298085, 0.6299329, 0.6298073, 0.757578, 0.8712803, 0.6074361,
0.6041038, 0.5806663, 0.7370924, 0.8047651, 0.5259543, 0.5427102,
0.516894, 0.6550035, 0.8535576, 0.5672181, 0.5675404, 0.5666988,
0.7058527, 0.8359801, 0.5432876, 0.5401, 0.5161817, 0.6821666,
0.7620894, 0.4607459, 0.4775193, 0.4517796, 0.5944339), se = c(0.07034495,
0.06925362, 0.06658399, 0.05511803, 0.05033492, 0.07640191,
0.06881164, 0.0610739, 0.06927518, 0.05892159, 0.0968367,
0.07165185, 0.0712719, 0.06156377, 0.07378427, 0.08711021,
0.0707946, 0.06380437, 0.06499677, 0.05985742, 0.09415382,
0.07906137, 0.06497578, 0.06292617, 0.07279913, 0.10484134,
0.07326857, 0.08336355, 0.06392891, 0.07210424), lcl = c(0.6650065,
0.4873071, 0.4930294, 0.5170043, 0.6461752, 0.6404038, 0.467785,
0.4805314, 0.4422311, 0.6070616, 0.5519029, 0.3871407, 0.4033297,
0.3975699, 0.5002863, 0.5979391, 0.4268475, 0.4408554, 0.4377245,
0.5769525, 0.5702268, 0.3891407, 0.4129088, 0.3943413, 0.52639,
0.5077061, 0.3240289, 0.3219411, 0.3319493, 0.4492079), ucl = c(0.9679442,
0.7527946, 0.7487096, 0.7300226, 0.842457, 0.9625845, 0.731478,
0.7156737, 0.7074705, 0.8357368, 0.9324112, 0.6608688, 0.6757102,
0.6343222, 0.7826314, 0.9580602, 0.6975683, 0.6859682, 0.6872281,
0.8085129, 0.9514068, 0.689567, 0.6622738, 0.6361283, 0.8056259,
0.9086707, 0.6036357, 0.6375852, 0.5774748, 0.7248249), Primary = c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L),
Sex = c("F", "F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "M", "M", "M", "M", "M", "M", "M",
"M", "M", "M", "M", "M", "M", "M", "M")), row.names = c(NA,
-30L), class = "data.frame")