简体   繁体   中英

Plot multiple lines using xyplot from lattice

I have a 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

The cluster variable is a factor. I would like to plot all entries from columns F1 - F12 corresponding to cluster 1 together in a lines-plot using xyplot from the lattice package. Is this possible?

I'm trying

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

but I get an error about something like (list) Object can not be converted to to double .

Convert to long format and you're good to go. Here's an example using the data that you've provided:

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))

在此处输入图片说明

I made the assumption that you want one line per mutant. If you wanted a line per F, and mutants along the x-axis, then just change to xyplot(x=value ~ Mutant|Fx, type='l') .

If you're open to using base graphics, you can plot a matrix with 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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM