R:用for循环绘制独特的线条

时间:2017-06-20 22:22:29

标签: r plot

dat <- list(c(1.12157347682574, 1.33192942183165, 2.08170603538164, 
3.21936891077735, 3.87674160966784, 4.01222877372179, 3.97721253044977, 
3.92748085243377, 3.89422132704385, 3.87554992373411, 3.86571722337484, 
3.86066855038186, 3.85810016465002, 3.85679624059312, 3.85613341785451, 
3.85579555076542, 3.85562274484616, 3.85553404933868, 3.85548836742791, 
3.85546476209749, 3.85545252710548), c(1.10529328957704, 1.31478057418982, 
2.05843350413484, 3.21445290319435, 3.93127780496161, 4.10019570732597, 
4.0548974496435, 3.97577600575589, 3.91388817353566, 3.87397233241435, 
3.85011420707963, 3.83631205949334, 3.82843188274012, 3.82395020512325, 
3.82139984806649, 3.81994467228049, 3.81911146133043, 3.81863258400429, 
3.81835633714707, 3.81819642258335, 3.81810355018493), c(1.08732237015566, 
1.29744314553374, 2.03681100176799, 3.18820303674863, 3.90649946705568, 
4.08059333708303, 4.04639279053423, 3.98156199908701, 3.93264549967637, 
3.90244185986869, 3.88514800191734, 3.87554934102343, 3.87028551934942, 
3.86740818622607, 3.86583394198314, 3.86497024645408, 3.86449471113229, 
3.86423191258367, 3.86408614948732, 3.86400502180298, 3.86395972379237
), c(1.30161877569605, 1.46553776200827, 2.10197173547206, 3.06930201699535, 
3.66250999070993, 3.85723600464989, 3.8942042549933, 3.88392091929778, 
3.8605155020708, 3.83375521970562, 3.80643844775653, 3.77938071354659, 
3.75282366570479, 3.72683925144018, 3.70144788585054, 3.67665362718513, 
3.65245483803303, 3.62884749589963, 3.60582627074711, 3.58338491537713, 
3.5615164406438), c(1.08469594718265, 1.29546127452487, 2.03810539840721, 
3.18905959610048, 3.89746111531108, 4.06513051117241, 4.03208432847638, 
3.97188967237102, 3.92762525203426, 3.90089820027383, 3.88590086923236, 
3.87773268456738, 3.87333409971113, 3.87097228241208, 3.8697027802883, 
3.86901848471182, 3.86864832970741, 3.86844736049309, 3.86833785249504, 
3.86827797747177, 3.86824513600819), c(1.07964052838977, 1.29190777425215, 
2.04231250239992, 3.19798917985082, 3.89723898066506, 4.05757629447758, 
4.02387699444488, 3.96603112527963, 3.9243602259927, 3.89958493083309, 
3.88586527520208, 3.87848244531751, 3.87455196717863, 3.87246483056295, 
3.87135520285416, 3.87076356341284, 3.87044698746471, 3.87027696477127, 
3.87018532072631, 3.87013575499355, 3.87010886245731), c(1.08933181649253, 
1.29901388665636, 2.03604356337407, 3.18580149536436, 3.9065276727864, 
4.08284397088934, 4.04915431500807, 3.9839665676791, 3.93452404770496, 
3.90388767912571, 3.88629572919666, 3.87650700142078, 3.8711264587724, 
3.8681788052111, 3.86656261507012, 3.86567402891195, 3.86518376322719, 
3.86491225889191, 3.86476135376567, 3.86467718966398, 3.86463009866786
), c(1.12459738833516, 1.33429622029097, 2.08134547519017, 3.21607128232391, 
3.87334654404529, 4.0092794872425, 3.97432485460807, 3.92449513705583, 
3.89113365765329, 3.87239201995412, 3.86251706226948, 3.85744441841396, 
3.85486281767585, 3.85355171294798, 3.85288501684111, 3.8525450675694, 
3.85237114377774, 3.85228184815236, 3.8522358438795, 3.85221206522138, 
3.85219973692982), c(1.08152696725377, 1.29354595382903, 2.04268473144721, 
3.19946254279135, 3.90444884340271, 4.06790786857492, 4.0329743488585, 
3.9721635966533, 3.92774092720633, 3.9009926112924, 3.88600650759486, 
3.87785240458544, 3.87346429522664, 3.87110919016533, 3.86984371655882, 
3.869161759929, 3.86879293902871, 3.86859272357885, 3.86848363941323, 
3.86842400221486, 3.86839129414273), c(1.08197483401857, 1.29359825225018, 
2.04082955314597, 3.19437482098117, 3.89680650725515, 4.05972606592981, 
4.02617525912275, 3.96741176617648, 3.92476392035391, 3.89926386772869, 
3.88507394855001, 3.87740396348501, 3.87330326448182, 3.87111672936341, 
3.86994950869332, 3.86932463408538, 3.86898891944037, 3.86880788736096, 
3.86870991350275, 3.86865670925241, 3.86862772534133), c(1.08216196424983, 
1.29413465287689, 2.04304891046787, 3.19856102707185, 3.90114775913897, 
4.0634953181634, 4.02931505555857, 3.97000974912286, 3.92696706310058, 
3.90120957987481, 3.88686190905547, 3.87909827895443, 3.87494285145371, 
3.87272456890303, 3.8715389950979, 3.87090352665212, 3.87056170077119, 
3.87037714404976, 3.87027713742418, 3.87022276077104, 3.87019310063444
), c(1.08208080422425, 1.29409482026275, 2.04322281433486, 3.19877157213528, 
3.90088920797055, 4.06293059188269, 4.02879633885981, 3.96968626960857, 
3.92683419521334, 3.90121559093586, 3.88695769522339, 3.879248936171, 
3.87512613020615, 3.87292696985694, 3.87175252322308, 3.87112350463803, 
3.87078540971225, 3.87060300879754, 3.87050424709519, 3.87045058912723, 
3.87042134381434), c(1.08220050068092, 1.29416045954134, 2.04300889772717, 
3.19847760351553, 3.90113372450959, 4.06353948547856, 4.02936728462705, 
3.97004710522954, 3.92698700475469, 3.90121623176852, 3.88685981428833, 
3.87909079476742, 3.87493215130221, 3.87271198077119, 3.87152530797643, 
3.87088920267017, 3.87054700860192, 3.87036223928244, 3.8702621099747, 
3.87020766254669, 3.87017796158474), c(1.08207245581171, 1.29409069738142, 
2.04324072847744, 3.1987945048703, 3.90086667118013, 4.06287780648447, 
4.02874718343757, 3.96965515642479, 3.92682083928555, 3.90121519699921, 
3.88696568414963, 3.87926204867567, 3.87514228806757, 3.87294491227269, 
3.87177150366836, 3.87114308652571, 3.87080533935571, 3.87062313929028, 
3.87052449352086, 3.87047090244975, 3.87044169573205), c(1.08218708599303, 
1.29415164868573, 2.0430238507559, 3.19850707937143, 3.90113545482259, 
4.06351892102187, 4.02934466379532, 3.97003155465113, 3.92697929062447, 
3.90121442227887, 3.88686187260142, 3.87909523146381, 3.87493800601191, 
3.87271866772437, 3.87153247939674, 3.87089665489438, 3.87055462321291, 
3.87036994768823, 3.87026987252343, 3.87021545634019, 3.87018577340675
))

