Hanna
Hanna

Reputation: 69

R: Data Pushes Map Boundaries (ggplot()/maps())

I'm currently struggling trying to create hard parameters for this plot. When I populate it with more points from basindf it ends up pushing the xlim well beyond the parameters I would need. When that happens, I attempt to adjust the xlim within all of the functions(map, ggplot, geom_polygon, geom_path) and nothing happens. Any suggestions?

map.dat <- map_data(map(database= "world", ylim=c(15,90), xlim=c(-180,-24), fill = TRUE))
ggplot(map.dat) +
  geom_polygon(aes(x=long, y=lat, group=group, fill=NULL)) +
  geom_path(data = basindf, aes(x = latitude, y = longitude, group = factor(id), col = wind)) + 
  theme(legend.position = "none")

You can use this set of information to substitute for basindf. It should display the problem.

"1128","1957244N15210",1957,"September",1957-09-01,"EP",-149.6,15,25
"1129","1957244N15210",1957,"September",1957-09-01,"EP",-150.5,15,45
"1130","1957244N15210",1957,"September",1957-09-01,"EP",-151.6,15.1,45
"1131","1957244N15210",1957,"September",1957-09-01,"EP",-152.6,15.2,45
"1132","1957244N15210",1957,"September",1957-09-02,"EP",-153.5,15.3,45
"1133","1957244N15210",1957,"September",1957-09-02,"EP",-154.7,15.7,45
"1134","1957244N15210",1957,"September",1957-09-02,"EP",-155.8,16.2,45
"1135","1957244N15210",1957,"September",1957-09-02,"EP",-157,17,45
"1136","1957244N15210",1957,"September",1957-09-03,"EP",-157.8,17.7,45
"1137","1957244N15210",1957,"September",1957-09-03,"EP",-159.1,18.7,45
"1138","1957244N15210",1957,"September",1957-09-03,"EP",-160.3,19.7,45
"1139","1957244N15210",1957,"September",1957-09-03,"EP",-161.7,20.7,45
"1140","1957244N15210",1957,"September",1957-09-04,"EP",-162.9,21.7,75
"1141","1957244N15210",1957,"September",1957-09-04,"EP",-164.4,22,75
"1142","1957244N15210",1957,"September",1957-09-04,"EP",-165.4,23,75
"1143","1957244N15210",1957,"September",1957-09-04,"EP",-166.5,23.8,75
"1144","1957244N15210",1957,"September",1957-09-05,"EP",-167.8,24.4,75
"1145","1957244N15210",1957,"September",1957-09-05,"EP",-168.5,24.6,75
"1146","1957244N15210",1957,"September",1957-09-05,"EP",-169.6,25,75
"1147","1957244N15210",1957,"September",1957-09-05,"EP",-170.5,25,75
"1148","1957244N15210",1957,"September",1957-09-06,"EP",-171.5,25,75
"1149","1957244N15210",1957,"September",1957-09-06,"EP",-172.2,25.1,75
"1150","1957244N15210",1957,"September",1957-09-06,"EP",-172.7,25.3,75
"1151","1957244N15210",1957,"September",1957-09-06,"EP",-173.3,25.4,75
"1152","1957244N15210",1957,"September",1957-09-07,"EP",-173.9,25.3,75
"1153","1957244N15210",1957,"September",1957-09-07,"EP",-174.5,25,75
"1154","1957244N15210",1957,"September",1957-09-07,"EP",-174.9,24.7,75
"1155","1957244N15210",1957,"September",1957-09-07,"EP",-175.4,24.3,75
"1156","1957244N15210",1957,"September",1957-09-08,"EP",-175.8,23.7,75
"1157","1957244N15210",1957,"September",1957-09-08,"EP",-176.2,23,75
"1158","1957244N15210",1957,"September",1957-09-08,"EP",-176.7,22.3,75
"1159","1957244N15210",1957,"September",1957-09-08,"EP",-177.5,21.6,75
"1160","1957244N15210",1957,"September",1957-09-09,"EP",-178.3,21,75
"1161","1957244N15210",1957,"September",1957-09-09,"EP",-179.2,20.5,75
"1162","1957244N15210",1957,"September",1957-09-09,"WP",179.5,19.7,0
"1163","1957244N15210",1957,"September",1957-09-10,"WP",178.3,19.4,0
"1164","1957244N15210",1957,"September",1957-09-10,"WP",177.3,19.8,0
"1165","1957244N15210",1957,"September",1957-09-10,"WP",176.1,20.1,0
"1166","1957244N15210",1957,"September",1957-09-10,"WP",174.8,20.5,0
"1167","1957244N15210",1957,"September",1957-09-11,"WP",173.5,20.9,0
"1168","1957244N15210",1957,"September",1957-09-11,"WP",172.1,21.4,0
"1169","1957244N15210",1957,"September",1957-09-11,"WP",170.9,21.9,0
"1170","1957244N15210",1957,"September",1957-09-11,"WP",169.6,22.5,0
"1171","1957244N15210",1957,"September",1957-09-12,"WP",168.1,23.2,0
"1172","1957244N15210",1957,"September",1957-09-12,"WP",166.7,23.9,0
"1173","1957244N15210",1957,"September",1957-09-12,"WP",165.1,24.5,0
"1174","1957244N15210",1957,"September",1957-09-12,"WP",163.7,25,0

Edit: The boundaries on this map are what I want:

Yet, this is what happens:

Edit #2: And while I'm at it, how would I change the blue lines into red?

Upvotes: 0

Views: 228

Answers (1)

hrbrmstr
hrbrmstr

Reputation: 78792

Having properly named (and potentially more) data would be helpful, but you can most certainly use xlim & ylim clip the map.

library(ggplot2)

map.dat <- map_data(map(database= "world", ylim=c(15,90), xlim=c(-180,-24), fill = TRUE))

basindf <- read.csv("~/Desktop/basindf.csv", stringsAs=FALSE)

gg <- ggplot()
# better way of making a map
gg <- gg + geom_map(data=map.dat, map=map.dat,
                    aes(x=long, y=lat, map_id=region), 
                    color="black", fill="#eeeeee", size=0.25)
# add your paths
gg <- gg + geom_path(data=basindf, 
                     aes(x=latitude, y=longitude, group=factor(id), 
                         color=wind))
gg <- gg + scale_color_distiller(palette="Reds")
# setup plot/map limits
gg <- gg + xlim(-170, -60)
gg <- gg + ylim(20, 60)
# pick at least some projection
gg <- gg + coord_map("albers",  lat0=39, lat1=45)
# map theme - library(ggthemes)
gg <- gg + ggthemes::theme_map()
gg <- gg + theme(legend.position = "none")
gg

enter image description here

Upvotes: 1

Related Questions