【问题标题】:ggplot2 - Draw Line Graph on Categorical Variableggplot2 - 在分类变量上绘制折线图
【发布时间】:2014-10-20 19:30:05
【问题描述】:

上下文:我想代表一个过程的旅程。

样本数据:

 ____________________________________________________________________
|       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 = Time,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 轴上增加了额外的因素。

如果有其他方法,我也很乐意尝试。

提前致谢!

【问题讨论】:

  • dput(dfCheckpoints) 会很有帮助的。

标签: r ggplot2 scatter-plot


【解决方案1】:

查看 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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    相关资源
    最近更新 更多