使用 GGplot 跳过多线图的 NA 值(并使用网格排列与另一个图结合)
Skipping NA values for a multiple line plot using GGplot (and combined with another plot using grid arrange)
我正在尝试跳过带有两条线的图形 (ad13com & Pd13c) 上的 NA 值,并使用网格排列将其与第二个图形 (xPDd13C) 组合。使用我的代码,由于我的数据中的 NA 值导致换行,我正在寻找连续的线(三个独立的线)。我尝试使用 na.rm 参数但没有取得任何进展,因为我对 r 还很陌生。我意识到问题出在我正在绘制未收集的数据,但我不知道如何将 skip 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)
非常感谢!
在 data
上使用 na_omit()
的地方,结果没有分配给任何东西。您需要将结果分配给 data
(或其他东西)。
data <- data %>% filter(core_id == "BKM0817") %>%
gather(key = param, value = value, -core_id, -Age, -depth) %>%
na.omit()
我设法在收集新数据后将其绘制出来(正如 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"))
现在的最终结果与问题中的图表相同,但现在用线单独连接所有点。
我正在尝试跳过带有两条线的图形 (ad13com & Pd13c) 上的 NA 值,并使用网格排列将其与第二个图形 (xPDd13C) 组合。使用我的代码,由于我的数据中的 NA 值导致换行,我正在寻找连续的线(三个独立的线)。我尝试使用 na.rm 参数但没有取得任何进展,因为我对 r 还很陌生。我意识到问题出在我正在绘制未收集的数据,但我不知道如何将 skip 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)
非常感谢!
在 data
上使用 na_omit()
的地方,结果没有分配给任何东西。您需要将结果分配给 data
(或其他东西)。
data <- data %>% filter(core_id == "BKM0817") %>%
gather(key = param, value = value, -core_id, -Age, -depth) %>%
na.omit()
我设法在收集新数据后将其绘制出来(正如 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"))
现在的最终结果与问题中的图表相同,但现在用线单独连接所有点。