Color points by date in ggplot2 - r

Hi all: I am struggling to color points by date in ggplot2. There are two outcomes that would work for me here. 1) colour the points by the variable recent_elections and just add straight lines denoting the date of the most recent election for each point. The current code does that. 2) preferably, but harder, just add the lines, coloured differently for each election, showing a legend that printed the date of the most recent federal election.
My current data and attempt is below.
library(dplyr)
library(tidyr)
library(ggplot2)
members <- structure(list(date = structure(c(6209, 6574, 7305, 14984, 15339,
15341, 17169, 17174), class = "Date"), members = c(180835, 193225,
200010, 86545, 95000, 128351, 41000, 124000), population = c(26449000,
26798000, 27512000, 33476688, 33476688, 33476688, 35151728, 35151728
), votes_previous_election = c(2359915, 2685263, 2685263, 4508474,
4508474, 4508474, 3470350, 3470350), vote_percent = c(18.8, 20.4,
20.4, 30.6, 30.6, 30.6, 19.7, 19.7), seats_previous_election = c(32,
43, 43, 103, 103, 103, 44, 44), recent_election = structure(c(5360,
6899, 6899, 15096, 15096, 15096, 16727, 16727), class = "Date")), .Names =
c("date",
"members", "population", "votes_previous_election", "vote_percent",
"seats_previous_election", "recent_election"), class = "data.frame",
row.names = c(NA,
-8L))
members %>%
select(population, votes_previous_election, seats_previous_election, members,
date, recent_election) %>%
mutate(., members_per_capita=members/population,
members_votes=members/votes_previous_election,
members_seats=members/seats_previous_election) %>%
gather(Variable, Value, c(members_per_capita,members_votes,
members_seats))%>%
ggplot(., aes(x=date, y=Value,
group=recent_election))+
geom_point(aes(fill=recent_election))+
facet_wrap(~Variable, scales='free')+
geom_vline(data=members, aes(xintercept=as.numeric(recent_election), col='red'), show.legend=F)

members %>%
select(population, votes_previous_election, seats_previous_election, members,
date, recent_election) %>%
mutate(., members_per_capita=members/population,
members_votes=members/votes_previous_election,
members_seats=members/seats_previous_election) %>%
gather(Variable, Value, c(members_per_capita,members_votes,
members_seats))%>%
ggplot(., aes(x=date, y=Value,
group=recent_election))+
geom_point()+
geom_vline(data=members, aes(xintercept=as.numeric(recent_election), col=factor(recent_election)), show.legend=T)+
facet_wrap(~Variable, scales='free') +
scale_color_discrete(name = "Recent Election") + xlim(as.Date("1984-01-01"), NA)
I changed the col="red" in geom_vline to col=factor(recent_election) so that the vertical lines are colored by recent_election. The factor() makes sure that recent_election is treated as discrete instead of continuous. scale_color_discrete sets the legend title. Note that the election date "1984-09-04" is going out of the x range of your points, so I added a xlim(as.Date("1984-01-01"), NA) to also include that election date. NA sets the upper limit automatically.

Related

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()

Scatter plot with ggplot2 colored by specific dates interval in r

I'm trying to assign different colors to the scatterplot based on their dates, more specifically the year.
This is how my dataset looks like:
> dput(head(CORt_r100_stack_join_fspec,10))
structure(list(Date = structure(c(16779, 16834, 16884, 16924,
16973, 16997, 17031, 17184, 17214, 17254), class = "Date"), meanNDVIN_int = c(0.677501157246889,
0.632728796482024, 0.578636981692124, 0.547002029242488, 0.632635423362751,
NA, 0.699596252720458, 0.670059391804396, 0.643347941166436,
0.674034259709311), meanNDVIW_int = c(0.784142418592418, 0.652437451242156,
0.648319814752948, 0.593432266488189, 0.767890365415717, NA,
0.779249089832163, 0.71974944410843, 0.715777992826006, 0.685045115352089
), meanNDVIE_int = c(0.703614512017928, 0.701963337684803, 0.488628353756438,
0.631309466083632, 0.781589421376217, NA, 0.799663418920722,
0.78910564747191, 0.710962969930836, 0.715644011856453), meanNDVINr_int_f = c(0.677501157246889,
0.632728796482024, 0.578636981692124, 0.547002029242488, 0.632635423362751,
0.687343078509066, 0.699596252720458, 0.670059391804396, 0.643347941166436,
0.674034259709311), meanNDVIWr_int_f = c(0.784142418592418, 0.652437451242156,
0.648319814752948, 0.593432266488189, 0.767890365415717, 0.749505859407419,
0.779249089832163, 0.71974944410843, 0.715777992826006, 0.685045115352089
), meanNDVIEr_int_f = c(0.703614512017928, 0.701963337684803,
0.488628353756438, 0.631309466083632, 0.781589421376217, 0.625916155640988,
0.799663418920722, 0.78910564747191, 0.710962969930836, 0.715644011856453
), NDVI_N = c(0.17221248, 0.644239685, 0.57222623, 0.558666635,
0.51654034, 0.42053949, 0.396706695, 0.641767447, 0.641008268,
0.662841949), NDVI_W = c(0.08182944, 0.69112807, 0.637699375,
0.629429605, 0.658829525, 0.60621678, 0.57186129, 0.72636742,
0.724193596, 0.738424976), NDVI_E = c(0.17135712, 0.659222803,
0.58665977, 0.573081253, 0.533498035, 0.437643585, 0.412841468,
0.652057206, 0.651854988, 0.670345511), NDVI_U = c(0.40520304,
0.578414833, 0.455746833, 0.428289893, 0.208847548, 0, 0, 0.475193691,
0.478691084, 0.505043773)), row.names = c(NA, 10L), class = "data.frame")
I've been plotting meanNDVIN_int against NDVI_N using this code:
ggplot(CORt_r100_join_fspec_2NDVIday,aes(x=NDVI_N)) +
geom_point(aes(y=meanNDVIN_int), colour="red")
theme_bw()+
ylab("meanNDVIN_int")+
xlab("NDVI_N")
Now I want to color each point differently (no matter the color) based on their year, 2015, 2016, and 2017.
I've used the scale_color_manual function to introduce the dates but no success so far.
Any help will be much appreciated.
Here is an alternative where you substring the first 4 characters from Date in color
df
ggplot(df,aes(x=NDVI_N)) +
geom_point(aes(y=meanNDVIN_int, color=substring(Date,1,4))) +
labs(color="Year")+
theme_bw()+
ylab("meanNDVIN_int")+
xlab("NDVI_N")
I created a year variable with lubridate and stored it asfactor for discrete colouring. You were just missing moving color inside the aes() to color it by year.
# Add year Variable;
CORt_r100_stack_join_fspec <- CORt_r100_stack_join_fspec %>% mutate(
year = as.factor(lubridate::year(Date))
)
# Plot;
ggplot(CORt_r100_stack_join_fspec,aes(x=NDVI_N)) +
geom_point(aes(y=meanNDVIN_int, color = year)) +
theme_bw() +
ylab("meanNDVIN_int")+
xlab("NDVI_N")
Note: The data you provided, and named is not the same as in your plot-call. So I changed CORt_r100_join_fspec_2NDVIday to CORt_r100_join_fspec_2NDVIday to make the plot and mutate function properly.

