0

我遇到了“格子”图形等高线图的问题。我一直在尝试使用下面的代码创建等高线图,并且遇到了显示范围的问题。基本上,contourpplot 函数使用我的 response(z) 变量 ABSerror 的范围(见下文)。

ABSerror http://danielcoakley.com/project/Testing/Temperature.png

但是,绘制的数据不会达到这些“最小、最大”限制,因为它们是由预测变量(小时、工作日)平均的。是否可以计算点阵图的显示范围并将其设置为颜色图例的新限制?我不想为 z 向量设置绝对限制,因为这会使代码不那么健壮,并且每次运行时都需要更改。

plot<-contourplot(ABStemp ~ hour * weekday,
                   aspect=0.3,
                   data = df,
                   cuts = 200,
                   #labels=TRUE,
                   contour=FALSE,
                   region = TRUE,
                   pretty=FALSE,
                   xlab = "Hour of Day",
                   ylab = "Day of Week",
                   col.regions=colorRampPalette(c("blue","yellow","red")),
                   zlim = range(100, finite = TRUE)
)

我在下面包含了 500 行数据框:

    df<-structure(list(ABStemp = c(2.33891051530419, 4.66977185341213, 
7.69092629549917, 11.1259634956075, 12.4989344259194, 10.4766508811911, 
12.1333252861115, 13.1427111020819, 13.8395728027101, 14.3513578101205, 
10.1150137331259, 5.14486679995218, 0.584418923890536, 2.36822622616402, 
1.90256632417697, 10.8309946541286, 14.0244639969632, 11.750660392261, 
10.9786334115844, 10.8389489143245, 9.88807810478433, 8.41323248299517, 
7.76005654828338, 2.23432319307088, 1.42651575885196, 3.97818093025987, 
5.98364201068521, 12.1327113715202, 16.6630927878909, 19.6025409795866, 
22.3533380207585, 21.6664228195007, 19.566910344456, 17.5305602166806, 
11.4138883749256, 4.47846199645294, 1.18418216015379, 4.95907843935502, 
4.15198230849739, 9.67593500154633, 14.0796157127847, 13.6682636683508, 
12.722836336779, 10.7137810035488, 9.31647396260888, 8.4024476064816, 
8.19525956966761, 3.37281771175294, 1.56437287656987, 5.40527765711733, 
13.5646511429841, 19.9655662099298, 23.6707556603167, 23.041197437406, 
21.3744768866254, 19.4214241150162, 19.1913001394706, 20.4763853858403, 
15.5308197426484, 9.40558035637685, 2.71528217047851, 3.08101029716437, 
3.37055247240881, 13.7953427517124, 18.6345920345412, 17.9883673749729, 
16.1619966461944, 14.4532078155512, 13.438212587822, 12.3720331692208, 
11.5153279333021, 1.81452101474621, 0.839654322007979, 4.1615787024834, 
11.6661758369217, 18.6811456671031, 23.2047102887316, 24.6750744255767, 
25.0222357414079, 25.7827288787888, 25.9782548940527, 24.6556924738125, 
20.5127706187546, 16.2556172057107, 12.5271296183477, 10.9611131864043, 
10.6150951792667, 22.3928563301905, 26.6681067231922, 24.6755909702781, 
23.2147294729545, 23.1323425131293, 23.3963336947643, 23.5573438900773, 
24.0301828927208, 8.49753243473921, 10.1805006078075, 12.2889694647281, 
15.7115236865939, 19.1085647003559, 21.1845732121901, 21.9782038074943, 
24.8715308127409, 24.0473711694237, 24.6904786262923, 22.5355252564063, 
20.4151021787812, 16.9746540335269, 9.24661888836837, 3.79265111395731, 
1.27631858358483, 12.402645060579, 17.2985901762333, 17.1313376423964, 
17.729811376458, 16.8225534613836, 15.1472900538297, 14.0517596861097, 
14.2376128219004, 0.308278216436768, 3.84192101024975, 13.188327199087, 
20.7074441529515, 27.3158262894137, 30.3349400179668, 31.3056271507191, 
32.5009019988318, 34.8630477283387, 33.374981820956, 30.2674519850418, 
24.9188176080028, 21.1840570064588, 17.9686717995784, 14.3467483048419, 
11.2691128813087, 23.8337490161465, 29.0849391270845, 29.6248186944942, 
29.386012054552, 28.7637847709596, 28.9110971378641, 28.8277975551496, 
28.8258095725158, 11.1482020511646, 10.3048686249461, 11.5537911454279, 
12.6487635415593, 15.141051096692, 18.4207035759421, 20.3921364957162, 
20.6986391864082, 22.3339225779584, 28.0766456499598, 30.2756390992694, 
28.4911213679877, 24.9888695614122, 20.4586080547411, 15.9841052888571, 
12.8901223486949, 24.4428240988806, 29.5586648421351, 28.3313230515127, 
27.0962382671756, 26.6790294807476, 26.3573300936104, 25.7992038364494, 
25.4855976543897, 7.35840040803988, 7.25201675398457, 9.725461523272, 
12.1342164312733, 13.1300616778934, 11.0174841271484, 10.4754255228911, 
9.50442956311057, 9.9402468322571, 10.5492444949961, 6.73672003535463, 
0.404446353941678, 2.68410551115763, 3.92625944672239, 3.83526016159428, 
2.99582288008988, 10.2444997945327, 16.1754244224394, 16.4315630892153, 
16.1257174992647, 15.3103781494378, 14.9008093717245, 14.6479919804145, 
15.0774193136139, 3.14483111523805, 4.40015938431536, 6.30335425421752, 
7.89295676809648, 10.3873658409837, 11.1675458065063, 11.2239362409328, 
13.4786551231579, 13.1174923553482, 11.8463935602633, 12.7792370089478, 
8.92853946342597, 3.67638327509499, 1.44336933818614, 3.7460763269343, 
2.80373132419384, 10.6758687377781, 17.1292041998935, 17.9235639821042, 
16.7225906653072, 15.5378208669432, 14.8132669217501, 14.965291900554, 
14.4626701869086, 2.09918561880569, 5.78385439843915, 10.7115515863618, 
18.3161496586981, 18.8453323554176, 20.1504317889165, 20.5243338520425, 
23.0946561914882, 23.1229411411489, 21.4766474646763, 18.8629064835698, 
13.6232806695288, 8.87195358826412, 3.02138370287754, 1.01567714013273, 
0.855184425602785, 15.2952996338701, 21.4434489591344, 20.5409630399889, 
19.0485834710192, 18.4566381805445, 17.4914927823451, 16.9949066569197, 
17.2394708863446, 5.20422190837278, 6.17270380733072, 5.42723981712398, 
4.91201796522349, 6.86605674843221, 8.9937132547134, 9.77633825599795, 
11.8107916732391, 16.043897776083, 17.3012176145297, 15.9028994432813, 
8.67297044050019, 5.16161707362852, 2.60348086326685, 1.20894961279237, 
2.51983065586451, 12.7011652003945, 17.0221447562056, 16.5190241116339, 
15.8870123508507, 15.4281483757257, 14.6144445210435, 13.2463831343735, 
12.6455956591155, 1.43836514820975, 7.3964941173677, 15.6589628080384, 
20.3821287445914, 24.6713477136591, 27.6904620792603, 28.5654304282508, 
25.3036289389789, 23.3449995578065, 23.0438525984448, 22.3306002371854, 
19.8544630570727, 14.9729598687669, 8.7587370696577, 5.31643600296484, 
4.04921813305131, 15.9829567620533, 21.3550401702375, 21.3824061491415, 
22.1984029205243, 22.4680353770285, 22.8554518167813, 23.0539756815934, 
23.5210387764831, 8.33814328095043, 9.55199051953161, 12.399383026239, 
14.4229377889505, 16.5763561723988, 16.059960214447, 17.6171246534286, 
21.3886648589456, 25.8181985070404, 24.5832331438196, 20.3333383167425, 
16.9244071165871, 15.8900358018522, 13.842652322184, 9.41731422210656, 
6.87007359112986, 17.2315876647855, 21.8982393005026, 22.1040991214791, 
21.8963615899734, 21.3128631580006, 21.1956782666886, 21.3259415120726, 
22.1871281639499, 7.38341716187449, 10.0050435819574, 15.5578500812052, 
17.6701921784541, 24.6080333615887, 29.3240271398202, 32.9230192385226, 
36.1174965373569, 38.200145076904, 36.6356882807734, 34.1940872924714, 
29.0602322683995, 23.4755814686931, 17.1688634312255, 10.5046421189841, 
6.42452005979931, 23.6418303116755, NA, NA, NA, NA, NA, NA, 54.4343511257372, 
2.82289846623857, 6.36470006309094, 9.35271310858323, 14.9183391138978, 
19.7911161231157, 23.6336700416108, 25.7408728308815, 29.6525364862642, 
35.0274565367524, 38.5647202768138, 38.9995901261482, 35.1574124906668, 
28.7562081067795, 19.7627823633146, 12.8232222891012, 7.16499577741958, 
24.326308948197, 29.9224828411809, 29.3190199333518, 28.4295405828911, 
27.1628370657564, 26.1315997390072, 24.854988187972, 23.9801017080776, 
6.23517119408303, 8.3530231126271, 17.6653627336558, 25.9994213088618, 
36.2609324085035, 36.9365270157112, 29.3130105060826, 24.9156375902589, 
20.2354035394718, 17.9195087953262, 17.6894987728821, 11.6173525273901, 
5.79071235499063, 2.20374299938686, 0.09282185071742, 0.252230390627006, 
14.8073827079744, 19.256744346541, 17.4414878022219, 15.5101496678282, 
13.3277769079227, 11.2014545744602, 9.04785296211735, 7.70497585988905, 
2.70107195580814, 0.567589042929954, 4.41529902811057, 2.27774764209442, 
5.78721696829642, 10.6062617324736, 14.0445971430991, 18.0539144777537, 
21.5232028045893, 24.0158053088079, 25.8407179578528, 21.8342515181315, 
13.6919908435447, 4.0255627802668, 0.665949933004891, 0.208953214880797, 
16.1561669086234, 21.4497981663637, 383.067368925988, NA, NA, 
NA, 133.631551984593, 15.170591109258, 0.395827787471503, 0.269068190956889, 
3.50358059070192, 3.01677307944915, 2.00600287832979, 1.34682034030451, 
0.741245171651234, 1.19986776789557, 0.443705360449056, 0.212748459428304, 
1.06686877230414, 0.644617596270293, 0.80585339118931, 2.73257367450756, 
5.61775101084879, 6.46683498731927, 5.53032831060134, 8.85927149838813, 
6.79263511135433, 4.95326747917033, 3.49094953886742, 1.95123597400578, 
0.231727674223854, 1.13819878503669, 4.49692523622297, 3.3496788250999, 
0.60491692162872, 5.40141722711159, 6.73203968177679, 9.10727409649512, 
9.12357601422001, 9.85039555082153, 10.47895829976, 8.97590521908966, 
6.24612668446798, 2.87472139336583, 2.59997288006899, 3.7032320868021, 
5.40642614609066, 7.23884019003137, 5.30743533293025, 9.10524755249705, 
7.5565554674022, 6.89258777489288, 6.65674106952376, 7.09801583390161, 
7.02491962705643, 5.87441415384407, 2.22566924966015, 0.539426182393964, 
5.16883242134497, 8.80263246750633, 15.133649266248, 20.9065093335231, 
24.804849931059, 27.22404205069, 30.0112830892839, 33.7105842238466, 
33.148299672093, 28.9566605146087, 23.7557141914921, 14.8418199201637, 
8.03722695037173, 6.25444447810442, 21.8121838742914, 26.6222413990827, 
25.9680743217436, 26.8106000806074, 26.3415568350102, 25.0031712782548, 
22.9096265004055, 21.1041419714525, 3.72000091641834, 8.05063054052596, 
13.9794816114201, 24.904641775123, 34.023028503998, 40.7916930393454, 
44.2950843181925, 48.0110797482155, 50.9533674170092, 51.9781701747674, 
48.6404957370374, 40.4096134991892, 31.0983021543994, 21.692028504472, 
15.9728661585324, 12.8755052184241, 30.7772281295453, 35.8791796190767, 
33.8729890245313, 31.7538840828441, 31.0234864087387, 29.8625890835194
), hour = c(7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 
23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 
23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 
23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 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6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L), .Label = c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", 
"Sun"), class = c("ordered", "factor"))), .Names = c("ABStemp", 
"hour", "weekday"), row.names = 2600:3100, class = "data.frame")

