简体   繁体   中英

R: overlying trajectory plot and scatter plot


I'm working with ggplot2 and trajectory plots, plots whom are like scatter plots, but with lines that connect points due a specific rule.
My goal is to overlay a trajectory plot with a scatter plot, and each of them has different data. First of all, the data:

# 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)
                     )

Creating a scatter plot with the first one is easy:

library(ggplot2)
ggplot(calculated, aes(x=x_c, y=y_c)) + geom_point()

After that, I created the trajectory plot, using 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")

With this result:

在此处输入图片说明

How can I overlay the ideal data to this trajectory plot (without trajectory of course, they should be only points)?
Thanks in advance!

qplot isn't usually recommended. Here's how you could plot the two dataframes. However, ggplot might work better for you if the dataframes were merged, and you had an x and y column, with an additional method column containing with calculated or 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")

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM