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?


ddplycall you are splitting your data by unique values ofLatitudeandLongitude(i.e. distinct points) and finding the convex hull for each point, which is just the point itself. - Mikko Marttilahulls <- economy[chull(economy$Latitude, economy$Longitude), ]- Mikko Marttiladdplyin order to find a convex hull for multiple groups at the same time; while in your problem you just have one set of points to find a solution for, so you don't needddplyhere. - Mikko Marttila