簡體   English   中英

ggplot2-在分類變量上繪制折線圖

[英]ggplot2 - Draw Line Graph on Categorical Variable

背景:我想代表整個過程的旅程。

樣本數據:

 ____________________________________________________________________
|       Time       |   ProcessNo (factor)   |  ProcessName (factor)  |
+------------------+------------------------+------------------------+
|    2014-08-01    |           1            |      Brainstorming     |
|    2014-08-03    |           2            |      Estimation        |
|    2014-08-04    |           1            |      Brainstorming     |
|    2014-08-09    |           3            |      Construction      |
|    2014-08-14    |           4            |      Rectifying        |
+--------------------------------------------------------------------+

我使用ggplot2繪制了一個圖,其中x =時間,y = ProcessName。

p <- ggplot(dfCheckpoints, aes(Time, ProcessName))
p <- p + geom_point()

問題:我想按時間順序覆蓋連接“流程”的一行。 如果有幫助,ProcessNo只是ProcessName變量的因子級別。

我嘗試添加新行:

p <- p + geom_line(data = dfCheckpoints, aes(x = Time, y = ProcessNo))

但它在Y軸上增加了額外的因素。

如果還有其他方法,我也很樂意嘗試。

提前致謝!

查看geom_line的在線文檔,我認為您需要在那里的分組變量。 這給出了我認為您所要求的。

require("ggplot2")
require("lubridate")

dfCheckpoints$Time <- ymd(dfCheckpoints$Time)
dfCheckpoints$ProcessName <- as.character(dfCheckpoints$ProcessName)
dfCheckpoints$group <- 1

p <- ggplot(dfCheckpoints, aes(Time, ProcessName, group = group)) + 
  geom_point() + geom_line()
p

對於其他嘗試此操作的人,這是我對數據的解釋的dput()

structure(list(Time = structure(1:5, .Label = c("2014-08-01", 
"2014-08-03", "2014-08-04", "2014-08-09", "2014-08-14"), class = "factor"), 
    ProcessNo = structure(c(1L, 2L, 1L, 3L, 4L), .Label = c("1", 
    "2", "3", "4"), class = "factor"), ProcessName = structure(c(1L, 
    3L, 1L, 2L, 4L), .Label = c("Brainstorming", "Construction", 
    "Estimation", "Rectifying"), class = "factor")), .Names = c("Time", 
"ProcessNo", "ProcessName"), row.names = c(NA, -5L), class = "data.frame")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM