我試圖跳過帶有兩條線的圖形上的 NA 值(ad13com 和 Pd13c),并使用網格排列將其與第二個圖形(xPDd13C)組合。使用我的代碼,由于我的資料中的 NA 值而換行,我正在尋找連續的線條(三個單獨的線條)。我曾嘗試使用 na.rm 引數,但沒有取得任何進展,因為我對 r 很陌生。我意識到問題來自這樣一個事實,即我正在繪制未收集的資料,但我不知道如何將跳過 NA 引數應用于以這種方式繪制的資料!
在我的代碼中,我首先在繪制資料之前收集資料,因為當我使用 facet wrap 生成多個繪圖時,我使用了類似的腳本,因此可能沒有必要為此應用程式收集資料?我也意識到我有多個 y 軸刻度,所以它只是替換現有的刻度并使用最后一個,我不太確定如何正確洗掉它,所以我只是把它留在里面,因為它作業正常

無論如何,這是我的代碼:
theme_set(theme_paleo(8))
theme_update(plot.title = element_text(hjust = 0.5))
#read the data
data <- read_csv("profundal.csv", col_types = cols(.default = col_guess())
)
#first gather the data and set out the omit na function in order to skip na values
data %>% filter(core_id == "BKM0817") %>%
gather(key = param, value = value, -core_id, -Age, -depth) %>%
na.omit()
#plot the first graph
prof1 <-
ggplot()
geom_lineh(data = data, mapping = aes(x=ad13com, y = Age), colour = "black", size = 1)
geom_point(data = data, mapping = aes(x=ad13com, y = Age), colour = "black", size = 2)
geom_lineh(data = data, mapping = aes(x=Pd13c, y = Age), linetype = 2, colour = "black", size = 1,)
geom_point(data = data, mapping = aes(x=Pd13c, y = Age), shape=0, colour = "black", size = 2.7)
scale_y_reverse()
labs (x = expression(delta ^13*"C (\u2030 V-PDB)"), y = "Age (Cal. yrs BP)")
ggtitle(expression(delta ^13*"C"[OM]~"and Chironomus"))
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
#plot the second graph
prof2 <-
ggplot()
geom_lineh(data = data, mapping = aes(x=xPDd13C, y = Age), colour = "black", size = 1)
geom_point(data = data, mapping = aes(x=xPDd13C, y = Age), shape = 2, colour = "black", size = 2)
scale_y_reverse(# Features of the first axis
name = "Age (Cal. yrs BP)",
# Add a second axis and specify its features
sec.axis = sec_axis( trans=~./17.927, name="Depth (cm)")
)
labs(x = expression(delta ^13*"C (\u2030 V-PDB)"), y = "Age (Cal. yrs BP)")
ggtitle( expression(Delta*delta ^13*"C"))
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
#combine the plots
profundal <-gridExtra::grid.arrange(
prof1,
prof2
labs(y = NULL)
scale_y_reverse(labels = NULL, name = "Age (Cal. yrs BP)",
# Add a second axis and specify its features
sec.axis = sec_axis( trans=~./17.927, name="Depth (cm)"))
theme(
plot.margin = unit(c(0.05,0.1, 0.056,0), "inches"),
axis.ticks.y = element_blank(),
plot.background = element_blank()
),
nrow = 1,
widths = c(4, 4)
)
# save the file
ggsave("test.png", units="in", width=8, height=6, dpi=300, plot=profundal)
提前謝謝了!
uj5u.com熱心網友回復:
當您使用na_omit()上data的結果沒有分配到任何東西。您需要將結果分配給data(或其他)。
data <- data %>% filter(core_id == "BKM0817") %>%
gather(key = param, value = value, -core_id, -Age, -depth) %>%
na.omit()
uj5u.com熱心網友回復:
我設法在收集后繪制了新資料(正如 Tjn 建議的那樣,最后使用 na.omit 函式)。
data2 <- data %>% filter(core_id == "BKM0817") %>%
gather(key = param, value = value, -core_id, -Age, -depth) %>%
na.omit()
資料如下所示: 
為了在同一張圖上繪制兩條線,我必須對資料進行子集化,并像這樣為第一個圖單獨繪制線和點,并為第二個圖遵循相同的結構:
prof1 <-
ggplot()
geom_lineh(data = subset(data2,param %in% "ad13com"), mapping = aes(x=value, y = Age), colour = "black", size = 1)
geom_point(data = subset(data2,param %in%"ad13com"), mapping = aes(x=value, y = Age), colour = "black", size = 2)
geom_lineh(data = subset(data2,param %in% "Pd13c"), mapping = aes(x=value, y = Age), linetype = 2, colour = "black", size = 1,)
geom_point(data = subset(data2,param %in% "Pd13c"), mapping = aes(x=value, y = Age), shape=0, colour = "black", size = 2.7)
scale_y_reverse()
labs (x = expression(delta ^13*"C (\u2030 V-PDB)"), y = "Age (Cal. yrs BP)")
ggtitle(expression(delta ^13*"C"[OM]~"and Chironomus"))
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
最終結果現在與問題中的圖形相同,但現在用線單獨連接所有點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/315854.html
上一篇:在同一圖上用置信區間覆寫兩條線
