【问题标题】:Label line in qplotqplot 中的标签线
【发布时间】:2012-01-11 01:02:43
【问题描述】:

我有一个qplot,它显示了 5 个不同的分组(用 colour = type 表示),每个分组有两个因变量。命令如下所示:

qplot(data = data, x = day, y = var1, geom = "line", colour = type) + 
      geom_line(aes(y = var2, colour = value)

我想标记这两条不同的线,以便我可以分辨出哪五个代表 var1,哪五个代表 var2。

我该怎么做?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以将数据转换为“高”格式,使用melt,并使用另一种美学(例如线型)来区分变量。

    # Sample data
    n <- 100
    k <- 5
    d <- data.frame(
      day = rep(1:n,k),
      type = factor(rep(1:k, each=n)),
      var1 = as.vector( replicate(k, cumsum(rnorm(n))) ),
      var2 = as.vector( replicate(k, cumsum(rnorm(n))) )
    )
    
    # Normalize the data
    library(reshape2)
    d <- melt(d, id.vars=c("day","type"))
    
    # Plot
    library(ggplot2)
    ggplot(d) + geom_line(aes(x=day, y=value, colour=type, linetype=variable))
    

    【讨论】:

    • 这行得通;这是最简单的方法吗?有没有一种方法可以在行上标记一个单词?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多