我正在創建一個y=results x=Date的圖。有一個很大的時間段我不想繪制,所以我將該圖分成了兩個面,不包括不需要的日期。
我希望在兩個面都有一條水平線,但標簽 "正常限制 "只出現在第一個面上。
這可能嗎?
df = data%>%
filter((Date > = dmy("25/03/2011") & 日期 < = dmy("18/05/2012")) | #filter unwanted data
(Date > = dmy("18/07/2019") &/span> Date < = dmy("28/04/2020") ))%> %
mutate(bin = Date > = dmy("18/07/2019") # define the cut point where I want data to be shown.
plot = ggplot(df, aes(date, 結果))
geom_point(colour = "red3", size= 2。 5)
geom_line( color= "紫色4", 尺寸= 1)
facet_wrap(bin ~ . ,規模= 'free_x')
scale_x_date(date_labels = "%b%Y"。 guide = guide_axis(angle=) operator">= 45)。 date_breaks = "2 months")
scale_y_continuous(breaks = seq(0,6。 2,by=0.5))
主題(strip.text。 x = element_blank()) # 這將洗掉真/假倉識別。
geom_vline(xintercept = dmy("29/8/2011")。 linetype="dashed",
color = "black"。 size=1, ) >
geom_vline(xintercept = dmy("26/1/2012")。 linetype="longdash",
color = "black"。 size=1, ) >
geom_vline(xintercept = dmy("16/9/19")。 linetype="dashed",
color = "black"。 size=1, ) >
geom_vline(xintercept = dmy("7/11/19")。 linetype="longdash",
color = "black"。 size=1, ) >
geom_hline(yintercept =0。 4,lineetype="dotted",
color = "black"。 size=1, ) >
注釋(y= 0。 4。 x = dmy("01/5/11">), label="normal Limit"。 geom = "label")
這就是資料:
> dput(data)
結構(list(Date = 結構(c(18873。 18843, 18766。 18746, 18746,
18745, 18738, 18705。 18691, 18688。 18645, 18583。 18549, 18541, 18549,
18509, 18502, 18457。 18435, 18381。 18380, 18362。 18320, 18276, 18276,
18232, 18219, 18207, 18199, 18195。 18194, 18190。
18177, 18164, 18152, 18148, 18127。 18108, 18095。 18066, 18038,18016, 17988, 17948。 17919, 17875。 17858, 17830。
17732, 17704, 17689。 17669, 17646。 17618, 17583。 17557, 17520, 17557,
17487, 17443, 17400, 17381, 17373, 17368, 17340。 17312, 17284, 17312,
17249, 17232, 17228, 17207, 17196。 17186, 17134。
17078, 17043, 17037。 17032, 17016。 17008, 16986。 16951, 16930, 16951,
16915, 16910, 16878。 16860, 16812。 16766, 16729, 16715, 16701, 16715,
16689, 16674, 16671。 16668, 16661。 16657, 16643。
16561, 16524, 16489。 16443, 16402。 16365, 16344。 16310, 16304, 16310,
16286, 16255, 16224。 16204, 16189。 16175, 16160。 16150, 16141, 16150,
16127, 16120, 16105, 16090, 16077。 16069, 16057。 16057, 16048,
16041, 16034, 16024, 16021, 16014。 16008, 16002。 15996, 15993,
15988, 15981, 15975, 15973, 15964。 15940, 15926。 15908, 15883, 15883,
15875, 15870, 15861。 15849, 15845。 15838, 15835。 15832, 15828,15828, 15826, 15819。 15819, 15798。 15783, 15765。 15715, 15674,
15660, 15635, 15602。 15593, 15572。 15552, 15517。 15489, 15478, 15489,
15455, 15427, 15399。 15380, 15365。 15364, 15362, 15355。 NA, 15308, 15308,
15273, 15260, 15250。 15230, 15226, 15180, 15175。 15154, 15114,
15082, 15058, 15028, 14995, 14973。 14935, 14911。
14816, 14790, 14757, 14727, 14697。 14666, 14607。 14580, 14536,
14517, 14480, 14452。 14391, 14363。 14328, 14295。
14174, 14146, 14118。 14081, 14055, 14022, 13992。 13845, 13817,
13782, 13719。 13691, 13661)。 class = "Date")。 結果 = c(0. 1,
0.1, 0。 2, 0.1, 0. 1, 0.1, 0.2, 0. 1, 0.2, 0. 2, 0.2,
0.2, 0。 2, 0.2, 0. 2, 0.2, 0.3, 0. 4, 1, 1, 1。 2, 1.3, 0. 9, 1.1,
1.2, 1, 0。 8, 0. 6, 0.5, 0. 1, 0.1, 0.2, 0。 2, 0.2, 0. 2, 0.1,
0.2, 0。 1, 0.2, 0. 1, 0.2, 0.3, 0。 3, 0.3, 0. 3, 0.3, 0. 2, 0.2,
0.2, 0。 4, 0.3, 0. 2, 0.3, 0. 2, 0.2, 0. 2, 0.2, 0. 2, 0.2,
0.2, 0。 2, 0.2, 0. 2, 0.3, 0. 3, 0.1, 0.2, 0。 2, 0.2, 0. 2, 0.2,
0.2, 0。 1, 0.2, 0. 2, 0.2, 0. 3, 0.2, 0. 2, 0.2, 0. 2, 0.2,
0.2, 0。 3, 0.2, 0. 2, 0.2, 0. 2, 0.3, 0. 2, 0.4, 0. 4, 0.3,
0.3, 0.3, 0. 3, 0.3, 0.33, 0. 29, 0.25, 0. 29, 0.36, 0.16,
0.18, 0.36, 0。 29, 0.26, 0.24, 0. 25, 0.33, 0.22, 0. 34, 0.39, 0.4,
0.22, 0.3, 0. 29, 0.31, 0. 37, 0.85, 0.52, 0. 29, 0.37, 0.4,
0.4, 0.43, 0. 4, 0.32, 0.27, 0. 18, 0.27, 0. 18, 0.22, 0.26,
0.28, 0.27, 0。 18, 0.18, 0.21, 0. 21, 0.23, 0.16, 0. 21, 0.21, 0.2,
0.28, 0.39, 0。 27, 0.34, 0.37, 0. 31, 0.33, 0.42, 0. 25, 0.31, 0.31,
0.23, 0.31, 0. 33, 0.4, 0.47, 0. 46, 1.19, 2.51, 4. 62, 5.57, 2.8,
6.05, NA, 1。 21, 0.56, 0. 34, 0.72, 0. 24, 0.28, 0.25,
0.2, 0。 2, 0.3, 0. 22, 0.29, 0. 2, 0.2, 0.3, 0. 3, 0.3, 0.2,
0.2, 0。 2, 0.3, 0. 2, 0.3, 0. 2, 0.2, 0.3, 0。 3, 0.3, 0. 2, 0.2,
0.3, 0。 2, 0.2, 0. 3, 0.3, 0. 3, 0.3, 0. 2, 0.2, 0. 2, 0.3,
0.24, 0.32, 0. 2)),行。 names = c(NA。 -234L)。 class = c("tbl_df"。
"tbl", "data.frame"))
uj5u.com熱心網友回復:
切面注釋的問題在于,annotate()不接受data引數,因為切面變數可以被查詢到。相反,我建議你使用一個常規的geom_label()層,其中data引數包含分面變數。簡化的例子如下:
library(ggplot2)
library(dplyr)
library(lubridate)
# data <- structure(..) # As per example data
# df <- data %>% filter(..) # As per example code
ggplot(df, aes(date, 結果))
geom_line()
facet_wrap(bin ~ . , scale = "free_x")
geom_label()
data = data. frame(bin = FALSE),
aes(x = dmy("01/5/11")。 y = 0. 4, label = "normal
限制")
)

uj5u.com熱心網友回復:
我以前也糾結過這個問題,我找到的最好的解決方案是使用網格圖形來覆寫我的注釋,例如:
library(tidyverse)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> 以下物件被屏蔽在'package:base'之外:
#>
#> date, intersect, setdiff, union
library(grid)
library(gridExtra)
#>
#> Attaching package: 'gridExtra'
#> 以下物件被屏蔽在'package:dplyr'中:
#>
#> combine
df1 < -結構(list(日期=結構(c(18873。 18843, 18766。 18746, 18746,
18745, 18738, 18705。 18691, 18688。 18645, 18583。 18549, 18541, 18549,
18509, 18502, 18457。 18435, 18381。 18380, 18362。 18320, 18276, 18276,
18232, 18219, 18207, 18199, 18195。 18194, 18190。
18177, 18164, 18152, 18148, 18127。 18108, 18095。 18066, 18038,18016, 17988, 17948。 17919, 17875。 17858, 17830。
17732, 17704, 17689。 17669, 17646。 17618, 17583。 17557, 17520, 17557,
17487, 17443, 17400, 17381, 17373, 17368, 17340。 17312, 17284, 17312,
17249, 17232, 17228, 17207, 17196。 17186, 17134。
17078, 17043, 17037。 17032, 17016。 17008, 16986。 16951, 16930, 16951,
16915, 16910, 16878。 16860, 16812。 16766, 16729, 16715, 16701, 16715,
16689, 16674, 16671。 16668, 16661。 16657, 16643。
16561, 16524, 16489。 16443, 16402。 16365, 16344。 16310, 16304, 16310,
16286, 16255, 16224。 16204, 16189。 16175, 16160。 16150, 16141, 16150,
16127, 16120, 16105, 16090, 16077。 16069, 16057。 16057, 16048,
16041, 16034, 16024, 16021, 16014。 16008, 16002。 15996, 15993,
15988, 15981, 15975, 15973, 15964。 15940, 15926。 15908, 15883, 15883,
15875, 15870, 15861。 15849, 15845。 15838, 15835。 15832, 15828,15828, 15826, 15819。 15819, 15798。 15783, 15765。 15715, 15674,
15660, 15635, 15602。 15593, 15572。 15552, 15517。 15489, 15478, 15489,
15455, 15427, 15399。 15380, 15365。 15364, 15362, 15355。 NA, 15308, 15308,
15273, 15260, 15250。 15230, 15226, 15180, 15175。 15154, 15114,
15082, 15058, 15028, 14995, 14973。 14935, 14911。
14816, 14790, 14757, 14727, 14697。 14666, 14607。 14580, 14536,
14517, 14480, 14452。 14391, 14363。 14328, 14295。
14174, 14146, 14118。 14081, 14055, 14022, 13992。 13845, 13817,
13782, 13719。 13691, 13661)。 class = "Date")。 結果 = c(0. 1,
0.1, 0。 2, 0.1, 0. 1, 0.1, 0.2, 0. 1, 0.2, 0. 2, 0.2,
0.2, 0。 2, 0.2, 0. 2, 0.2, 0.3, 0. 4, 1, 1, 1。 2, 1.3, 0. 9, 1.1,
1.2, 1, 0。 8, 0. 6, 0.5, 0. 1, 0.1, 0.2, 0。 2, 0.2, 0. 2, 0.1,
0.2, 0。 1, 0.2, 0. 1, 0.2, 0.3, 0。 3, 0.3, 0. 3, 0.3, 0. 2, 0.2,
0.2, 0。 4, 0.3, 0. 2, 0.3, 0. 2, 0.2, 0. 2, 0.2, 0. 2, 0.2,
0.2, 0。 2, 0.2, 0. 2, 0.3, 0. 3, 0.1, 0.2, 0。 2, 0.2, 0. 2, 0.2,
0.2, 0。 1, 0.2, 0. 2, 0.2, 0. 3, 0.2, 0. 2, 0.2, 0. 2, 0.2,
0.2, 0。 3, 0.2, 0. 2, 0.2, 0. 2, 0.3, 0. 2, 0.4, 0. 4, 0.3,
0.3, 0.3, 0. 3, 0.3, 0.33, 0. 29, 0.25, 0. 29, 0.36, 0.16,
0.18, 0.36, 0。 29, 0.26, 0.24, 0. 25, 0.33, 0.22, 0. 34, 0.39, 0.4,
0.22, 0.3, 0. 29, 0.31, 0. 37, 0.85, 0.52, 0. 29, 0.37, 0.4,
0.4, 0.43, 0. 4, 0.32, 0.27, 0. 18, 0.27, 0. 18, 0.22, 0.26,
0.28, 0.27, 0。 18, 0.18, 0.21, 0. 21, 0.23, 0.16, 0. 21, 0.21, 0.2,
0.28, 0.39, 0。 27, 0.34, 0.37, 0. 31, 0.33, 0.42, 0. 25, 0.31, 0.31,
0.23, 0.31, 0. 33, 0.4, 0.47, 0. 46, 1.19, 2.51, 4. 62, 5.57, 2.8,
6.05, NA, 1。 21, 0.56, 0. 34, 0.72, 0. 24, 0.28, 0.25,
0.2, 0。 2, 0.3, 0. 22, 0.29, 0. 2, 0.2, 0.3, 0. 3, 0.3, 0.2,
0.2, 0。 2, 0.3, 0. 2, 0.3, 0. 2, 0.2, 0.3, 0。 3, 0.3, 0. 2, 0.2,
0.3, 0。 2, 0.2, 0. 3, 0.3, 0. 3, 0.3, 0. 2, 0.2, 0. 2, 0.3,
0.24, 0.32, 0. 2)),行。 names = c(NA。 -234L)。 class = c("tbl_df"。
"tbl", "data.frame"))
df <- df1 %>%
filter((Date > = dmy("25/03/2011") & 日期 < = dmy("18/05/2012")) | #filter unwanted data
(Date > = dmy("18/07/2019") &/span> Date < = dmy("28/04/2020") ))%> %
mutate(bin = ifelse(Date > = dmy("18/07/2019")。 "yes", "no"))
plot = ggplot(df, aes(date, 結果))
geom_point(colour = "red3", size= 2。 5)
geom_line( color= "紫色4", 尺寸= 1)
facet_wrap(bin ~ . ,規模= 'free_x')
scale_x_date(date_labels = "%b%Y"。 guide = guide_axis(angle=) operator">= 45)。 date_breaks = "2 months")
scale_y_continuous(breaks = seq(0,6。 2,by=0.5))
主題(strip.text。 x = element_blank()) # this remove the true/false bin identification
geom_vline(xintercept = dmy("29/8/2011")。 linetype="dashed",
color = "black"。 size=1)
geom_vline(xintercept = dmy("26/1/2012")。 linetype="longdash",
color = "black"。 size=1)
geom_vline(xintercept = dmy("16/9/19")。 linetype="dashed",
color = "black"。 size=1)
geom_vline(xintercept = dmy("7/11/19")。 linetype="longdash",
color = "black"。 size=1)
geom_hline(yintercept =0。 4,lineetype="dotted",
color = "black", size=1)
# annotate(y= 0.4, x = dmy("01/5/11"), label="normal Limit", geom = "label")
gtext <- grid:: textGrob("normal Limit", x = unit(0。 15, "npc"), y = unit(0. 25, "npc"))
gcombined <- grid:: grobTree(ggplotGrob(plot),gtext)
grid.arrange(gcombined)

創建于2021-09-12,由reprex包(v2.0.1)
這是否適合你的問題?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/318936.html
標籤:
上一篇:R-如何"創建"或繪制缺失資料?
下一篇:處理具有自定義分隔符的測驗檔案


