使用格子

时间:2016-01-04 08:44:13

标签: r lattice

我有一个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变量是一个因素。我想使用1包中的xyplot在一个行图中绘制与集群lattice对应的F1 - F12列的所有条目。这可能吗?

我正在尝试

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

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

1 个答案:

答案 0 :(得分: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))

enter image description here

我假设你想要每个突变体一行。如果你想要每个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)

enter image description here