library(dplyr) library(pipeR) wugo1.tt <- data.frame(station = c("瑞豐站", "武慶三路", "五塊厝", "三信家商", "五權國小", "林德官(三和市場)", "光華路口(光華夜市)", "三多市場", "新光三越(捷運三多商)"), wugo1.diff = c(4, 6, 8, 8, 6, 5, 5, 4, 4), row.names = c(31, 21, 10, 5, 11, 20, 15, 3, 30), stringsAsFactors = FALSE) wugo1.std <- data.frame(station = c("瑞豐站", "永豐路口", "崗山市場", "崗山北街口", "鐵路機料廠", "鳳山工業區(監理所後)", "鐵路機廠", "武慶三路", "五塊厝"), time = c("2017/2/15 16:45", "2017/2/15 16:47", "2017/2/15 16:48", "2017/2/15 16:49", "2017/2/15 16:50", "2017/2/15 16:51", "2017/2/15 16:53", "2017/2/15 16:54", "2017/2/15 16:57"), stringsAsFactors = FALSE) wugo1.final <- wugo1.std %>>% # sort firstly arrange(time, station) %>>% mutate(rn = 1:n()) %>>% (~ xticks <- setNames(.$rn, .$station)) %>>% # left join with wugo1.tt left_join(wugo1.tt, by = "station") %>>% select(rn, station, wugo1.diff) with(wugo1.final, { plot(x = rn, y = wugo1.diff, xaxt = "n", xlab = "Station") axis(side = 1, at = xticks, labels = names(xticks)) })