# Density plots/ kernel densities

0

I am trying to create a map from latitude and longitudes of catch locations. I was able to plot this on the map, however I would like to make densities of these locations. Therefore I have a few questions... (Image is in link below)
: https://i.stack.imgur.com/SvqNH.jpg

1. Would that require me to zoom in on only the Atlantic ocean?
2. Should the points be smaller?
3. How the heck can I map kernel densities or densities of the most used locations of these coordinates?
``````library(maps)
library(ggplot2)
library(stringr)
world_map <- map_data("world")
#Creat a base plot with gpplot2
p <- ggplot() + coord_fixed() +
xlab("") + ylab("")

base_world_messy <- p + geom_polygon(data=world_map, aes(x=long, y=lat, group=group),
colour="black", fill="light green") +
ggtitle("striper reports")

base_world_messy

#Strip the map down so it looks clean
cleanup <-
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_rect(fill = 'blue', colour = 'blue'),
axis.line = element_line(colour = "white"), legend.position="none",
axis.ticks=element_blank(), axis.text.x=element_blank(),
axis.text.y=element_blank())

base_world <- base_world_messy + cleanup

base_world

map_data <-
base_world +
geom_point(data=striper,
aes(x=Long, y=Lat), colour="Red",
fill="Red",pch=1, size=.2, alpha=I(0.5)) + ggtitle("striper report")
map_data
``````

try

``````map_data <-
base_world +
geom_density2d(data=striper,
aes(x=Long, y=Lat), colour="Red",
) + ggtitle("striper report")
``````

1 Like

Hello,
it yielded a blank map without any data points. Is this because I replace the geom_points with the geom_density2d?

I took your code. made random striper data. and changed geom_points to geom_density2d
the result on my machine is:

``````library(maps)
library(ggplot2)
library(stringr)
world_map <- map_data("world")
#Creat a base plot with gpplot2
p <- ggplot() + coord_fixed() +
xlab("") + ylab("")

base_world_messy <- p + geom_polygon(data=world_map, aes(x=long, y=lat, group=group),
colour="black", fill="light green") +
ggtitle("striper reports")

base_world_messy

#Strip the map down so it looks clean
cleanup <-
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_rect(fill = 'blue', colour = 'blue'),
axis.line = element_line(colour = "white"), legend.position="none",
axis.ticks=element_blank(), axis.text.x=element_blank(),
axis.text.y=element_blank())

base_world <- base_world_messy + cleanup

base_world

set.seed(42)

striper <- data.frame(Long=rnorm(1000,-60,10),
Lat = runif(100,19,71.4))

map_data <-
base_world +
geom_density2d(data=striper,
aes(x=Long, y=Lat), colour="Red",
) + ggtitle("striper report")
map_data
``````
1 Like

Thank you for your time, I believe it is from my initial csv file.
I have two columns :
Lat Long
78.945 45.656
.... .......
...... .....

This extends for a few thousand, is there a more compatible format I should use?

that doesnt seem different to what I had assumed when I made an artificial striper....
what output do you get when you ignore the map part and simply plot points and density of your striper ?

``````m <- ggplot(striper, aes(x = Long, y = Lat)) +
geom_point()

# contour lines
m + geom_density_2d()``````

I receive an output of a plot with lat and long on each axis. It appears fine in a plot format.

and can you share the visual output of ?

`````` base_world +
geom_density2d(data=striper,
aes(x=Long, y=Lat), colour="Red"  )``````
1 Like

I think it worked! The density is just very contained...

great, perhaps play around with bins param

``````base_world +
geom_density2d(data=striper,
aes(x=Long, y=Lat), colour="Red" ,
bins=5 )
``````
``````base_world +
geom_density2d(data=striper,
aes(x=Long, y=Lat), colour="Red" ,
bins=50 )
``````

And as you say, you might consider zooming the map

1 Like