有没有办法在 ggplot2 中跳过线图的 NA 值
Is there a way to skip NA values for a line plot in ggplot2
我正在尝试绘制 4 个共享 y 轴的折线图,但其中一个图有缺失值 (NA)。
我希望能够连接 CI-TR 图的 NA 值两侧的两个点。
这是我正在处理的数据(注意 CI-右侧的 TR 具有 NA 值)
这是我的代码,我已经从 excel 文件中读取了 table:
#read the excel file (same as the table attached)
data <- read_csv("test3.csv", col_types = cols(.default = col_guess())
# gather the data for age and depth
plots <- data %>% filter(core_id == "BKM0817") %>%
gather(key = param, value = value, -core_id, -Age, -depth)
#this is to relabel the graph titles (NB the added a is to order alphabetically in the order I want them to appear)
plots %>%
mutate(facet_label = fct_recode(
param,
"delta ^ 13 * C[OM] ~(`\u2030 V-PDB`)" = "ad13com",
"delta ^ 15 * N ~(`\u2030 AIR`)" = "d15N",
"'C/N'" = "C/N",
"CI-TR" = "aaCI-TR"
)) %>%
# now plot the graphs
ggplot(aes(y = Age, x = value)) +
geom_hline(yintercept = c(10720, 10568, 10620), col = "black", alpha = 0.8, lty = 2) +
geom_lineh(colour = "black", size = 0.5) +
geom_point(size = 2) +
facet_wrap(~facet_label, scales = "free_x", labeller = label_parsed, ncol = 4) +
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 = NULL, y = "Age (Cal. yrs BP)") +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))`
如果有任何 NA
值,线 geom 将被切断,因此简单的解决方案是从数据框中删除 NA
值。您可以使用 na.omit()
来执行此操作,但只需注意 您在代码中使用它的地方 。您的原始数据集如下所示:
df <- data.frame(pos=1:4, A=c(1.05, 2.3, 4.24, 3.89),
B=c(4.44, NA, 2.22, 3.33))
df
>
pos A B
1 1 1.05 4.44
2 2 2.30 NA
3 3 4.24 2.22
4 4 3.89 3.33
收集后绘制此图你得到:
df %>%
gather(key=type, value=value, -pos) %>%
ggplot(aes(x=pos, y=value)) +
geom_line(linetype=2, color='blue', size=0.7) +
geom_point(color='red', size=3) +
facet_wrap(~type)
如果您在 df
上使用 na.omit()
,那么它会删除 整个第二行 ,这会删除第 [=19= 列的第二个观察值】 还有。在这种情况下,只需确保在 之后使用 na.omit()
gather()
函数将数据帧旋转得更长:
df %>%
gather(key=type, value=value, -pos) %>%
na.omit() %>% # important this comes after the gather
ggplot(aes(x=pos, y=value)) +
geom_line(linetype=2, color='blue', size=0.7) +
geom_point(color='red', size=3) +
facet_wrap(~type)
在您的情况下,下面的伪代码让您了解在您自己的代码中放置 na.omit()
的位置:
plots %>%
mutate(...) %>%
na.omit() %>%
ggplot(...) + ...
我正在尝试绘制 4 个共享 y 轴的折线图,但其中一个图有缺失值 (NA)。
我希望能够连接 CI-TR 图的 NA 值两侧的两个点。
这是我正在处理的数据(注意 CI-右侧的 TR 具有 NA 值)
这是我的代码,我已经从 excel 文件中读取了 table:
#read the excel file (same as the table attached)
data <- read_csv("test3.csv", col_types = cols(.default = col_guess())
# gather the data for age and depth
plots <- data %>% filter(core_id == "BKM0817") %>%
gather(key = param, value = value, -core_id, -Age, -depth)
#this is to relabel the graph titles (NB the added a is to order alphabetically in the order I want them to appear)
plots %>%
mutate(facet_label = fct_recode(
param,
"delta ^ 13 * C[OM] ~(`\u2030 V-PDB`)" = "ad13com",
"delta ^ 15 * N ~(`\u2030 AIR`)" = "d15N",
"'C/N'" = "C/N",
"CI-TR" = "aaCI-TR"
)) %>%
# now plot the graphs
ggplot(aes(y = Age, x = value)) +
geom_hline(yintercept = c(10720, 10568, 10620), col = "black", alpha = 0.8, lty = 2) +
geom_lineh(colour = "black", size = 0.5) +
geom_point(size = 2) +
facet_wrap(~facet_label, scales = "free_x", labeller = label_parsed, ncol = 4) +
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 = NULL, y = "Age (Cal. yrs BP)") +
theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))`
如果有任何 NA
值,线 geom 将被切断,因此简单的解决方案是从数据框中删除 NA
值。您可以使用 na.omit()
来执行此操作,但只需注意 您在代码中使用它的地方 。您的原始数据集如下所示:
df <- data.frame(pos=1:4, A=c(1.05, 2.3, 4.24, 3.89),
B=c(4.44, NA, 2.22, 3.33))
df
>
pos A B
1 1 1.05 4.44
2 2 2.30 NA
3 3 4.24 2.22
4 4 3.89 3.33
收集后绘制此图你得到:
df %>%
gather(key=type, value=value, -pos) %>%
ggplot(aes(x=pos, y=value)) +
geom_line(linetype=2, color='blue', size=0.7) +
geom_point(color='red', size=3) +
facet_wrap(~type)
如果您在 df
上使用 na.omit()
,那么它会删除 整个第二行 ,这会删除第 [=19= 列的第二个观察值】 还有。在这种情况下,只需确保在 之后使用 na.omit()
gather()
函数将数据帧旋转得更长:
df %>%
gather(key=type, value=value, -pos) %>%
na.omit() %>% # important this comes after the gather
ggplot(aes(x=pos, y=value)) +
geom_line(linetype=2, color='blue', size=0.7) +
geom_point(color='red', size=3) +
facet_wrap(~type)
在您的情况下,下面的伪代码让您了解在您自己的代码中放置 na.omit()
的位置:
plots %>%
mutate(...) %>%
na.omit() %>%
ggplot(...) + ...