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

时间:2017-01-30 20:00:08

标签: 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, 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() 

3 个答案:

答案 0 :(得分:0)

您可以使用for loop通过以下代码替换split,它应该更快,因为您不需要在循环内多次subset您的数据,

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

它应该给你以下相同的输出。

    Checkpoint Route               Times Direction
1.1          A     1 2017-01-31 04:10:00        F1
1.2          B     1 2017-01-31 04:25:00        F1
1.3          C     1 2017-01-31 05:00:00        F1
2.4          A     2 2017-01-31 08:00:00        F1
2.5          B     2 2017-01-31 08:22:00        F1
2.6          C     2 2017-01-31 09:00:00        F1
3.7          C     3 2017-01-31 10:00:00        F2
3.8          B     3 2017-01-31 10:15:00        F2
3.9          A     3 2017-01-31 10:50:00        F2

答案 1 :(得分:0)

您也可以使用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

答案 2 :(得分:0)

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