ggmap, using coord_cartesian pushes all points to

2019-07-18 20:46发布

问题:

As the title says when I add coord_cartesian to my ggmap it moves all of my points up. Here's some data.

pricedata<-structure(list(nodename = c("CIN.WABRIVR.2", "CIN.WHEATCTG1", 
                                       "CONS.ADA", "CONS.ALCONA", "CONS.CADILAC", "CONS.CROTON", "CONS.GAYLORD1", 
                                       "CONS.GRATIOT1", "CONS.GRAYLGY2", "CONS.GRAYLNG", "CONS.HARDY", 
                                       "CONS.HILLMAN", "CONS.HODENPYL", "CONS.HOLL", "CONS.KALK", "CONS.KARN1", 
                                       "CONS.KENCNTY1", "CONS.LANS", "CONS.LUDINGTN1", "CONS.MIPOWER1", 
                                       "CONS.RENAIGEN1", "CONS.STRAITS", "CONS.TUSCOLA1", "CONS.VKLINCOLN", 
                                       "CONS.VKMCBAIN1", "CONS.ZEELAND1A"), 
                          lat = c(39.922328, 39.53, 42.962672, 44.561961, 44.26169, 43.437322, 45.0306, 43.433889, 
                                  43.408056, 44.604921, 43.486618, 45.0688, 44.36286, 42.7925, 44.6889, 43.644996, 
                                  42.949575, 42.719722, 43.8942, 43.9375, 43.1864, 45.766859, 43.525278, 44.68, 44.204, 42.8067), 
                          lon = c(-87.446358, -87.4247, -85.494071, -83.804505, -85.435224, -85.664462, -84.7039, -84.4975, -84.462222, 
                                  -84.690578, -85.629866, -83.8932, -85.819968, -86.092222, -85.2019, -83.840074, -85.693209, -84.551667, 
                                  -86.4447, -86.425, -84.8429, -84.756601, -83.65, -83.4167, -85.2206, -86.0558), 
                          price = c(30.3, 32.08, 36.71, 35.78, 36.12, 36.33, 35.58, 35.16, 36.12, 36.12, 35.9, 35.8, 36.05, 36.38, 
                                    35.98, 23.18, 36.06, 34.55, 34.87, 34.6, 34.6, 38.49, 34.23, 35.64, 35.43, 36.33), 
                          pricecut = structure(c(7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
                                                 8L, 8L, 5L, 8L, 8L, 8L, 8L, 8L, 9L, 8L, 8L, 8L, 8L), 
                         .Label = c("(-10,0]", "(0,6]", "(6,14]", "(14,20]", "(20,26]", "(26,30]", "(30,34]", 
                                    "(34,38]", "(38,42]", "(42,46]", "(46,50]", "(50,56]", "(56,62]", "(62,68]", 
                                    "(68,76]", "(76,82]", "(82,90]", "(90,100]", "(100,115]", "(115,125]", "(125,150]", 
                                    "(150,200]", "(200,250]", "(250,300]", "(300,400]", "(400,500]", 
                                    "(500,600]", "(600,800]", "(800,1e+03]"), 
                         class = c("ordered", "factor"))), .Names = c("nodename", "lat", "lon", "price", "pricecut"), row.names = 75:100, class = "data.frame")

Here's my code plus the result

m<-get_map(location=c(lon=-89.6,lat=41.8),zoom=5)
base<-ggmap(m,extent='device') 
base+geom_point(aes(x=lon,y=lat, colour=pricecut), size=6, alpha=.7, data=pricedata)

That is the result I expect

However, when I add coord_cartesian things get strange

base+geom_point(aes(x=lon,y=lat, colour=pricecut), size=6, alpha=.7, data=pricedata)+coord_cartesian(xlim=c(-95,-80), ylim=c(38,50))

回答1:

Instead of using coord_cartesian you can probably better set the limits with scale_x_continuous and scale_y_continuous as follows:

ggmap(m) +
  geom_point(aes(x=lon,y=lat, colour=pricecut), size=6, alpha=.7, data=pricedata) +
  scale_x_continuous(limits = c(-95, -80), expand = c(0, 0)) +
  scale_y_continuous(limits = c(38, 50), expand = c(0, 0))

which gives the following map:

Note: I omitted extent='device' from the ggmap call so you can see what the boundaries are in this plot.


As with regard to the effect of using coord_cartesian, it seems that coord_cartesian somehow messes with the ratios of the map. Let's start with just the map:

ggmap(m)

gives:

When you slice this map with scale_y_continuous:

ggmap(m) +
  geom_blank() +
  scale_y_continuous(limits = c(38, 50), expand = c(0, 0))

you get:

However when doing a similar slice with coord_cartesian:

ggmap(m) +
  geom_blank() +
  coord_cartesian(ylim=c(38,50))

you get:

As you can see, the map gets stretched horizontally while at the same time maintaining the same height. This causes the map to shift vertically. When using scale_y_continuous the map keeps the correct ratio. It's therefore not the points that get shifted upwards, but the map that gets shifted downwards.



标签: r ggplot2 ggmap