R 避免在ggplot中循环和更改及绘制比例

R 避免在ggplot中循环和更改及绘制比例,r,ggplot2,R,Ggplot2,数据集示例:我从A到B到C或以相反顺序旅行的数据集。我想根据下面的代码绘制我的旅行路线。此外,我希望有一个y刻度,这样它显示B和C之间的距离大于A和B。在旅行路线上,可以假设所有人的距离相等。另外,我希望能为我的循环提供一些反馈和替代解决方案 我的代码: library(ggplot2) df <- data.frame(Checkpoint = rep(c("A", "B", "C"), 3), Route = as.factor(rep(1:3, e

数据集示例:我从A到B到C或以相反顺序旅行的数据集。我想根据下面的代码绘制我的旅行路线。此外,我希望有一个y刻度,这样它显示B和C之间的距离大于A和B。在旅行路线上,可以假设所有人的距离相等。另外,我希望能为我的循环提供一些反馈和替代解决方案

我的代码:

library(ggplot2)

df <- data.frame(Checkpoint = rep(c("A", "B", "C"), 3),
                 Route = as.factor(rep(1:3, each = 3)),
                 Times = as.POSIXct(c("4:10", "4:25", "5:00", "8:00", "8:22", 
                                      "9:00","10:00", "10:15", "10:50"), format = "%H:%M"))

df[7:9,1]  <- rev(df[7:9,1])

# If travelled from A to C, then F1, F2

df$Direction <- NA

for(i in 1:length(levels(df$Route))){

  y <- which(df$Route == i)
  x <- filter(df, Route == i)

  l <- ifelse(x[x$Checkpoint == "A", "Times"]  < x[x$Checkpoint == "B", "Times"], "F1", "F2")

  df$Direction[y] <-l

} ; rm(y,x,l,i)


ggplot(df, aes(x=Times, color = Route, y= Checkpoint, group=Route))  +
  geom_line() + 
  theme_light() 
库(ggplot2)

df您可以使用
split
for循环
替换为以下代码,速度应该更快,因为您不需要在循环中多次迭代地
子集

df <- do.call(rbind, lapply(split(df, df$Route), 
       function(x) {
         x['Direction'] <- ifelse(x[x['Checkpoint'] == "A", "Times"]  < x[x['Checkpoint'] == "B", "Times"], "F1", "F2")
         x}))

您还可以使用plyr软件包,并在一行中完成此操作:

library(plyr)
ddply(df,c('Route'),transform,Direction=ifelse(Times[3]>Times[1]&Checkpoint[1]=='A','A->C','C->A'))


   Checkpoint Route               Times Direction
    1          A     1 2017-01-30 04:10:00      A->C
    2          B     1 2017-01-30 04:25:00      A->C
    3          C     1 2017-01-30 05:00:00      A->C
    4          A     2 2017-01-30 08:00:00      A->C
    5          B     2 2017-01-30 08:22:00      A->C
    6          C     2 2017-01-30 09:00:00      A->C
    7          C     3 2017-01-30 10:00:00      C->A
    8          B     3 2017-01-30 10:15:00      C->A
    9          A     3 2017-01-30 10:50:00      C->A

diff在绘图时,您在哪里使用方向?我不确定此绘图的目的是什么,但与其改变比例,不如获得检查点之间的距离(甚至粗略估计)和绘图距离(x)与时间(y)的关系,这不是更合逻辑吗?@Wave正是我想要做的。但是我仍然希望检查点A/B/C在Y轴上。“在绘图时,你在哪里使用方向?”我没有,没有想到,但我稍后会使用它。
library(plyr)
ddply(df,c('Route'),transform,Direction=ifelse(Times[3]>Times[1]&Checkpoint[1]=='A','A->C','C->A'))


   Checkpoint Route               Times Direction
    1          A     1 2017-01-30 04:10:00      A->C
    2          B     1 2017-01-30 04:25:00      A->C
    3          C     1 2017-01-30 05:00:00      A->C
    4          A     2 2017-01-30 08:00:00      A->C
    5          B     2 2017-01-30 08:22:00      A->C
    6          C     2 2017-01-30 09:00:00      A->C
    7          C     3 2017-01-30 10:00:00      C->A
    8          B     3 2017-01-30 10:15:00      C->A
    9          A     3 2017-01-30 10:50:00      C->A
diff <- df[1:3,]
diff$time <- diff$Times - diff$Times[1]
diff <- diff[,c(1,5)]

df <- merge(df, diff)
df$time <- as.numeric(df$time)


ggplot(df, aes(x=Times, color = Route, y= time, group=Route))  +
  geom_line() + 
  scale_y_continuous(breaks = unique(df$time), labels = c("A","B","C")) +
  theme_light()