我有一些时间序列的数据,其中观察到一些数据并且模拟了一些数据。我想生成一个随时间变化的整个数据系列图,颜色表示数据源。但是,我只能弄清楚如何在同一组中的ggplot连接点中制作geom_line()。
这是一个澄清的例子:
# Create sample data
df <- data.frame(cbind(seq(1,9,1), c(1,2,3,4,5,4,3,2,1), c("obs","obs", "obs", "obs", "sim","sim","obs","sim", "obs")))
colnames(df) <- c("time", "value", "source")
# Make a plot
p <- ggplot(df, aes(x=time, y=value, group=source, color=source))
p + geom_point() # shows all the points in sequential order as dots
p + geom_point() + geom_line() # connects obs to obs and sim to sim
在这个例子中,我希望一条线在x轴上从1:9顺序连接,连接所有点,但是根据组改变线条(和点)的颜色。
答案 0 :(得分:5)
df <- data.frame(cbind(
seq(1,9,1),
c(1,2,3,4,5,4,3,2,1),
c("obs","obs","obs","obs","sim","sim","obs","sim","obs"),
c("all","all","all","all","all","all","all","all","all")))
colnames(df) <- c("time", "value", "source", "group")
ggplot(df,aes(x=time,y=value)) +
geom_point(aes(colour=source)) +
geom_path(data=df,aes(y=value,x=time,group=group,colour=source))