Reputation: 726
I have a large data frame with a ton of observations. The data frame is called totdets_per_month
and the first lines of data looks like this:
month yearcollected deploy_long deploy_lat total
<ord> <int> <dbl> <dbl> <int>
1 Jan 2016 -54.6 39.9 1
2 Jan 2016 -54.6 39.9 2
3 Jan 2016 -54.6 39.9 9
4 Jan 2016 -54.4 39.9 9
5 Jan 2016 -54.4 39.9 2
6 Jan 2016 -54.4 39.9 2
I have data for all months at a range of coordinates,
I'm trying to map these coordinates as points, but have the points assigned to a gradient of color based on my numeric column total
.
This is my code to plot the dataframe
#Importing a map
world = ne_countries(scale = "medium", returnclass = "sf")
#map of total detections in each zone per month
ggplot(data = world) +
geom_sf(fill = "lightgrey") +
coord_sf(xlim=c(-64.5,-62.8), ylim=c(42.7,45), expand = FALSE) +
geom_rect(data = rect1, aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2), fill = NA, color = "black", size = 1) +
geom_point(data = totdets_per_month ,
mapping = aes(x = deploy_long,
y = deploy_lat,
fill = total),
pch = 21) +
scale_colour_gradient(low="black", high="green") +
theme(panel.background = element_rect(fill = "#add8e6"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank()) +
facet_wrap(vars(month))
This is the image the code outputs
as you can see the points are all one color. Does anybody know how to fix this?
Upvotes: 1
Views: 1806
Reputation: 24079
This is a consolidated answer based on my comments above:
Notice the color scale is going from black to blue and not to green. You have a problem with your aes definition. In your aes, you are specifying "fill=total", but you are only defining scale_colour_gradient
, first change scale_colour_gradient
to scale_fill_gradient
.
#create sample data
x<- runif(20, 1, 20)
y<-x^4
df<-data.frame(x, y)
library(ggplot2)
#this is a linear scale for the colors
ggplot(df) + geom_point(aes(x=x, y=y, fill=y), pch=21, size=4) +
scale_fill_gradient(low="black", high="green")
In the plot above the few extremely large values of y is causing most of the remaining points to plot black. In order to adjust for this skewing the color scale, then you will need to manually specify the color breaks.
In the example below, I used the cut
function to divide my range by the power of 10 and assign break to a different color value.
#create color ramp
colfunc <-colorRampPalette(c("black", "green"))
#adjust scaling of the colors and
# add custom color color to dataframe
df$color<-cut(y, breaks=c(0, 10, 100, 1000, 10000, 100000, Inf))
df$color<-factor(df$color, labels=colfunc(5))
ggplot(df) + geom_point(aes(x=x, y=y, fill=color), pch=21, size=4) +
scale_fill_identity(guide="legend", labels=c(10^(1:5)))
Now the plot does a better job of showing the distribution.
Upvotes: 1