I am trying to create a ggplot that includes coordinates, density, and a convex hull polygon.
The data is a set of twenty latitudinal and longitudinal points.
This is my code:
# Data
economy <- read.csv("data-economy.csv", header=TRUE)
# Convex hulls.
hulls <- ddply(economy, .(Latitude, Longitude), function(economy)
economy[chull(economy$Latitude, economy$Longitude), ])
fig <- ggplot(economy, aes(Latitude, Longitude, colour="black", fill="black")) +
geom_point() +
geom_density2d(alpha=.5) +
labs(x = "Latitude", y = "Longitude") +
geom_polygon(data=hulls, alpha=.2)
fig
The resulting plot looks like this:
I've tried a few things, and I can't get the convex hull to include only the points with max latitude and longitude. I can get the shape that I want outside of ggplot by using this code:
X <- economy
chull(X)
plot(X, cex = 0.5)
hpts <- chull(X)
hpts <- c(hpts, hpts[1])
lines(X[hpts, ])
The result that it gives me is this:
How can I get the same shape as in R base in ggplot?
Also, why when I change the color in my ggplot code, does it not change the plot?