【问题标题】:Plot multiple lines using xyplot from lattice使用 lattice 中的 xyplot 绘制多条线
【发布时间】:2016-01-04 08:44:13
【问题描述】:

我有一个 data.table:

> fin
     Mutant       F1       F2       F3       F4       F5       F6       F7       F8       F9     F10      F11 F12 cluster
  1:  A115D  6.53193  7.19020  8.45634  8.49147  9.28304 16.83618 10.70517 16.14696 12.04896 7.88130  3.47452   0       7
  2:  A115F  0.90377  4.33477  5.71287  6.63125  5.86933  9.41705 14.59203 17.42900 14.16879 8.93167  1.97882   0      10
  3:  A115G  3.26668  4.46146  5.42433  7.80924  8.52429 10.92138 11.27432 14.89700 10.70361 7.08529  3.02807   0       4
  4:  A115H  2.91278  5.09545  6.01828  8.18154  8.11368 11.98551 11.33009 16.63858 12.19801 7.77392  3.16354   0       4
  5:  A115I  9.35627  9.29640  9.78475 10.76222 12.80510 16.13456 16.51090 17.98271 13.80018 9.06526  4.72650   0      15
 ---                                                                                                                     
299:   Y80R -1.19326 -2.05579 -1.16474  1.74387  4.79593  5.59487 11.35956 13.45000  9.71288 5.79897 -0.60654   0       9
300:   Y80S -0.77282 -1.51611 -0.07168  3.16070  3.16795  7.73116 11.60527 14.05083  9.47901 5.80185  1.60430   0       6
301:   Y80T -0.16135 -0.05859  2.02493  3.28120  6.10268 11.71562 12.45665 13.96543 10.07102 5.98976  1.64061   0       3
302:   Y80V -0.24050 -0.59869  0.36746  3.07046  3.75905  9.17579 11.83179 14.90189 12.79275 8.46631  3.94015   0       6
303:   Y80W  0.77770 -0.10166  2.27790  6.11470  6.01080  9.47050 13.95344 18.42320 14.78544 9.10575  4.64121   0      10

cluster 变量是一个因素。我想使用lattice 包中的xyplot 将与集群1 对应的列F1 - F12 中的所有条目一起绘制成线条图。这可能吗?

我在努力

> xyplot(fin[fin$cluster==1, .SD, .SDcols=2:13]~1:12, type="l")

但我收到关于 (list) Object can not be converted to to double 之类的错误。

【问题讨论】:

  • 这是否意味着您希望在同一个图上使用 12 条线,其中每条线代表不同的 F 组?
  • 我没有看到要尝试的数据,也不清楚您的 x 应该是什么。但是您可以使用F1+F2+F3~x 之类的公式。这有帮助吗?

标签: r lattice


【解决方案1】:

转换为长格式,一切顺利。以下是使用您提供的数据的示例:

library(tidyr)
library(dplyr)
fin %>% 
  gather(Fx, value, -Mutant, -cluster) %>% 
  filter(cluster==4) %>%
  xyplot(x=value ~ Fx|Mutant, type='l', scales=list(alternating=FALSE, tck=1:0))

我假设您希望每个突变体有一条线。如果您希望每个 F 有一条线,以及沿 x 轴的突变体,则只需更改为 xyplot(x=value ~ Mutant|Fx, type='l')

如果您愿意使用base 图形,您可以使用matplot 绘制矩阵:

par(oma=c(0, 0, 0, 5))
matplot(t(fin[fin$cluster==4, 2:13, with=FALSE]), type='l', 
        lty=1, col=1:2, ylab='Value', xlab='', xaxt='n', las=1)
axis(1, at=1:12, labels=colnames(fin)[2:13])
legend(par('usr')[2], par('usr')[4], fin$Mutant[fin$cluster==4], col=1:2, 
       lty=1, bty='n', xpd=NA)

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2013-06-30
    • 2020-09-25
    • 1970-01-01
    • 2016-04-18
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多