我不确定为什么facet_grid会生成不同于不使用facet_grid的图表。如果您对我为什么得到两个不同的结果有任何想法,请告诉我。 谢谢:))
我的代码:
tYear<-as.factor(maxMWyr$Year)
ggplot(maxMWyr, aes(group = tYear, colour = factor(tYear))) +
geom_line(aes(index, maxMW)) + facet_grid(~factor(isPeak))
但是当我在isPeak上对数据进行子集并单独创建图表时。我得到一个完全不同的图表。
maxMWyr<-subset(temp, isPeak == 1)
tYear<-as.factor(maxMWyr$Year)
ggplot(maxMWyr, aes(group = tYear, colour = factor(tYear))) +
geom_line(aes(index, maxMW))
数据示例(选择60个随机行):
maxMWyr<-structure(list(index = c(0.049349960201645, 0.133413221489914,
0.206420801273547, 0.545636109446775, 0.30622009569378, 0.14676517571885,
0.209463051568315, 0.85380467345716, 0.422723642172524, 0.4770206022187,
0.368084681445976, 0.75518341307815, 0.00699021370081885, 0.307549175970229,
0.813893312101911, 0.3654883163571, 0.913078149920255, 0.751198083067093,
0.962254120148857, 0.318290734824281, 0.574960127591707, 0.832270069112174,
0.102414433536747, 0.823447173956461, 0.869196073228973, 0.120986999204033,
0.390923566878981, 0.315202971610507, 0.676236044657097, 0.451355661881978,
0.364167995746943, 0.405943221013531, 0.762005837092067, 0.131416017575394,
0.0932382461701004, 0.062200956937799, 0.278010784901138, 0.0583067092651757,
0.108825878594249, 0.536416799574694, 0.275359424920128, 0.42740163770721,
0.871180347513481, 0.517543859649123, 0.792531948881789, 0.476608187134503,
0.149376492438313, 0.0115837827042141, 0.817118553960659, 0.616736568803675,
0.722188935490314, 0.833865814696486, 0.346046325878594, 0.0469249201277955,
0.173921725239617, 0.0341521869382864, 0.700636942675159, 0.572484025559105,
0.858585858585859, 0.458865814696486), maxMW = c(14.01552, 14.244,
6.1032, 0.78624, 8.04576, 11.13984, 17.83632, 0.2088, 11.6184,
0.73584, 0.80016, 0.43872, 17.50224, 1.3512, 0.21216, 0.80256,
0.20448, 0.25584, 0.18528, 0.97872, 0.76608, 0.45168, 11.8032,
0.36384, 0.22512, 11.18736, 0.63312, 0.82512, 0.98736, 1.01424,
5.67504, 0.672, 0.27744, 6.28608, 8.14656, 20.77248, 0.9456,
20.8848, 14.06544, 0.79728, 1.0968, 0.72912, 0.2064, 6.81024,
0.70464, 0.93888, 9.28992, 17.41584, 0.71184, 0.67008, 0.5472,
0.64704, 13.43664, 18.10848, 18.88032, 16.66032, 0.26424, 5.32224,
0.65904, 9.56016), Year = c(2016L, 2015L, 2016L, 2015L, 2015L,
2013L, 2014L, 2012L, 2014L, 2012L, 2012L, 2013L, 2015L, 2013L,
2016L, 2012L, 2013L, 2013L, 2015L, 2013L, 2015L, 2015L, 2016L,
2015L, 2016L, 2016L, 2016L, 2016L, 2014L, 2015L, 2015L, 2016L,
2016L, 2012L, 2012L, 2014L, 2012L, 2014L, 2013L, 2013L, 2013L,
2012L, 2012L, 2014L, 2014L, 2015L, 2016L, 2015L, 2014L, 2015L,
2015L, 2014L, 2014L, 2013L, 2014L, 2015L, 2016L, 2014L, 2014L,
2014L), isPeak = c(0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0,
1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0,
0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1)), .Names = c("index", "maxMW", "Year", "isPeak"), row.names = c(40313L,
27002L, 40905L, 29066L, 32493L, 9529L, 23360L, 4276L, 19681L,
6814L, 1844L, 16643L, 26369L, 14959L, 39192L, 1831L, 17237L,
12556L, 34961L, 10388L, 33504L, 34472L, 40513L, 30457L, 43403L,
40583L, 37067L, 41315L, 25116L, 33039L, 32711L, 41657L, 42999L,
659L, 5361L, 22806L, 1393L, 17856L, 9339L, 15820L, 10173L, 2141L,
4363L, 24519L, 21533L, 33134L, 40690L, 26392L, 25646L, 29422L,
29950L, 21740L, 19297L, 9029L, 18435L, 26505L, 38623L, 20431L,
25802L, 19862L), class = "data.frame")