Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

r - First time working with interactive plots in ggplotly, would like some feedback on my plot

This is my first time working with ggplot2/ggploty() and would like some feedback as to what I could do to make my plot better.

This is the code i've used:

 # interactive plot with ribbon
test <- ggplot(data = wide.data, aes(x = x, y = pred)) +
  geom_ribbon(aes(ymin = lwr, ymax = upr, fill = model), alpha = .5) + 
  geom_point(aes( group = model, fill = model)) + 
  labs(x = "Number of Games Played", y = "Total Number of Goals") + scale_y_continuous(breaks=seq(0,70,5)) +
  scale_x_continuous(breaks=seq(0,50,5))

ggplotly(test)

Local Hosts won't work so here is the short version, even though its not so short:

> dput(wide.data)
structure(list(x = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 
8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 
10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 
12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 
14L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 
17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 
19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 
21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 
23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 
25L, 25L, 26L, 26L, 26L, 26L, 26L, 26L, 27L, 27L, 27L, 27L, 27L, 
27L, 28L, 28L, 28L, 28L, 28L, 28L, 29L, 29L, 29L, 29L, 29L, 29L, 
30L, 30L, 30L, 30L, 30L, 30L, 31L, 31L, 31L, 31L, 31L, 31L, 32L, 
32L, 32L, 32L, 32L, 32L, 33L, 33L, 33L, 33L, 33L, 33L, 34L, 34L, 
34L, 34L, 34L, 34L, 35L, 35L, 35L, 35L, 35L, 35L, 36L, 36L, 36L, 
36L, 36L, 36L, 37L, 37L, 37L, 37L, 37L, 37L, 38L, 38L, 38L, 38L, 
38L, 38L), model = c("BLI", "BPL", "LG1", "LGA", "MLS", "SRA", 
"BLI", "BPL", "LG1", "LGA", "MLS", "SRA", "BLI", "BPL", "LG1", 
"LGA", "MLS", "SRA", "BLI", "BPL", "LG1", "LGA", "MLS", "SRA", 
"BLI", "BPL", "LG1", "LGA", "MLS", "SRA", "BLI", "BPL", "LG1", 
"LGA", "MLS", "SRA", "BLI", "BPL", "LG1", "LGA", "MLS", "SRA", 
"BLI", "BPL", "LG1", "LGA", "MLS", "SRA", "BLI", "BPL", "LG1", 
"LGA", "MLS", "SRA", "BLI", "BPL", "LG1", "LGA", "MLS", "SRA", 
"BLI", "BPL", "LG1", "LGA", "MLS", "SRA", "BLI", "BPL", "LG1", 
"LGA", "MLS", "SRA", "BLI", "BPL", "LG1", "LGA", "MLS", "SRA", 
"BLI", "BPL", "LG1", "LGA", "MLS", "SRA", "BLI", "BPL", "LG1", 
"LGA", "MLS", "SRA", "BLI", "BPL", "LG1", "LGA", "MLS", "SRA", 
"BLI", "BPL", "LG1", "LGA", "MLS", "SRA", "BLI", "BPL", "LG1", 
"LGA", "MLS", "SRA", "BLI", "BPL", "LG1", "LGA", "MLS", "SRA", 
"BLI", "BPL", "LG1", "LGA", "MLS", "SRA", "BLI", "BPL", "LG1", 
"LGA", "MLS", "SRA", "BLI", "BPL", "LG1", "LGA", "MLS", "SRA", 
"BLI", "BPL", "LG1", "LGA", "MLS", "SRA", "BLI", "BPL", "LG1", 
"LGA", "MLS", "SRA", "BLI", "BPL", "LG1", "LGA", "MLS", "SRA", 
"BLI", "BPL", "LG1", "LGA", "MLS", "SRA", "BLI", "BPL", "LG1", 
"LGA", "MLS", "SRA", "BLI", "BPL", "LG1", "LGA", "MLS", "SRA", 
"BLI", "BPL", "LG1", "LGA", "MLS", "SRA", "BLI", "BPL", "LG1", 
"LGA", "MLS", "SRA", "BLI", "BPL", "LG1", "LGA", "MLS", "SRA", 
"BLI", "BPL", "LG1", "LGA", "MLS", "SRA", "BLI", "BPL", "LG1", 
"LGA", "MLS", "SRA", "BLI", "BPL", "LG1", "LGA", "MLS", "SRA", 
"BLI", "BPL", "LG1", "LGA", "MLS", "SRA", "BLI", "BPL", "LG1", 
"LGA", "MLS", "SRA", "BLI", "BPL", "LG1", "LGA", "MLS", "SRA", 
"BLI", "BPL", "LG1", "LGA", "MLS", "SRA"), lwr = c(0.991908465191337, 
1.024356375271, 0.991908465191337, 0.713669169484484, 1.12586982342001, 
1.12084858138188, 2.06420487168374, 1.97777465268675, 2.06420487168374, 
1.62334863906913, 2.23910774286228, 2.21022346166219, 2.97388434126839, 
2.96968311787809, 3.11511279055919, 2.71272351934943, 3.36497756628229, 
3.11990293124683, 3.96579280645972, 3.96159158306943, 3.9773593493607, 
3.73707989462043, 4.47821548572456, 4.23314085068911, 4.95770127165106, 
4.91500986048518, 5.066734229641, 4.76143626989143, 5.60408530914457, 
5.18655912810486, 5.98205764692206, 5.7772564192867, 6.11764214851645, 
5.67111573947608, 6.72995513256457, 6.09623859768951, 6.9739661121134, 
6.49092558877118, 7.23849072989833, 6.4341005009485, 7.83280914918269, 
7.08814706288085, 7.92738438952915, 7.48283405396252, 8.10073728869984, 
7.38751877836426, 8.9356631658008, 8.08005552807218, 8.69036915100158, 
8.196503223447, 9.15164520757529, 8.43842669723971, 10.0615329892208, 
9.15235193456459, 9.68227761619292, 9.24741114232245, 10.0613246771599, 
9.43033516243104, 11.1747709086631, 10.0145984933661, 10.4951374013193, 
10.0103959037949, 11.0532331423513, 10.0959286392011, 12.2880088281053, 
11.0655064122415, 11.448555678735, 11.0023043689862, 12.0451416075426, 
10.8589134006735, 13.4163944903645, 12.0574148774329, 12.3582351483197, 
11.9119838385709, 13.0960495264181, 11.4335539927294, 14.5296324098068, 
13.1083227963083, 13.3825915235907, 12.8216633081555, 14.0057289960027, 
12.3869722701452, 15.6504809911887, 13.9705693551098, 14.0481850003607, 
13.4408352207955, 14.997637461194, 13.3788807353365, 16.7637189106309, 
15.0214772739853, 15.0990929192361, 14.2038199822679, 16.0699338676864, 
14.3707892005279, 17.8895887340509, 16.0937736804777, 16.0525111966519, 
15.1572382596836, 17.1593087479667, 15.3626976657192, 19.0104373154328, 
17.183148560758, 17.0059294740676, 16.1815946349546, 18.1127270253825, 
16.3546061309105, 20.1236752348751, 18.2340564796334, 18.0568373929431, 
17.0912741045393, 19.2259649448248, 17.1674659160369, 21.244523816257, 
19.2259649448248, 19.0487458581344, 17.8542588660117, 20.339202864267, 
17.9304506775093, 22.3729094785161, 20.1793832222405, 19.8616056432607, 
18.5679280354962, 21.3901107831425, 18.9548070527803, 23.4861473979584, 
21.230291141116, 20.8150239206765, 19.521346312912, 22.4410187020179, 
19.8644865223649, 24.5993853174007, 22.2221996063073, 21.7247033902612, 
20.5132547781033, 23.4919266208934, 20.9538614026453, 25.7252551408207, 
23.2141080714986, 22.7166118554525, 21.2762395395757, 24.4453448983091, 
21.9782177779162, 26.8461037222026, 24.0763546303002, 23.3822053322225, 
22.2681480047671, 25.3550243678938, 22.9701262431076, 27.9489577388207, 
24.9860340998848, 24.4065617074935, 23.2600564699584, 26.4059322867692, 
23.9620347082989, 29.062195658263, 26.0369420187603, 25.3162411770782, 
24.2844128452294, 27.3978407519606, 24.7748944934253, 30.1830442396449, 
27.1263168990406, 26.0792259385506, 25.2378311226451, 28.5186893333425, 
25.728312770841, 31.285898256263, 28.0359963686252, 27.0326442159663, 
25.8570030352851, 29.380935892144, 26.7202212360324, 32.3991361757052, 
28.8982429274267, 28.0245526811577, 26.5706722047696, 30.4532322986364, 
27.629900705617, 33.5250059991252, 30.011480846869, 29.0489090564287, 
27.4803516743542, 31.5426071789167, 28.295494182387, 34.6382439185675, 
31.0033893120603, 30.1212054629211, 28.4722601395456, 32.534515644108, 
29.3464021012625, 35.7514818380098, 31.9952977772517, 31.1131139281124, 
29.523168058421, 33.6238905243883, 30.0600712707469, 36.8773516614298, 
33.0462056961271, 32.1050223933037, 30.4328475280057, 34.6157989895797, 
30.6347118628029, 37.9982002428117, 34.167054277509, NA, 31.3425269975903, 
35.5254784591643, 31.5881301402187, 39.1240700662317, 35.2179621963844, 
NA, 32.2959452750061, 36.3877250179658, 32.58003860541, 40.2499398896517, 
36.2902586028768, NA, 33.1581918338076, 37.3796334831571, 33.3430233668824, 
41.3758097130717, 37.3625550093693, NA, 34.1825482090786, 38.4519298896496, 
34.3349318320738, 42.4890476325139, 38.4519298896496), pred = c(1.14244921317105, 
1.18853138677409, 1.14244921317105, 0.901144403312629, 1.56756991682395, 
1.50679152271014, 2.42879627669905, 2.28668514123042, 2.42879627669905, 
1.95672013877945, 3.01593956754501, 2.84502506662769, 3.48437201216587, 
3.42913435440147, 3.66526862143132, 3.294953682697, 4.58350948436895, 
3.90060080209451, 4.62682122533692, 4.57158356757252, 4.67991718843272, 
4.48348506947109, 6.03187913509002, 5.34897045281557, 5.76927043850797, 
5.66973732202884, 6.01815073235027, 5.67201645624518, 7.59944905191396, 
6.4471242072719, 6.95780182528206, 6.68438588903025, 7.25462307708254, 
6.72759219171201, 9.16701896873791, 7.50269994273873, 8.10025103845311, 
7.58553029234288, 8.76141459979268, 7.66508542555129, 10.559231901657, 
8.64514915590978, 9.19840479290944, 8.72797950551393, 9.77606316679409, 
8.76323918000761, 11.951444834576, 9.78759836908083, 10.1358980267487, 
9.62912390882656, 11.0125355115264, 9.99971152473988, 13.5190147514, 
11.0739454326088, 11.2783472399198, 10.8655962535588, 12.0681112469932, 
11.1421607379109, 14.967384402121, 12.0885939996102, 12.2536554815996, 
11.8030894873981, 13.2105604601642, 12.0083656408477, 16.4157540528421, 
13.3250663443425, 13.3518092360559, 12.9455387005692, 14.3530096733353, 
12.945858874687, 18.0465539392867, 14.4675155575135, 14.4073849715227, 
14.001114436036, 15.5894820180675, 13.7461963137094, 19.4949235900078, 
15.7039879022458, 15.5959163582968, 15.0566901715028, 16.6450577535344, 
14.8443500681658, 21.0017151127179, 16.7186364692472, 16.4621212612336, 
15.8893102611506, 17.7875069667054, 15.9867992813368, 22.450084763439, 
17.9551088139795, 17.6985936059659, 16.8268034949899, 19.0738540302334, 
17.1292484945079, 24.0176546802629, 19.2414558775075, 18.7967473604222, 
17.9249572494462, 20.412087574151, 18.2716977076789, 25.5244462029731, 
20.579689421425, 19.8949011148785, 19.1134886362203, 21.5102413286073, 
19.41414692085, 26.9728158536941, 21.8161617661573, 21.1313734596108, 
20.1690643716871, 22.9586109793284, 20.3894551625298, 28.4796073764043, 
22.9586109793284, 22.2738226727819, 21.1065576055264, 24.4069806300494, 
21.3269483963691, 30.1104072628489, 24.0567647337847, 23.2491309144617, 
22.007702008839, 25.6434529747817, 22.5154797831431, 31.55877691357, 
25.2932370785169, 24.347284668918, 23.1058557632954, 26.879925319514, 
23.57105551861, 33.007146564291, 26.435686291688, 25.4028604043848, 
24.2483049764664, 28.1163976642462, 24.9092890625275, 34.574716481115, 
27.5781355048591, 26.5453096175559, 25.1857982103057, 29.2145514187025, 
26.0978204493016, 36.0815080038251, 28.5927840718605, 27.4115145204927, 
26.3282474234767, 30.2701271541694, 27.2402696624727, 37.4737209367442, 
29.6483598073273, 28.6000459072668, 27.4706966366478, 31.5065994989016, 
28.3827188756437, 38.9220905874652, 30.8848321520595, 29.6556216427336, 
28.6592280234219, 32.6490487120727, 29.3580271173235, 40.4288821101754, 
32.2230656959771, 30.5931148765729, 29.7573817778782, 34.1558402347828, 
30.4561808717798, 41.8210950430944, 33.2786414314439, 31.6912686310292, 
30.590001867526, 35.1704888017842, 31.5986300849509, 43.2694646938155, 
34.2932899984453, 32.8337178442003, 31.4911462708386, 36.4568358653122, 
32.6542058204177, 44.8370346106394, 35.7416596491664, 34.0222492309743, 
32.5467220063055, 37.7950694092298, 33.5204107233545, 46.2854042613605, 
36.8841088623374, 35.3085962945023, 33.6891712194765, 38.9375186224008, 
34.7568830680868, 47.7337739120815, 38.0265580755085, 36.4510455076734, 
34.9256435642088, 40.2757521663184, 35.6580274713994, 49.3013438289055, 
39.2630304202408, 37.5934947208444, 35.9812192996756, 41.4182013794894, 
36.4583649104218, 50.8081353516156, 40.7698219429509, NA, 37.0367950351424, 
42.4737771149563, 37.5565186648782, 52.3757052684396, 42.0062942876832, 
NA, 38.1349487895988, 43.4884256819577, 38.6989678780492, 53.9432751852635, 
43.2926413512112, NA, 39.1495973566002, 44.6308748951287, 39.6364611118885, 
55.5108451020875, 44.5789884147392, NA, 40.3381287433743, 45.9172219586567, 
40.7789103250595, 56.9592147528085, 45.9172219586567), upr = c(1.29298996115076, 
1.35270639827718, 1.29298996115076, 1.08861963714077, 2.00927001022789, 
1.8927344640384, 2.79338768171436, 2.59559562977408, 2.79338768171436, 
2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. You can set plot to open with some ribbons hidden if you build the plot directly using plot_ly() (instead of ggplot) by using visible="legendonly".
  2. You can set the names of each trace as they appear in the legend using name=.

A minimal example:

my.data = data.frame(x=1:40, y=1:40)
plot_ly(my.data, x=x, y=y, mode="markers", name="foo") %>%
  add_trace(type="area", x=c(x,rev(x)), y= c(y+10, rev(y)-10), 
            fill = "toself", visible="legendonly", name="bar")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...