R:叠加轨迹图和散点图
R: overlying trajectory plot and scatter plot
我正在使用 ggplot2 和轨迹图,这些图类似于散点图,但由于特定规则而使用连接点的线。
我的目标是用散点图叠加轨迹图,每个都有不同的数据。
首先是数据:
# first dataset
ideal <- data.frame(ideal=c('a','b')
,x_i=c(0.3,0.8)
,y_i=c(0.11, 0.23))
# second dataset
calculated <- data.frame(calc = c("alpha","alpha","alpha")
,time = c(1,2,3)
,x_c = c(0.1,0.9,0.3)
,y_c = c(0.01,0.26,0.17)
)
用第一个创建散点图很容易:
library(ggplot2)
ggplot(calculated, aes(x=x_c, y=y_c)) + geom_point()
之后,我使用 this helpful link:
创建了轨迹图
library(grid)
library(data.table)
qplot(x_c, y_c, data = calculated, color = calc, group = calc)+
geom_path (linetype=1, size=0.5, arrow=arrow(angle=15, type="closed"))+
geom_point (data = calculated, colour = "red")+
geom_point (shape=19, size=5, fill="black")
结果如下:
如何将ideal
数据叠加到这个轨迹图上(当然没有轨迹,它们应该只是点)?
提前致谢!
通常不推荐 qplot
。以下是绘制这两个数据框的方法。但是,如果合并数据框,ggplot 可能更适合您,并且您有一个 x
和 y
列,另外一个 method
列包含 calculated
或 ideal
.
library(ggplot2)
ideal <- data.frame(ideal=c('a','b')
,x_i=c(0.3,0.8)
,y_i=c(0.11, 0.23)
)
# second dataset
calculated <- data.frame(calc = c("alpha","alpha","alpha")
,time = c(1,2,3)
,x_c = c(0.1,0.9,0.3)
,y_c = c(0.01,0.26,0.17)
)
ggplot(aes(x_c, y_c, color = "calculated"), data = calculated) +
geom_point( size = 5) +
geom_path (linetype=1, size=0.5, arrow = arrow(angle=15, type="closed"))+
geom_point(aes(x_i, y_i, color = "ideal"), data = ideal, size = 5) +
labs(x = "x", y = "y", color = "method")
我正在使用 ggplot2 和轨迹图,这些图类似于散点图,但由于特定规则而使用连接点的线。
我的目标是用散点图叠加轨迹图,每个都有不同的数据。
首先是数据:
# first dataset
ideal <- data.frame(ideal=c('a','b')
,x_i=c(0.3,0.8)
,y_i=c(0.11, 0.23))
# second dataset
calculated <- data.frame(calc = c("alpha","alpha","alpha")
,time = c(1,2,3)
,x_c = c(0.1,0.9,0.3)
,y_c = c(0.01,0.26,0.17)
)
用第一个创建散点图很容易:
library(ggplot2)
ggplot(calculated, aes(x=x_c, y=y_c)) + geom_point()
之后,我使用 this helpful link:
创建了轨迹图library(grid)
library(data.table)
qplot(x_c, y_c, data = calculated, color = calc, group = calc)+
geom_path (linetype=1, size=0.5, arrow=arrow(angle=15, type="closed"))+
geom_point (data = calculated, colour = "red")+
geom_point (shape=19, size=5, fill="black")
结果如下:
如何将ideal
数据叠加到这个轨迹图上(当然没有轨迹,它们应该只是点)?
提前致谢!
qplot
。以下是绘制这两个数据框的方法。但是,如果合并数据框,ggplot 可能更适合您,并且您有一个 x
和 y
列,另外一个 method
列包含 calculated
或 ideal
.
library(ggplot2)
ideal <- data.frame(ideal=c('a','b')
,x_i=c(0.3,0.8)
,y_i=c(0.11, 0.23)
)
# second dataset
calculated <- data.frame(calc = c("alpha","alpha","alpha")
,time = c(1,2,3)
,x_c = c(0.1,0.9,0.3)
,y_c = c(0.01,0.26,0.17)
)
ggplot(aes(x_c, y_c, color = "calculated"), data = calculated) +
geom_point( size = 5) +
geom_path (linetype=1, size=0.5, arrow = arrow(angle=15, type="closed"))+
geom_point(aes(x_i, y_i, color = "ideal"), data = ideal, size = 5) +
labs(x = "x", y = "y", color = "method")