Making a Stacked Bar Chart Out of Table Columns in R

I'm trying to create a stacked bar graph showing body composition. I have a table/data set (I don't know the correct term) that looks like this:
structure(list(data.Date = structure(1:7, .Label = c("2021-03-06",
"2021-03-07", "2021-03-08", "2021-03-09", "2021-03-10", "2021-03-11",
"2021-03-12"), class = "factor"), total_bf = c(19.6612, 18.2182,
19.6803, 21.7047, 18.126, 19.7, 19.1424), total_muscle = c(41.5948,
43.043, 42.1578, 42.1866, 43.4017, 42.2, 42.2728), other = c(37.544,
38.8388, 38.0619, 38.0087, 39.1723, 38.1, 38.2848)), class = "data.frame", row.names = c(NA,
-7L))
Each column is a weight in kilograms. Together they add up to the total body weight of the subject. What I want is a stacked bar graph where each bar represents a date and each bar is split by total_bf, total_muscle and other. All of the guides and Q&As I've seen don't seem to apply to my situation. Maybe this is because I am new but nothing I've tried has worked yet.
An example of what I'm trying to achieve:
The only difference is that on my graph blue would be body fat (total_bf), green would be other and red would be muscle (total_muscle).
You can convert data from the wide format to the long format using tidyr::pivot_longer() function:
library(ggplot2)
df <- structure(list(
data.Date = structure(
1:7,
.Label = c("2021-03-06", "2021-03-07", "2021-03-08", "2021-03-09",
"2021-03-10", "2021-03-11", "2021-03-12"), class = "factor"),
total_bf = c(19.6612, 18.2182, 19.6803, 21.7047, 18.126, 19.7, 19.1424),
total_muscle = c(41.5948, 43.043, 42.1578, 42.1866, 43.4017, 42.2, 42.2728),
other = c(37.544, 38.8388, 38.0619, 38.0087, 39.1723, 38.1, 38.2848)
), class = "data.frame", row.names = c(NA, -7L))
long <- tidyr::pivot_longer(df, -data.Date)
Then using ggplot2, the defaults already make a stacked bar chart, so you just need to specify x, y and fill aesthetics.
ggplot(long, aes(data.Date, value, fill = name)) +
geom_col()
Since your date is encoded as a factor, if you want to encode it as a real date you can convert it as follows:
long$date <- as.Date(strptime(as.character(long$data.Date), format = "%Y-%m-%d"))
ggplot(long, aes(date, value, fill = name)) +
geom_col()
Created on 2021-03-12 by the reprex package (v0.3.0)

I'm learning R. I was wondering how I could make a barplot with a data set I've made?

Here is the data I've made:
structure(list(Resort = c("Park City", "Powder Mountain", "Snowbird",
"Alta", "Snow Basin", "Deer Valley"), `Named Runs` = c(348, 154,
140, 116, 107, 103)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
Here is my code. I'm trying to get a bar graph, with resorts on the x-axis and named runs on the y-axis. Unfortunately, my graph is only showing resorts. How can I fix this? Thanks.
library(readxl)
Utah_ski_resort_data_ <- read_excel("Desktop/Utah ski resort data..xlsx")
View(Utah_ski_resort_data_)
table(Utah_ski_resort_data_$Resort)
table(Utah_ski_resort_data_$`Named Runs`)
barplot(table(Utah_ski_resort_data_$Resort)
barplot with formula:
barplot(`Named Runs` ~ Resort, Utah_ski_resort_data_)
And this is a ggplot2 barplot:
library(ggplot2)
library(dplyr)
Utah_ski_resort_data_ %>%
ggplot(aes(x = Resort, y = `Named Runs`)) +
geom_bar(stat = "identity")

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)

Resources