par(mfrow = c(1, 1))
plot(NA, ylim = c(0, 7), xlim = c(0, 20))
for(i in 1:15){
  lines(dat[[i]], type = "l", col = colorRampPalette(c("blue", "red"))(i)) 
}

我正在尝试将15条曲线绘制到for循环中的同一个图形上。理想情况下,我希望能够轻松区分它们。我也试过lines(dat[[i]], type = "l", col = rainbow(i)),但那不是运气。我只想要一个有15条曲线的情节,每条曲线都有自己独特的颜色。

3 个答案:

答案 0 :(得分:0)

以下是ggplot2

的解决方案
library(ggplot2)

# reformat the data
data_long = data.frame( x = numeric(), y = numeric(), curve = character() )
for( i in 1:length(dat) ){
  y = dat[i][[1]]
  x = 1:length( y )
  data_long = rbind( data_long, data.frame( x, y, curve = i ) )
}
data_long$curve = factor( data_long$curve )

# create plot
ggplot( data_long, aes( x, y, color = curve ) ) +geom_line() + theme_bw()

enter image description here

答案 1 :(得分:0)

你可以试试这个:

par(mfrow = c(1, 1))
plot(NA, ylim = c(1, 4), xlim = c(0, 10)) #better view over the lines

mypal <- colorRampPalette( c( "red", "green", "blue" ) )( 15 )

for(i in 1:15){
         lines(dat[[i]], type = "l", col = mypal[i], , lwd=1.5) #thickness set to be higher
              }

将绘制:

enter image description here

答案 2 :(得分:0)

这里是rainbow(),加上一个传奇......

par(mfrow = c(1, 1))
plot(NA, ylim = c(0, 7), xlim = c(0, 20))

cols <- rainbow(15)
# cols <- heat.colors(15)
# cols <- terrain.colors(15)
# cols <- cm.colors(15)

# I like a darker rainbow
# darkerizer <- function(colors, dark=.85) adjustcolor(colors, red.f=dark, green.f=dark, blue.f=dark)
# cols <- darkerizer(rainbow(15), dark=.85) 

for(i in 1:15) lines(dat[[i]], col = cols[i], lwd=2)
legend("topleft", legend=paste("number",1:15), lwd=2, col=cols)