上面的数据框包含影响我的 zlim() 值的异常值。如果您使用我的代码绘制此数据框,您会得到以下结果,这很好。

ABSerror http://danielcoakley.com/project/Testing/TemperatureIssue.png

但是,在绘制整个数据集时,这一高值会扭曲可视化。有没有什么方法可以在不统计去除异常值的情况下处理这个问题?

4

2 回答 2

1

一种可能性是使用 定义中断boxplot.stats(),离群值在 范围之外boxplot.stats(ABStemp)$stats。(仅供参考的异常值包含在 中boxplot.stats(ABStemp)$out)。
一个美学缺点是你失去了配色方案的连续性。

breaks <- c(boxplot.stats(df$ABStemp)$stats,max(df$ABStemp,na.rm=TRUE)) 
#if you had some low outliers, you would use instead: c(min(ABStemp,na.rm=TRUE),boxplot.stats(ABStemp)$stats,max(ABStemp,na.rm=TRUE))
plot<-contourplot(ABStemp ~ hour * weekday,
                   aspect=0.3,
                   data = df,
                   #cuts = 200,
                   #labels=TRUE,
                   contour=FALSE,
                   region = TRUE,
                   #pretty=FALSE,
                   xlab = "Hour of Day",
                   ylab = "Day of Week",
                   col.regions=colorRampPalette(c("blue","yellow","red")),
                   at=breaks) #<-This is where you define your breaks for z

