在stat_density2d中加入额外的维度

时间:2017-05-22 10:39:02

标签: r ggplot2

我正在进行一些单粒子跟踪实验,并且我制作了一个看起来像这样的情节

enter image description here

这样的数据的摘录

Tracks_subset <- structure(list(x = c(50.9870663039413, 51.1454673261645, 51.2438755862949, 
51.2095459932038, 51.6660042216598, 50.9114140942979, 51.0024489561214, 
50.8857311418507, 51.2794722111027, 51.2431331170315, 51.1686446878892, 
51.6828347775235, 52.1450571292308, 52.0846161251043, 52.6398039669697, 
52.8833174825244, 52.5705151101221, 47.7126603341761, 47.7043102797697, 
47.7383984362003, 47.6860850023564, 47.6951829901912, 47.6699652212862, 
47.7413324987855, 47.6843782373267, 47.644111020292, 47.6618622920263, 
47.6541735611274, 47.6649621568611, 47.6523884564593, 47.6610279361442, 
47.6512197115578, 47.6657086161767, 47.6192509459374, 47.6207586571131, 
47.5774206679784, 47.6200920953012, 47.7306727739304, 47.6379509867329, 
47.714775352656, 47.6550462636824, 47.6609930612728, 47.7000183950083, 
47.6649651728503, 47.6539558351917, 47.6389944434634, 47.6717405874446, 
47.6953802257671, 47.670156194978, 47.6659335190123, 47.6765976765644, 
47.6840293686863, 47.6835120352331, 47.7206680944438, 47.6295515092937, 
47.6296091556035, 47.6942899171821, 47.7250190805859, 47.5914020018512, 
47.70039477981, 47.6173948003708, 47.6403690517817, 47.706224437305, 
47.6278056300272, 47.7050590713087, 47.6202775363008, 47.616604053917, 
47.6756516087319, 47.7266296774674, 47.6446057760854, 47.5705082443691, 
47.6595393038973, 47.7454552888996, 47.6741550431176, 47.5860886147769, 
47.5711827843158, 47.6032212783683, 47.6664108892973, 47.6658197925874, 
48.3968244300988, 48.9904220754653, 48.8091985242839, 48.6060438195256, 
47.9442329308765, 47.5918680853048, 61.7398341199382, 61.5105531711929, 
61.3508575465536, 60.7780284493327, 60.9300399030291, 61.5425582885666, 
61.6082571595723, 61.5298863337404, 61.7856612691648, 61.9071110539638, 
62.2590343816915, 62.4185223478263, 62.9672715597194, 63.3275273995809, 
63.8113387354958, 64.3231547762155, 63.7003651772996, 64.5725239608709
), y = c(25.4839360156051, 25.6047955465986, 25.4262289850266, 
25.6172499551253, 25.2123769606517, 25.0385424557608, 24.966470254684, 
25.0765695667183, 25.3864947576636, 25.5754234194841, 25.362590160186, 
25.7772644867977, 25.5496302761478, 25.2288998273316, 25.0558763011072, 
24.9078484752662, 24.976087151153, 25.3889343715977, 25.4338531136808, 
25.4429033909954, 25.4318520315956, 25.3990616319309, 25.4511690945816, 
25.3200107971065, 25.481502531704, 25.4813627020502, 25.4853491195569, 
25.4847769218943, 25.4575428518825, 25.4557467653372, 25.4624512146039, 
25.4595647154719, 25.4563293549661, 25.4804991266559, 25.3812415821813, 
25.4473050286014, 25.4196869940826, 25.4442315703239, 25.479341197772, 
25.4300738230213, 25.4693914326269, 25.4567345365435, 25.4674089428837, 
25.4112535933299, 25.4637733103663, 25.3956145368333, 25.4990099732346, 
25.4839175917237, 25.4745320245195, 25.4005315613532, 25.469591766703, 
25.4539955232768, 25.4657553925353, 25.4245323404107, 25.4361711386943, 
25.3891764540008, 25.466965662987, 25.1009694093232, 25.0358862503509, 
25.1789725026332, 25.42714785297, 25.4795979566479, 25.468929968642, 
25.4239887143419, 25.5281893742505, 25.4093001632827, 25.4384275123265, 
25.4344555350342, 25.4429352491382, 25.5199514977758, 25.4910332653875, 
25.4595502380103, 25.4968060944952, 25.4827240176748, 25.3698050293595, 
25.4439838950347, 25.3741755234997, 25.5563892658156, 25.6960651973953, 
25.7204681252841, 26.4352696253167, 27.1441226535699, 26.6615707288907, 
26.3571435264103, 26.2462341038955, 25.4044026980314, 24.9429648374475, 
25.316181029454, 25.5746195865509, 25.4159071371913, 25.0535276243763, 
25.1404356519081, 24.9217292382466, 25.0681095303721, 25.6848864023155, 
25.7992168557371, 25.2353282682059, 24.9773329424632, 24.4822347858453, 
24.268105758617, 24.6415345738956, 24.0369860755695, 23.6865149091661
), steplength = c(0, 0.199243343862037, 0.203887720506984, 0.194081250972988, 
0.610144455009775, 0.774354373270278, 0.116110930731278, 0.160452194371228, 
0.501084477506786, 0.192391707241276, 0.225491734526058, 0.660565095527355, 
0.515234739003464, 0.326375758563115, 0.581524445214716, 0.284975559445094, 
0.320159087122816, 0, 0.0456882566850305, 0.035269107279719, 
0.053468008239723, 0.0340291594490852, 0.0578888895422253, 0.14931773938219, 
0.171240673444157, 0.0402674598162118, 0.0181933826629311, 0.00770999306101455, 
0.0292931453981066, 0.0127013333423588, 0.0109357326776552, 0.0102241452834658, 
0.0148457372554814, 0.0523688169390838, 0.0992689947975958, 0.079009874415496, 
0.0508291898464581, 0.113271897260304, 0.0991464359452704, 0.091264765459093, 
0.071508310668843, 0.0139843276580312, 0.0404588633537657, 0.066197822223741, 
0.0536612168447871, 0.0697815280139674, 0.108457024734435, 0.0280466126241494, 
0.0269135765181205, 0.0741208441744882, 0.06987872508325, 0.0172763670024788, 
0.0117712428774199, 0.055496961741496, 0.0918569197986897, 0.0469947200495333, 
0.101167000316718, 0.367284003435199, 0.148624834100549, 0.179869678487995, 
0.261686837845756, 0.0572610653386249, 0.0667138499153149, 0.0903837688578274, 
0.129714577882537, 0.146022440613998, 0.029358081257867, 0.0591809963859102, 
0.0516785162644559, 0.112514101125709, 0.0795406083136827, 0.0944336305219004, 
0.0936459039218517, 0.0726775751944748, 0.143200536740801, 0.0756616672734531, 
0.0768093343124944, 0.192859469091138, 0.139677182309732, 0.731411842228994, 
0.929139036438272, 0.731651960406345, 0.523572529910693, 0.728470709050467, 
0.369407477451884, 0, 0.515261732169024, 0.405947063673079, 0.628429520644252, 
0.219766065707624, 0.711686506777716, 0.108946532303133, 0.232324087684107, 
0.294699860051154, 0.628620521453137, 0.37002902748092, 0.586009172701016, 
0.606372233581375, 0.61229605162923, 0.529079057477617, 0.633565102904463, 
0.867954936241549, 0.939942010041431)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -103L), .internal.selfref = <pointer: 0x0>, .Names = c("x", 
"y", "steplength"))

情节的代码是

ggplot(data = Tracks_subset, aes(x = x,
                                 y = y)) +
    # stat_bin2d(bins = 10, aes(fill = ..density..)) + # blocky heat map
    stat_density2d(aes(fill = ..level..,
                       alpha = ..level..),
                   bins = 20,
                   geom = 'polygon') +
    xlim(-20,max(Tracks_subset$x)+20) + # Adds padding for polygons to have proper shape
    ylim(-20,max(Tracks_subset$y)+20) +
    coord_cartesian(xlim = c(0,max(Tracks_subset$x)), # Clips extra padding visually only
                    ylim = c(0,max(Tracks_subset$y)),
                    expand = FALSE) +
    scale_fill_continuous(low = "white",
                          high = "red") +
    theme_classic(base_size = 12,
                  base_family = "Droid Sans") +
    theme(axis.text    = element_text(color = "black"),
          strip.text.y = element_blank(),
          strip.background = element_rect(color = "white"),
          legend.text  = element_text(size = 12, family = "Droid Sans"),
          legend.title = element_blank(),
          legend.position = "none") +
    facet_wrap(condition + lipase ~ video,
               scales = "free")

现在我的问题是,我怎么能以某种方式将steplength参数作为一个额外的维度(例如,而不是..level..我可能以某种方式使用步长)或stat_density2d中的权重?我知道stat_bin2d有可能,但这给我一个非常块状的外观,我不喜欢。

0 个答案:

没有答案