How to overlay a 2d density plot on top of a map - r

I need some help in overlaying a 2d density plot on top of a ggmap plot. I don't really know how to procede. Any help is welcome.
This is a subset of my data, including start and end coordinates for each individual ride of a fictional bike sharing company:
df <- structure(list(start_lat = c(41.94018, 41.890762, 41.845695,
41.857813, 41.9287386666667, 42.0044803333333, 41.879255, 41.886835,
41.874734, 41.95469, 41.95, 41.8809518333333, 41.96590013976,
41.909668, 41.931248, 41.96167, 41.912133, 41.87947235235, 41.936266,
41.922695, 41.9101756666667, 41.86, 41.91468, 41.892278, 42.03,
41.911386, 41.9716, 41.93, 41.940195, 41.9560855, 41.915784,
41.93314, 41.943739, 41.8671848333333, 41.87464, 41.882242, 41.926277,
41.96167, 41.76, 41.883668, 41.967096, 41.8, 41.9024035, 41.939743,
41.9093960065, 41.915983, 41.87772613, 41.8984238333333, 41.8836331666667,
41.925905, 41.967096, 41.92, 41.884576228, 41.838499, 41.9028846666667,
41.89993001, 41.8, 41.866095, 41.97, 41.9093960065, 41.88, 41.8922376666667,
41.81, 41.9438251666667, 41.883668, 41.9207793333333, 41.954383,
41.9434726666667, 41.8945555, 41.911386, 41.88917683258, 41.86722595682,
41.8531223333333, 41.92, 41.919936, 41.90096, 41.894722, 41.872187,
41.881892, 41.920082, 41.897448, 41.88917683258, 41.9, 41.925858,
41.89, 41.8908470406238, 41.85, 41.890173, 41.92556258, 41.885637,
41.9030376666667, 41.93314, 41.838198, 41.892278, 41.93, 41.894722,
41.90345, 41.6922943333333, 41.9080621666667, 42.025784), start_lng = c(-87.65304,
-87.631697, -87.6225141666667, -87.62455, -87.6538015, -87.6615086666667,
-87.639904, -87.62232, -87.6498425, -87.67393, -87.71, -87.6167566666667,
-87.6936384935, -87.648128, -87.644336, -87.65464, -87.634656,
-87.6256886059, -87.652662, -87.697153, -87.6823075, -87.62,
-87.64332, -87.612043, -87.71, -87.638677, -87.650154, -87.74,
-87.6529666666667, -87.668857, -87.634581, -87.64776, -87.66402,
-87.6260033333333, -87.65703, -87.641066, -87.630834, -87.65464,
-87.58, -87.64867, -87.667429, -87.58, -87.6277486666667, -87.658865,
-87.6776919292, -87.677335, -87.65478743, -87.6223878333333,
-87.629143, -87.64926, -87.667429, -87.7, -87.63188991, -87.6080766666667,
-87.6874035, -87.63443007, -87.59, -87.607267, -87.71, -87.6776919292,
-87.63, -87.6119485, -87.61, -87.671138, -87.64867, -87.6637163333333,
-87.648043, -87.6796343333333, -87.6534645, -87.638677, -87.6385057718,
-87.6153553902, -87.6318963333333, -87.74, -87.64883, -87.623777,
-87.634362, -87.661501, -87.648789, -87.677855, -87.628722, -87.6385057718,
-87.62, -87.638973, -87.66, -87.6186168193817, -87.72, -87.626185,
-87.65840426, -87.641823, -87.631299, -87.64776, -87.645143,
-87.612043, -87.71, -87.634362, -87.667747, -87.6426485, -87.6315093333333,
-87.684107), end_lat = c(41.918306, 41.886875, 41.8456825, 41.8530845574128,
41.890831, 41.99, 41.885637, 41.881319815, 41.88, 41.961068,
41.93, 41.880958, 41.966399801841, 41.89766, 41.9267559875, 41.9578665241517,
41.911386, 41.867888, 41.95078, 41.932588, 41.9245285, 41.8776751666667,
41.9105780349, 41.9239313113662, 42.0192226666667, 41.904613,
41.9947796884, 41.96, 41.9296915, 41.94, 41.94, 41.907066, 41.923931,
41.8707831666667, 41.87772613, 41.872187, 41.892278, 41.961004,
41.7689161666667, 41.8793563587, 41.95078, 41.79, 41.882242,
41.932225, 41.912133, 41.9093960065, 41.8810317, 41.9, 41.89,
41.912133, 41.926277, 41.93190196886, 41.874053, 41.8368228333333,
41.882754, 41.894666, 41.76, 41.882134, 41.96, 41.89637337, 41.87,
41.8787191666667, 41.8, 41.91, 41.917805, 41.88, 41.926277, 41.93,
41.8990156666667, 41.890762, 41.8854833079, 41.874754, 41.85,
41.94, 41.920771, 41.894345, 41.94334, 41.871737, 41.88338, 41.92154,
41.882134, 41.902997, 41.876243, 41.892278, 41.89, 41.886024,
41.86, 41.8918473721099, 41.9093960065, 41.8854833079, 41.89,
41.92883, 41.834734, 41.891466, 41.9296816666667, 41.902973,
41.918491153687, 41.75, 41.9218326666667, 41.9840446107), end_lng = c(-87.636282,
-87.62603, -87.6224476666667, -87.6319313049316, -87.6313945,
-87.66, -87.641823, -87.6295209193, -87.65, -87.695439, -87.71,
-87.616743, -87.6887042820454, -87.62351, -87.6344287848, -87.6495051383972,
-87.638677, -87.623041, -87.659172, -87.636427, -87.658447, -87.6240391666667,
-87.6494219288, -87.6358245313168, -87.6736431666667, -87.640552,
-87.6602845349, -87.69, -87.7080808333333, -87.67, -87.68, -87.667252,
-87.635825, -87.6257745, -87.65478743, -87.661501, -87.612043,
-87.649603, -87.634775, -87.6297910363, -87.659172, -87.6, -87.641066,
-87.658617, -87.634656, -87.6776919292, -87.62408432, -87.62,
-87.63, -87.634656, -87.630834, -87.7011951301, -87.627716, -87.6133453333333,
-87.6259215, -87.638437, -87.55, -87.625125, -87.69, -87.66098386,
-87.62, -87.6355345, -87.59, -87.66, -87.682437, -87.63, -87.630834,
-87.71, -87.6299358333333, -87.631697, -87.6523048564, -87.649807,
-87.64, -87.73, -87.663712, -87.622798, -87.67097, -87.65103,
-87.64117, -87.653818, -87.625125, -87.683825, -87.624426, -87.612043,
-87.65, -87.624117, -87.72, -87.6205801963806, -87.6776919292,
-87.6523048564, -87.63, -87.668507, -87.625813, -87.626761, -87.7081071666667,
-87.63128, -87.6974228024483, -87.64, -87.6439593333333, -87.6602738295
)), row.names = c(NA, -100L), class = "data.frame")
Next, i extracted min and max values for latitude and longitude and then used those values as limits for my map plot:
library(ggmap)
library(ggplot2)
map_lim <- data.frame(
min_lat = min(df[, c("start_lat", "end_lat")]),
max_lat = max(df[, c("start_lat", "end_lat")]),
min_lng = min(df[, c("start_lng", "end_lng")]),
max_lng = max(df[, c("start_lng", "end_lng")])
)
map_lim
## min_lat max_lat min_lng max_lng
## 1 41.69229 42.03 -87.74 -87.55
map <- get_stamenmap(
bbox = c(left = map_lim$min_lng, right = map_lim$max_lng, bottom = map_lim$min_lat, top = map_lim$max_lat)
)
ggmap(map)
This is the resulting map:
Now i want to add a 2d density plot based on the same set of coordinates on top of this map, but i don't know how to do it. This is my 2d density plot code.
density2d <- ggplot(df, aes(x = start_lng, y = start_lat)) +
coord_equal(xlim = c(map_lim$min_lng, map_lim$max_lng), ylim = c(map_lim$min_lat, map_lim$max_lat)) +
xlab("Longitude") +
ylab("Latitude") +
stat_density2d(aes(fill = ..level..), alpha = 0.5, geom = "polygon") +
scale_fill_viridis_c()
density2d

You simply add the density layer to your map:
ggmap(map) +
stat_density2d(data = df, aes(x = start_lng, y = start_lat,
fill = ..level..), alpha = 0.5, geom = "polygon") +
scale_fill_viridis_c()

Related

How can I smooth lines in ggplot?

I want to reproduce the following graph but with smoother lines:
Such that the lines are similar to the following graph:
So far, I've tried the following, but I only get a trend instead of smoothing the two series:
plot_fig4 <- ggplot(fig4, aes(x=dias))+
geom_line(aes(y=complete_preds_means), color="#9a6584", size=0.5)+
geom_line(aes(y=contrafact), colour="#000000", size=0.5) +
geom_line(aes(y=complete_preds_means), method = "lm", formula=y~spline(x,21))+
geom_ribbon(aes(ymin=complete_preds_lower, ymax=complete_preds_upper), fill="#9a6584", alpha=0.2)
My data:
structure(list(dias = structure(c(19052, 19053, 19054, 19055,
19056, 19057, 19058, 19059, 19060, 19061, 19062, 19063, 19064,
19065, 19066, 19067, 19068, 19069, 19070, 19071), class = "Date"),
complete_preds_means = c(341.07434, 381.59167, 455.47815,
485.05597, 527.60876, 562.63965, 602.48975, 624.663, 626.5637,
527.2239, 420.71643, 389.30804, 378.74396, 366.61548, 361.36566,
363.37253, 319.31824, 314.39688, 303.60342, 294.8934), contrafact = c(364.5,
358.89, 466.64, 470.11, 464.25, 487.27, 591.2, 715.33, 628.02,
505.98, 402.9, 316.81, 323.35, 358.61, 354.26, 369.5, 317.01,
336.5, 285.33, 270.91), complete_preds_lower = c(320.6368042,
361.7870895, 432.4487762, 461.2275833, 503.2255051, 535.7108551,
576.3850006, 597.9762146, 601.4407013, 504.0448837, 398.7777023,
368.0046799, 356.3603165, 345.5847885, 339.9679932, 342.7514801,
298.3247482, 293.4419693, 282.5286865, 275.4635284), complete_preds_upper = c(359.9897186,
402.5708664, 477.4746765, 508.7775711, 550.3326447, 587.6521027,
628.5320251, 649.9691833, 649.4831665, 547.9886108, 442.046402,
410.8121475, 399.0208908, 389.8615128, 387.4929993, 386.2935928,
340.140834, 336.3622116, 324.793483, 315.4606934)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
Like this?
df %>%
pivot_longer(-dias) %>%
ggplot() +
aes(x = dias, y = value, col = name) +
geom_smooth(se = FALSE)
You can use the function smooth, with the folowing parameters
Add one geom_smooth(...) line for each of your columns.
If you want the interval confidence for one serie, you switch the "se = FALSE" to True.
> ggplot()+ geom_smooth(data=data, aes(x=dias,
> y=complete_preds_means), method = loess, se=FALSE)

Customize alpha values based on conditions for multiple facets time series plots in R

For the time series plot which is composed by two subplots:
library(tidyverse)
library(lubridate)
library(feasts)
library(tsibble)
library(gghighlight)
df %>%
mutate(date = as.Date(date, origin = "1899-12-30")) %>%
mutate(year=as.numeric(year(date))) %>%
pivot_longer(`food_index`:`energy_index`) %>%
mutate(date=yearmonth(date)) %>%
as_tsibble(index=date, key=name) %>%
gg_season(value, alpha=1) +
geom_line(size=0.8, alpha=0.8) +
geom_point(size=2, alpha=1)
Out:
Let's say if the current year is 2022, I wanna to plot the line of that year with alpha=1, other years' lines with smaller alpha, ie., alpha=0.3.
How could I do that? Thanks for your helps at advance.
Data:
df <- structure(list(date = c(42766, 42794, 42825, 42855, 42886, 42916,
42947, 42978, 43008, 43039, 43069, 43100, 43131, 43159, 43190,
43220, 43251, 43281, 43312, 43343, 43373, 43404, 43434, 43465,
43496, 43524, 43555, 43585, 43616, 43646, 43677, 43708, 43738,
43769, 43799, 43830, 43861, 43890, 43921, 43951, 43982, 44012,
44043, 44074, 44104, 44135, 44165, 44196, 44227, 44255, 44286,
44316, 44347, 44377, 44408, 44439, 44469, 44500, 44530, 44561
), food_index = c(58.53, 61.23, 55.32, 55.34, 61.73, 56.91, 54.27,
59.08, 60.11, 66.01, 60.11, 63.41, 69.8, 72.45, 81.11, 89.64,
88.64, 88.62, 98.27, 111.11, 129.39, 140.14, 143.44, 169.21,
177.39, 163.88, 135.07, 151.28, 172.81, 143.82, 162.13, 172.22,
176.67, 179.3, 157.27, 169.12, 192.51, 194.2, 179.4, 169.1, 193.17,
174.92, 181.92, 188.41, 192.14, 203.41, 194.19, 174.3, 174.86,
182.33, 182.82, 185.36, 192.41, 195.59, 202.6, 201.51, 225.01,
243.78, 270.67, 304.57), energy_index = c(127.36, 119.87, 120.96,
112.09, 112.19, 109.24, 109.56, 106.89, 109.35, 108.35, 112.39,
117.77, 119.52, 122.24, 120.91, 125.41, 129.72, 135.25, 139.33,
148.6, 169.62, 184.23, 204.38, 198.55, 189.29, 202.47, 220.23,
240.67, 263.12, 249.74, 240.84, 243.42, 261.2, 256.76, 258.69,
277.98, 289.63, 293.46, 310.81, 318.68, 310.04, 302.17, 298.62,
260.92, 269.29, 258.84, 241.68, 224.18, 216.36, 226.57, 235.98,
253.86, 267.37, 261.99, 273.37, 280.91, 291.84, 297.88, 292.78,
289.79)), row.names = c(NA, 60L), class = "data.frame")
You could achieve this by creating a boolean variable that detects the year you would like to highlight and then passing that as the alpha aesthetic inside your plot:
df %>%
mutate(date = as.Date(date, origin = "1899-12-30")) %>%
mutate(year=as.numeric(year(date))) %>%
pivot_longer(`food_index`:`energy_index`) %>%
mutate(date=yearmonth(date),
highlight = ifelse(year == "2021", T, F)) %>%
as_tsibble(index=date, key=name) %>%
gg_season(value, alpha = 0.2) +
geom_line(aes(alpha = highlight),
size=0.8) +
geom_point(aes(alpha = highlight),
size=2) +
scale_alpha_manual(values = c(0.2, 1)) +
guides(alpha = "none") +
theme_bw()

Plot time series without straight lines

I am trying to plot some time series but since I have data only for the summer I get these straight lines. Any idea how to fix that? The code I used: Any idea would be helpful!
ggplot(ba, aes(x=date1, y=pc1)) +
geom_line(color="turquoise4") +
theme_minimal() +
labs(x="", y="Loading", title="Correlation of PC1 and original series") +
theme(plot.title = element_text(hjust=0.5, size=20, face="bold"))+
scale_x_date(date_labels= ("%Y"))
And the plot:
The data: structure(list(date1 = structure(c(10712, 10713, 10714, 10715,
10716, 10717, 10718, 10719, 10720, 10721, 10722, 10723, 10724,
10725, 10726, 10727, 10728, 10729, 10730, 10731, 10732, 10733,
10734, 10735, 10736, 10737, 10738, 10739, 10740, 10741, 10742,
10743, 10744, 10745, 10746, 10747, 10748, 10749, 10750, 10751,
10752, 10753, 10754, 10755, 10756, 10757, 10758, 10759, 10760,
10761, 10762, 10763, 10764, 10765, 10766, 10767, 10768, 10769,
10770, 10771, 10772, 10773, 10774, 10775, 10776, 10777, 10778,
10779, 10780, 10781, 10782, 10783, 10784, 10785, 10786, 10787,
10788, 10789, 10790, 10791, 10792, 10793, 10794, 10795, 10796,
10797, 10798, 10799, 10800, 10801, 10802, 10803, 10804, 10805,
10806, 10807, 10808, 10809, 10810, 10811, 10812, 10813, 10814,
10815, 10816, 10817, 10818, 10819, 10820, 10821, 10822, 10823,
10824, 10825, 10826, 10827, 10828, 10829, 10830, 10831, 10832,
10833, 10834, 10835, 10836, 10837, 10838, 10839, 10840, 10841,
10842, 10843, 10844, 10845, 10846, 10847, 10848, 10849, 10850,
10851, 10852, 10853, 10854, 10855, 10856, 10857, 10858, 10859,
10860, 10861, 10862, 10863, 10864, 11078, 11079, 11080, 11081,
11082, 11083, 11084, 11085, 11086, 11087, 11088, 11089, 11090,
11091, 11092, 11093, 11094, 11095, 11096, 11097, 11098, 11099,
11100, 11101, 11102, 11103, 11104, 11105, 11106, 11107, 11108,
11109, 11110, 11111, 11112, 11113, 11114, 11115, 11116, 11117,
11118, 11119, 11120, 11121, 11122, 11123, 11124), class = "Date"),
pc1 = c(2.64462123197862, 2.4380313244096, 2.21417935009087,
2.02249236956036, 1.75829175459456, 1.58770371446918, 1.62230139615394,
1.73502227021784, 1.75083678213192, 1.64509065138032, 1.57921033180313,
1.70228767677341, 1.77303175099386, 1.78384290706931, 1.86580160595479,
1.9106874120324, 1.73936455049801, 1.50577059168685, 1.24226003967481,
1.07813468676617, 1.06276891964951, 1.09622663209529, 1.07692457712675,
0.978692818737612, 1.06365064520783, 1.2525349982313, 1.08237838015766,
0.645239033194787, 0.479482241789711, 0.683701830568681,
0.792197472275541, 0.631531270886538, 0.520337262457156,
0.667200695099021, 0.767559380073353, 0.7856163663635, 0.737745147101418,
0.654712633988225, 0.440140874164089, 0.111631055132755,
-0.22450806112272, -0.444238159039355, -0.584576558346287,
-0.444097467542865, -0.227821057355029, -0.120935149111578,
-0.0932195161137341, 0.037283855810637, 0.206479031035409,
0.173515424607062, 0.234536409515456, 0.317957256707112,
0.290090191780606, 0.0607339338833623, -0.27556992053308,
-0.3586166955826, -0.3534130521313, -0.501651666926942, -0.571570071652576,
-0.79110428934397, -0.985635595643097, -0.994138228085185,
-0.839909782593256, -0.699274458194957, -0.580683825031177,
-0.530811870371419, -0.4746353951302, -0.489386570992314,
-0.787222651887671, -1.1059671054324, -1.17983265148469,
-1.1058432515423, -0.970485807735322, -0.679713450749357,
-0.516950863200668, -0.495312393712548, -0.673645368786615,
-0.792675131421433, -0.692021409445821, -0.611096320716252,
-0.676712376641795, -0.723244566814595, -0.621986199057006,
-0.563969216349158, -0.649311354664407, -0.679237194242732,
-0.624476984795223, -0.738344795218295, -0.877867797047079,
-0.879375052767018, -0.84262582765393, -0.845707036138972,
-0.959691974084994, -1.06904324062176, -0.97905489332525,
-0.847145240762566, -0.86837819324592, -0.935323976060101,
-0.796486491787169, -0.461073031709012, -0.275818888900351,
-0.513613296467615, -0.786611502858454, -0.799843667083875,
-0.632676241199403, -0.468611824279096, -0.534017599627378,
-0.501551518704511, -0.239313348556757, -0.208935210151003,
-0.510483950549102, -0.62974750963569, -0.399113422985878,
-0.072812659658845, 0.0377885597304766, -0.0102829082610216,
-0.0571349366394233, -0.101917027852624, -0.202941574141862,
-0.22849727264844, -0.125157862652187, 0.168703915373856,
0.43626132948925, 0.446099489882147, 0.435379929023588, 0.236210503991287,
-0.122289033919648, -0.288101855449495, -0.186400543130663,
0.0316721901308679, 0.121240481805255, -0.0753698973566349,
-0.384779730900963, -0.531179497125517, -0.373632181420806,
-0.0148926315001478, 0.146040939981569, 0.13371186468668,
0.200262938351445, 0.465073170745138, 0.506805629621484,
0.345398737766814, 0.171110245173291, 0.176555396235594,
0.262743070740985, 0.398601589660576, 0.433248104072272,
0.453883432665361, 0.604637145172226, 0.843278371818699,
1.13506306230201, 1.42652005730684, 1.63221068108998, 1.86442509826484,
1.97067279998339, 2.0139860665512, 2.13720187260212, 2.31355711206366,
2.32477728002809, 2.36236228869303, 2.24108767618426, 2.12991693141636,
2.11677885248848, 2.01466853738993, 1.77967782944265, 1.48938981000699,
1.34042958586002, 1.33016846412245, 1.31770813627339, 1.26104969519401,
1.37385446004522, 1.61517275597383, 1.84510291043685, 1.91280500843462,
1.84897419443657, 1.52674793906846, 1.29429812528379, 1.06717755247561,
0.910500917679731, 0.904461327314293, 1.05380123048097, 1.08631739987863,
1.04843964584885, 1.10153891962662, 1.15936307711726, 1.20129772010444,
1.18746954945955, 1.00056619329093, 0.725225823060771, 0.573790799694267,
0.655776789864271, 0.780033607405981, 0.664875593837605,
0.452000300833336, 0.394589410057676, 0.402170545544567,
0.403979206396259, 0.395485848597801, 0.433314713756909,
0.437960603442615)), row.names = c(NA, 200L), class = "data.frame")strong text
1) Using the input in the Note at the end expand the dates to include the missing ones using NA's for them. Then plot.
library(ggplot2)
library(zoo)
z <- read.zoo(ba)
zz <- merge(z, zoo(, seq(start(z), end(z), 1)))
autoplot(zz) + xlab("")
2) Another approach is to use distinct facets for each year.
library(ggplot2)
breaks <- unique(as.Date(cut(ba$date1, "month")))
ba2 <- transform(ba, year = as.integer(format(date1, "%Y")))
p <- ggplot(ba2, aes(date1, pc1)) +
geom_line() +
facet_grid(cols = vars(year), scales = "free_x", space = "free_x")
p + scale_x_date(breaks = breaks, date_labels = "%b")
(continued after image)
or to remove strip text and only place the year below each facet use p from above with
breaks <- as.Date(tapply(format(ba$date1), format(ba$date1, "%Y"), min))
p +
scale_x_date(breaks = breaks, date_labels = "%Y") +
theme(strip.text.x = element_blank())
3) The facet idea could also be implemented in lattice.
library(lattice)
ba3 <- transform(ba, year = format(date1, "%Y"))
xyplot(pc1 ~ date1 | year, ba3, type = "l",
scales = list(x = list(relation = "free")), layout = c(NA, 1))
Note
There was a problem with the dput output in the question so the following was used.
ba <-
structure(list(date1 = structure(c(10712, 10713, 10714, 10715,
10716, 10717, 10718, 10719, 10720, 10721, 10722, 10723, 10724,
10725, 10726, 10727, 10728, 10729, 10730, 10731, 10732, 10733,
10734, 10735, 10736, 10737, 10738, 10739, 10740, 10741, 10742,
10743, 10744, 10745, 10746, 10747, 10748, 10749, 10750, 10751,
10752, 10753, 10754, 10755, 10756, 10757, 10758, 10759, 10760,
10761, 10762, 10763, 10764, 10765, 10766, 10767, 10768, 10769,
10770, 10771, 10772, 10773, 10774, 10775, 10776, 10777, 10778,
10779, 10780, 10781, 10782, 10783, 10784, 10785, 10786, 10787,
10788, 10789, 10790, 10791, 10792, 10793, 10794, 10795, 10796,
10797, 10798, 10799, 10800, 10801, 10802, 10803, 10804, 10805,
10806, 10807, 10808, 10809, 10810, 10811, 10812, 10813, 10814,
10815, 10816, 10817, 10818, 10819, 10820, 10821, 10822, 10823,
10824, 10825, 10826, 10827, 10828, 10829, 10830, 10831, 10832,
10833, 10834, 10835, 10836, 10837, 10838, 10839, 10840, 10841,
10842, 10843, 10844, 10845, 10846, 10847, 10848, 10849, 10850,
10851, 10852, 10853, 10854, 10855, 10856, 10857, 10858, 10859,
10860, 10861, 10862, 10863, 10864, 11078, 11079, 11080, 11081,
11082, 11083, 11084, 11085, 11086, 11087, 11088, 11089, 11090,
11091, 11092, 11093, 11094, 11095, 11096, 11097, 11098, 11099,
11100, 11101, 11102, 11103, 11104, 11105, 11106, 11107, 11108,
11109, 11110, 11111, 11112, 11113, 11114, 11115, 11116, 11117,
11118, 11119, 11120, 11121, 11122, 11123, 11124), class = "Date"),
pc1 = c(2.64462123197862, 2.4380313244096, 2.21417935009087,
2.02249236956036, 1.75829175459456, 1.58770371446918, 1.62230139615394,
1.73502227021784, 1.75083678213192, 1.64509065138032, 1.57921033180313,
1.70228767677341, 1.77303175099386, 1.78384290706931, 1.86580160595479,
1.9106874120324, 1.73936455049801, 1.50577059168685, 1.24226003967481,
1.07813468676617, 1.06276891964951, 1.09622663209529, 1.07692457712675,
0.978692818737612, 1.06365064520783, 1.2525349982313, 1.08237838015766,
0.645239033194787, 0.479482241789711, 0.683701830568681,
0.792197472275541, 0.631531270886538, 0.520337262457156,
0.667200695099021, 0.767559380073353, 0.7856163663635, 0.737745147101418,
0.654712633988225, 0.440140874164089, 0.111631055132755,
-0.22450806112272, -0.444238159039355, -0.584576558346287,
-0.444097467542865, -0.227821057355029, -0.120935149111578,
-0.0932195161137341, 0.037283855810637, 0.206479031035409,
0.173515424607062, 0.234536409515456, 0.317957256707112,
0.290090191780606, 0.0607339338833623, -0.27556992053308,
-0.3586166955826, -0.3534130521313, -0.501651666926942, -0.571570071652576,
-0.79110428934397, -0.985635595643097, -0.994138228085185,
-0.839909782593256, -0.699274458194957, -0.580683825031177,
-0.530811870371419, -0.4746353951302, -0.489386570992314,
-0.787222651887671, -1.1059671054324, -1.17983265148469,
-1.1058432515423, -0.970485807735322, -0.679713450749357,
-0.516950863200668, -0.495312393712548, -0.673645368786615,
-0.792675131421433, -0.692021409445821, -0.611096320716252,
-0.676712376641795, -0.723244566814595, -0.621986199057006,
-0.563969216349158, -0.649311354664407, -0.679237194242732,
-0.624476984795223, -0.738344795218295, -0.877867797047079,
-0.879375052767018, -0.84262582765393, -0.845707036138972,
-0.959691974084994, -1.06904324062176, -0.97905489332525,
-0.847145240762566, -0.86837819324592, -0.935323976060101,
-0.796486491787169, -0.461073031709012, -0.275818888900351,
-0.513613296467615, -0.786611502858454, -0.799843667083875,
-0.632676241199403, -0.468611824279096, -0.534017599627378,
-0.501551518704511, -0.239313348556757, -0.208935210151003,
-0.510483950549102, -0.62974750963569, -0.399113422985878,
-0.072812659658845, 0.0377885597304766, -0.0102829082610216,
-0.0571349366394233, -0.101917027852624, -0.202941574141862,
-0.22849727264844, -0.125157862652187, 0.168703915373856,
0.43626132948925, 0.446099489882147, 0.435379929023588, 0.236210503991287,
-0.122289033919648, -0.288101855449495, -0.186400543130663,
0.0316721901308679, 0.121240481805255, -0.0753698973566349,
-0.384779730900963, -0.531179497125517, -0.373632181420806,
-0.0148926315001478, 0.146040939981569, 0.13371186468668,
0.200262938351445, 0.465073170745138, 0.506805629621484,
0.345398737766814, 0.171110245173291, 0.176555396235594,
0.262743070740985, 0.398601589660576, 0.433248104072272,
0.453883432665361, 0.604637145172226, 0.843278371818699,
1.13506306230201, 1.42652005730684, 1.63221068108998, 1.86442509826484,
1.97067279998339, 2.0139860665512, 2.13720187260212, 2.31355711206366,
2.32477728002809, 2.36236228869303, 2.24108767618426, 2.12991693141636,
2.11677885248848, 2.01466853738993, 1.77967782944265, 1.48938981000699,
1.34042958586002, 1.33016846412245, 1.31770813627339, 1.26104969519401,
1.37385446004522, 1.61517275597383, 1.84510291043685, 1.91280500843462,
1.84897419443657, 1.52674793906846, 1.29429812528379, 1.06717755247561,
0.910500917679731, 0.904461327314293, 1.05380123048097, 1.08631739987863,
1.04843964584885, 1.10153891962662, 1.15936307711726, 1.20129772010444,
1.18746954945955, 1.00056619329093, 0.725225823060771, 0.573790799694267,
0.655776789864271, 0.780033607405981, 0.664875593837605,
0.452000300833336, 0.394589410057676, 0.402170545544567,
0.403979206396259, 0.395485848597801, 0.433314713756909,
0.437960603442615)), row.names = c(NA, -200L), class = "data.frame")

R ggplot - Can't allocate big vector

I'm trying to plot a relatively small data set, and I can't get it to show me the plot. It keeps giving the error Error: cannot allocate vector of size 9.7 Gb. This doesn't make much sense to me as the data set is rather small.
> nrow(locs)
[1] 130
> head(locs)
STATION AVGTRANGE LAT LONG
1: USC00286979 22.13333 40.6971 -75.2042
2: USC00360022 21.33333 40.5361 -79.8152
3: USC00360132 24.37037 40.5227 -78.3694
4: USC00360140 19.80000 40.4949 -78.4667
5: USC00360147 22.36667 41.3585 -77.9262
6: USC00360457 20.68000 40.8209 -76.4983
How I'm plotting it.
gg <- ggplot(data = locs, aes(x = LONG, y = LAT)) +
geom_raster(aes(fill=AVGTRANGE), interpolate=TRUE)
gg # can't allocate here
Here is the dput my data.
> dput(locs)
structure(list(STATION = structure(1:130, .Label = c("USC00286979",
"USC00360022", "USC00360132", "USC00360140", "USC00360147", "USC00360457",
"USC00360560", "USC00360656", "USC00360754", "USC00360785", "USC00360861",
"USC00360868", "USC00361139", "USC00361212", "USC00361301", "USC00361350",
"USC00361354", "USC00361362", "USC00361377", "USC00361480", "USC00361485",
"USC00361705", "USC00361726", "USC00361751", "USC00361802", "USC00361810",
"USC00361838", "USC00361920", "USC00362071", "USC00362183", "USC00362323",
"USC00362470", "USC00362574", "USC00362721", "USC00362942", "USC00363018",
"USC00363028", "USC00363226", "USC00363311", "USC00363321", "USC00363343",
"USC00363417", "USC00363437", "USC00363451", "USC00363632", "USC00363665",
"USC00363698", "USC00364214", "USC00364325", "USC00364432", "USC00364763",
"USC00364778", "USC00364815", "USC00364839", "USC00364896", "USC00364934",
"USC00364976", "USC00364992", "USC00365050", "USC00365109", "USC00365344",
"USC00365573", "USC00365686", "USC00365738", "USC00365902", "USC00365918",
"USC00366111", "USC00366151", "USC00366194", "USC00366238", "USC00366508",
"USC00366649", "USC00366886", "USC00366921", "USC00366927", "USC00367029",
"USC00367073", "USC00367103", "USC00367167", "USC00367186", "USC00367229",
"USC00367409", "USC00367477", "USC00367732", "USC00367782", "USC00367863",
"USC00367931", "USC00367938", "USC00368073", "USC00368184", "USC00368308",
"USC00368361", "USC00368400", "USC00368449", "USC00368469", "USC00368596",
"USC00368668", "USC00368868", "USC00368873", "USC00368888", "USC00368905",
"USC00369298", "USC00369367", "USC00369408", "USC00369823", "USR0000PALL",
"USW00003761", "USW00004726", "USW00004751", "USW00004787", "USW00004843",
"USW00013739", "USW00014711", "USW00014712", "USW00014736", "USW00014737",
"USW00014751", "USW00014762", "USW00014770", "USW00014777", "USW00014778",
"USW00014860", "USW00054737", "USW00054782", "USW00054786", "USW00054789",
"USW00054792", "USW00093778", "USW00094732", "USW00094823"), class = "factor"),
AVGTRANGE = c(22.1333333333333, 21.3333333333333, 24.3703703703704,
19.8, 22.3666666666667, 20.68, 23.35, 21.4333333333333, 25.75,
23.4333333333333, 23.6428571428571, 26.4333333333333, 27.551724137931,
25.3448275862069, 25.0666666666667, 26.6842105263158, 23.4444444444444,
29.6, 23.3, 30.2631578947368, 27.0454545454545, 25.9333333333333,
24.2083333333333, 27.448275862069, 28.2333333333333, 21.4666666666667,
24.1111111111111, 25.7333333333333, 23.8571428571429, 21.6,
26.08, 26.2916666666667, 27.1034482758621, 28.3666666666667,
27.9259259259259, 23.6, 25.7, 26.3666666666667, 26.0344827586207,
20.2666666666667, 23.0909090909091, 27.2727272727273, 25.9666666666667,
24.8214285714286, 20.2413793103448, 24.0333333333333, 20.6333333333333,
26.0344827586207, 22.6, 29.0333333333333, NA, 25.625, 19.0333333333333,
18.7666666666667, 21.0689655172414, 22, 24.1333333333333,
25.0333333333333, 24.0666666666667, 24.3666666666667, 20.7333333333333,
32.5, 26.6666666666667, NA, 22.2666666666667, 25.1333333333333,
27.1481481481481, 22.7, 24.4827586206897, 21.6071428571429,
20.8461538461538, 29.9333333333333, 17.3928571428571, 26.2666666666667,
23.84, 23.1481481481481, 23.8275862068966, 26.9, 26.7931034482759,
25.3636363636364, NA, 23.5333333333333, 27.3571428571429,
17.2, 24.5, 22.0666666666667, NA, 23.8333333333333, 26.5172413793103,
27.6551724137931, 21.2307692307692, 26.5384615384615, 19.5,
20.8, 25.3, 18.6666666666667, 25.2758620689655, 23.8333333333333,
24.3461538461538, 27.6551724137931, 25.7666666666667, 24,
26.0344827586207, 24.6, 28.7333333333333, 27.7, 20.1034482758621,
18.6071428571429, 26.1785714285714, 22.5714285714286, 22.6071428571429,
17.1785714285714, 19.3571428571429, 21.6071428571429, 24.4285714285714,
23.6071428571429, 21.6785714285714, 19.9642857142857, 25.2142857142857,
22.7241379310345, 23.0357142857143, 17.8928571428571, 22.2962962962963,
21.2857142857143, 21.8571428571429, 21, 25.6428571428571,
25.6071428571429, 19.4444444444444, 22.6785714285714), LAT = c(40.6971,
40.5361, 40.5227, 40.4949, 41.3585, 40.8209, 40.8619, 39.9355,
41.0072, 40.3803, 40.3916, 41.8975, 40.8415, 41.6516, 41.5217,
39.848, 39.9353, 41.9301, 40.1468, 41.0489, 41.1922, 39.7994,
39.9969, 41.3575, 41.775, 41.7391, 41.9903, 40.2258, 40.46,
40.1275, 41.5216, 40.4681, 40.50194, 40.0136, 40.71306, 41.1184,
41.4004, 39.8815, 41.5631, 40.0962, 40.5513, 40.9666, 40.2305,
39.78333, 40.5511, 39.77056, 40.2817, 40.5972, 41.4992, 41.6767,
40.0499, 40.1167, 41.4234, 40.1692, 40.3333, 40.8223, 40.9474,
40.5864, 41.64583, 41.131, 40.8344, 40.3391, 39.7808, 41.6725,
40.6475, 40.5319, 40.412, 40.61417, 40.1482, 40.075, 39.8,
41.9245, 39.9587, 40.8729, 40.12, 41.7394, 39.7275, 41.8157,
40.6515, 41.589, 40.9248, 41.3299, 41.4196, 39.8958, 40.5101,
40.683, 40.7831, 40.335, 40.05889, 41.05583, 39.8582, 41.8162,
40.5711, 40.7933, 41.40389, 41.008, 40.8532, 41.8975, 41.4792,
41.63, 41.7511, 41.84667, 39.89861, 41.7004, 40.0417, 41.4864,
39.8593, 40.31611, 41.8, 41.17833, 41.62639, 39.87327, 40.1962,
40.36667, 40.29639, 40.64985, 40.21722, 40.35472, 40.82056,
41.3336, 41.2433, 42.0803, 40.12028, 40.23833, 40.33, 41.13889,
41.04667, 39.91806, 40.08194, 40.4846), LONG = c(-75.2042,
-79.8152, -78.3694, -78.4667, -77.9262, -76.4983, -75.6428,
-77.2577, -76.4482, -76.0274, -79.8594, -78.7144, -79.9163,
-76.8463, -77.4478, -79.5898, -77.6394, -79.297, -79.8986,
-77.9411, -79.4361, -79.3665, -79.5963, -79.2172, -78.0417,
-77.971, -77.1567, -77.1894, -76.8703, -79.4058, -76.4043,
-78.7289, -80.0833, -78.3653, -79.5144, -75.7277, -79.8305,
-77.3506, -78.6014, -75.7513, -80.2167, -78.5871, -75.4354,
-79.9166, -75.9913, -77.0325, -76.8703, -79.1186, -80.4681,
-78.8036, -76.2742, -76.4333, -76.4933, -79.1411, -76.4667,
-75.6962, -76.8786, -77.5692, -80.425, -77.4336, -76.1352,
-79.8604, -79.041, -75.0641, -80.3861, -80.2172, -79.7245,
-79.7191, -74.953, -76.0717, -76.05, -78.0072, -75.1728,
-78.2161, -75.5011, -75.4465, -79.913, -78.2873, -78.5551,
-75.3303, -79.2825, -77.7381, -78.7493, -76.3948, -79.5459,
-79.6684, -76.8617, -75.313, -77.5213, -80.06, -77.4774,
-80.4249, -75.2781, -77.8672, -78.0183, -75.1876, -76.7891,
-77.1419, -79.4432, -79.693, -76.443, -79.1494, -80.1655,
-77.3871, -78.5278, -79.1025, -75.7861, -78.8338, -78.6333,
-78.8988, -80.215, -75.2267, -76.7724, -75.9666, -78.3202,
-75.4477, -76.8513, -79.9216, -76.8641, -75.7269, -76.9217,
-80.1824, -76.2944, -75.5572, -75.1225, -75.3794, -78.4116,
-76.8741, -75.0111, -80.2144)), .Names = c("STATION", "AVGTRANGE",
"LAT", "LONG"), class = c("data.table", "data.frame"), row.names = c(NA,
-130L), .internal.selfref = <pointer: 0x2a40128>, sorted = "STATION")
I am not sure what your are trying to achieve with the geom_raster, as your data does not seem to fit the purpose.
Consider the outputs of dot plot:
gg <- ggplot(data = locs, aes(x = LONG, y = LAT, colour = AVGTRANGE)) +
geom_point()
#geom_raster(aes(fill=AVGTRANGE), interpolate=TRUE)
gg
data(faithfuld)
gg <- ggplot(faithfuld, aes(waiting, eruptions, colour = density)) +
geom_point()
#geom_raster(aes(fill = density), interpolate = TRUE)
gg
I have also tried geom_contour on your data and it does not work:
gg <- ggplot(data = locs, aes(x = LONG, y = LAT, z = AVGTRANGE)) +
geom_contour()
gg
UPDATE
I have checked the code of the geom_raster and the reason it tries to create the giant plot is that resolution of the plot is based on the minimal distance between points. As some of the points in your data are quite close to each other size of the matrix is so large.
If you round LAT and LONG to 2 digits code works.
locs$LAT <- round(locs$LAT, 0)
locs$LONG <- round(locs$LONG, 0)

Colour lattice wireframe from class formula by z data heights

Edit: I've discovered one solution, which is to transform the 2d Temp matrix into a 1D array, re-running the code with just this matrix gave me the output I wanted i.e. plot coloured by height of z data. The code is below:
mycols<-colorRampPalette(c("dodgerblue", "firebrick"), space="rgb")
wireframe(Temp,
zlim=c(10,18),
ylab=list(label="Time", rot=-35), scales=list(arrows=FALSE),
zlab=list(label=expression(paste("Tw (", degree, "C)")), rot=94),
xlab=list(label="Distance downstream (m)", rot=35),
drape = T, shade=F, colorkey = T, aspect = c(1,1),
col.regions=mycols(200), col="black")
I'm using Windows 7, R version 2.15.0 and lattice_0.20-6.
I'm plotting observed data in lattice using wireframe, I have 3 matrices of observed values (Temp,Dist,Time) so I'm using the formula method (Temp~Dist*Time). How do I instruct wireframe to colour the wireframe based on the heights/ values of my z (Temp) data. At present my code produces a wireframe that colours the plot based on the values of my y (Time) data.
I attach below my code.
mycols<-colorRampPalette(c("dodgerblue", "firebrick"), space="rgb")
wireframe(Temp[1:13,97:193]~Dist[1:13,97:193]*Time[1:13,97:193],
zlim=c(10,18),
ylab=list(label="Time", rot=-35), scales=list(arrows=FALSE),
zlab=list(label=expression(paste("Tw (", degree, "C)")), rot=94),
xlab=list(label="Distance downstream (m)", rot=35),
drape = T, shade=F, colorkey = T, aspect = c(1,1),
col.regions=mycols(200), col="black")
Some sample data. Please excuse the naming of matrix columns:
structure(list(Tw1.1. = c(12.15, 11.18526437, 10.51390093, 10.134,
9.711, 9.597, 9.59, 9.557, 9.602, 9.673, 9.753, 10.017, 10.32
), Tw1.1..1 = c(11.97, 11.05071394, 10.39239194, 10.011, 9.63,
9.546, 9.59, 9.571, 9.648, 9.745, 9.837, 10.171, 10.49), Tw1.1..2 = c(11.79,
10.90182264, 10.26796411, 9.893, 9.563, 9.52, 9.6, 9.619, 9.713,
9.808, 9.956, 10.321, 10.6), Tw1.1..3 = c(11.64, 10.74647418,
10.14505213, 9.788, 9.526, 9.525, 9.62, 9.682, 9.787, 9.914,
10.105, 10.42, 10.7), Tw1.1..4 = c(11.52, 10.58632287, 10.01657543,
9.699, 9.514, 9.543, 9.67, 9.743, 9.885, 10.049, 10.249, 10.528,
10.79), Tw1.1..5 = c(11.39, 10.46294559, 9.879615153, 9.619,
9.529, 9.577, 9.74, 9.823, 10.017, 10.21, 10.361, 10.58, 10.84
), Tw1.1..6 = c(11.26, 10.3417786, 9.765186747, 9.576, 9.557,
9.645, 9.81, 9.933, 10.186, 10.344, 10.474, 10.664, 10.94), Tw1.1..7 = c(11.1,
10.22494533, 9.674806064, 9.546, 9.605, 9.717, 9.9, 10.072, 10.338,
10.453, 10.557, 10.76, 11.03), Tw1.1..8 = c(10.93, 10.1003152,
9.604424236, 9.549, 9.676, 9.8, 10, 10.244, 10.436, 10.561, 10.668,
10.848, 11.12), Tw1.1..9 = c(10.76, 9.970098496, 9.545171854,
9.577, 9.757, 9.891, 10.15, 10.399, 10.558, 10.667, 10.778, 10.941,
11.22), Tw1.1..10 = c(10.63, 9.858851801, 9.501869458, 9.611,
9.851, 10.014, 10.31, 10.503, 10.683, 10.789, 10.868, 11.059,
11.34), Tw1.1..11 = c(10.51, 9.770908839, 9.48413064, 9.676,
9.946, 10.164, 10.48, 10.632, 10.8, 10.89, 10.945, 11.152, 11.41
), Tw1.1..12 = c(10.4, 9.702806469, 9.508922546, 9.756, 10.074,
10.332, 10.59, 10.774, 10.905, 10.96, 11.031, 11.213, 11.48)), .Names = c("Tw1.1.",
"Tw1.1..1", "Tw1.1..2", "Tw1.1..3", "Tw1.1..4", "Tw1.1..5", "Tw1.1..6",
"Tw1.1..7", "Tw1.1..8", "Tw1.1..9", "Tw1.1..10", "Tw1.1..11",
"Tw1.1..12"), class = "data.frame", row.names = c("Open", "B8",
"B12", "B25", "B26", "B13", "UsAWS", "B19", "B5", "B3", "B27",
"B17", "DSAWS"))

Resources