user2530062
user2530062

Reputation: 517

In ggmap/ggplot, how to set size in absolute not relative values

In the following simplified example, the size variable controls the relative size of lines to be plotted on the map with geom_path.

The problem is that apparently the size is not in millimeters (as in documentation for ggplot), but for any two or more values in size variable, the lines with two extremes (min and max) are plotted with minimal and greatest width available and all the other lines are on discrete scale somewhere in the middle.

The greatest width is just too wide and I wanted to make it thinner. But since the size appear to be relative to min/max and is not an absolute (millimeter/pixel whatever) value, I am unable to control the actual size.

Please change the somevalue here and see that nothing changes between plots.

library(ggmap)

base_layer <- get_googlemap(center = c(lon = 28.5, lat = 37) , zoom = 3 , maptype="roadmap" , size = c(640,640) , scale = 2 , color = "bw")

somevalue <- 3

df <- data.frame(
  group = c("g1","g1","g2","g2"), 
  size = c(1,1,somevalue,somevalue), 
  color = c("blue","blue", "red", "red"),
  lon = c(10,20,10,-10),
  lat = c(52,60,52,60)
)

ggmap(base_layer) + 
  geom_path(data = df, aes(x = lon, y = lat, alpha = 0.6, group = group, color = color, size = size))

example

Upvotes: 3

Views: 1395

Answers (1)

Axeman
Axeman

Reputation: 35397

You can control the size in two ways:

1) You can disable the automatic rescaling by using scale_size_identity.

2) You can manually set the size range by using scale_size_continuous.

Option 1) will scale with your data values, option 2) will not.

Both of these give the same plot:

ggmap(base_layer) + 
  geom_path(data = df, aes(x = lon, y = lat, group = group, color = color, size = size),
            alpha = 0.6) +
  scale_size_identity()

ggmap(base_layer) + 
  geom_path(data = df, aes(x = lon, y = lat, group = group, color = color, size = size),
            alpha = 0.6) +
  scale_size_continuous(range = c(1, 3))

enter image description here

p.s. Note that one needs to place the alpha value outside the aes, when setting it to a constant.

Upvotes: 3

Related Questions