在此处输入图像描述

如果boxplot.stats()异常值的定义不适合您的需要,您可以使用quantile()而不是boxplot.stats()使用所需的突破点来修改它。

于 2013-10-31T08:43:28.540 回答
0

根据您的回复@piannapus,我能够想出一个解决方法。我使用了相同的 'at' 函数,但我没有使用boxplot.stats(),而是重新计算了我绘制的数据范围的统计数据,找到了最大值和最小值,并应用了该seq函数来查找切点。这让我在传说中保持了平稳过渡。

df<-df[!(is.na(df$ABStemp) | df$ABStemp==""), ] # Remove any NA/Blank values, where present
df1<-with(df, tapply(ABStemp, list(hour, month), mean))
max<-max(df1,na.rm=T)
min<-min(df1,na.rm=T)
seq <- seq(min,max,max/200)

plot2<-contourplot(ABStemp ~ hour * weekday,
                   aspect=0.3,
                   data = df,
                   #cuts = 200,
                   #labels=TRUE,
                   contour=FALSE,
                   region = TRUE,
                   pretty=FALSE,
                   at=seq,
                   xlab = "Hour of Day",
                   ylab = "Day of Week",
                   col.regions=colorRampPalette(c("blue","yellow","red")),
)

这可能不是解决问题的最有效方法,但它确实有效。此外,在进行此更改时,我注意到轮廓图甚至没有给我所需的视觉摘要。我假设它会自动计算一年中每一天的每一小时的平均值。然而,相反,它似乎一直在使用来自原始数据帧的直接值,而不计算平均值(即仅使用单个实例的值,而不是汇总数据范围)。无论如何,下面是使用该tapply函数进行计算的结果。

ABSerror http://danielcoakley.com/project/Testing/TemperatureIssueResolved.png

于 2013-10-31T19:16:48.493 回答