[英]How do I correctly connect data points ggplot
我正在制作地层 plot 但不知何故,我的数据点连接不正确。
此 plot 的目的是连接 x 轴上的值,以便您了解 d18O 随时间(年龄,ma)的变化。
我使用了以下脚本:
library(readxl)
R_pliocene_tot <- read_excel("Desktop/R_d18o.xlsx")
View(R_pliocene_tot)
install.packages("analogue")
install.packages("gridExtra")
library(tidyverse)
R_pliocene_Rtot <- R_pliocene_tot %>%
gather(key=param, value=value, -age_ma)
R_pliocene_Rtot
R_pliocene_Rtot %>%
ggplot(aes(x=value, y=age_ma)) +
geom_path() +
geom_point() +
facet_wrap(~param, scales = "free_x") +
scale_y_reverse() +
labs(x = NULL, y = "Age (ma)")
这导致下图:
我猜geom_path
function 出了点问题,但我不知道它是什么。
尽管评论似乎解决了问题,但我认为所提出的问题并未得到解答。 所以这里是关于ggplot2
库关于geom_path
的一些介绍
library(dplyr)
library(ggplot2)
# This dataset contain two group with random value for y and x run from 1->20
# The param is just to replicate the question param variable.
df <- tibble(x = rep(seq(1, 20, by = 1), 2),
y = runif(40, min = 1, max = 100),
group = c(rep("group 1", 20), rep("group 2", 20)),
param = rep("a param", 40))
df %>%
ggplot(aes(x = x, y = y)) +
# In geom_path there is group aesthetics which help the function to know
# which data point should is in which path.
# The one in the same group will be connected together.
# here I use the color to help distinct the path a bit more.
geom_path(aes(group = group, color = group)) +
geom_point() +
facet_wrap(~param, scales = "free_x") +
scale_y_reverse() +
labs(x = NULL, y = "Age (ma)")
在您与group = 1
配合良好的数据中,我猜所有数据点都属于一个组,您只想画一条线连接所有这些数据点。 因此,以我上面的数据示例并使用美学group = 1
进行绘制,您可以看到与上例类似的两条线的结果,但现在group 1
的终点现在与group 2
的起点相连。 因此,所有数据点现在都在一条路径上,但它们绘制的顺序取决于它们在数据中出现的顺序。 (我保留颜色只是为了帮助看清楚一点)
df %>%
ggplot(aes(x = x, y = y)) +
geom_path(aes(group = 1, color = group)) +
geom_point() +
facet_wrap(~param, scales = "free_x") +
scale_y_reverse() +
labs(x = NULL, y = "Age (ma)")
希望这能让您更好地理解ggplot2::geom_path
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.