0

I need to make map with points with labels. I try to make put points on the map withot labels.

library(maps)
library(ggplot2)
world_map <- map_data("world")



Latitude  <-  c(-40.68222046,   82.49914551,    -7.96999979,    23.26666641,    38.7700004578,  -41.4081916809, 32.2700004578,  71.3230133057,  55.2000007629,  1.7000000477,   -46.4333381653, -27.1666660309, 13.4300003052,  -75.571509, 46.9500007629,  28.3090000153,  25.6666660309,  19.5200004578,  45.9300003052,  53.3258323669,  28.2199993134,  19.5362300873,  -23.5699996948, 40.0499992371,  -64.7743301392, 13.1700000763,  -4.6700000763,  52.7200012207,  -14.2474746704, -89.9969482422, 72.5800018311,  -69.005,    36.7299995422,  39.9000015259,  44.4500007629,  31.1299991608,  36.2874984741,  78.906688,  48.8166656494,  44.1666679382,  49.7666664124,  54.1500015259,  53.3333320618,  46.9666671753,  35.5182,    45.9353408813,  47.9000015259,  69.1999969482,  -37.7983016968, 36.5383338928,  -19.2773323059, -34.3534812927, -66.2833023071, 36, 49.8400001526,  33.2799987793,  42.6166648865,  -62.2382011414, -67.6047210693, 24.2883,    -54.498500824,  39.0319,    43.932638,  24.4667,    -0.0621999986,  -0.2019443959,  -54.8484649658, 41.0541000366,  -12.76666641,   71.596, 82.49914551,    15.08,  -66.2833,   49.3833,    -67.6167,   -54.4833,   60.0889,    38.766, 44.1776,    47.8011)
Longitude <- c(144.6883392, -62.34152603,   -14.39999962,   5.633333206,    -27.3799991608, 174.870803833,  -64.8799972534, -156.6114654541,    -162.7166595459,    -157.1699981689,    51.8335800171,  -109.4167022705,    144.7799987793, -25.50386,  16.6499996185,  -16.4993991852, -80.1999969482, -154.8200073242,    -90.2699966431, -9.8994445801,  -177.3699951172,    -155.5761566162,    15.029999733,   -105.5899963379,    -64.0544204712, -59.4300003052, 55.1699981689,  174.1000061035, -170.5645141602,    -24.7999992371, -38.4799995422, 39.58,  126.1299972534, -113.7200012207,    111.0999984741, 34.8800010681,  100.8963012695, 11.889342,  13.2166662216,  10.6833333969,  7.0500001907,   22.0666675568,  6.2666668892,   19.5833339691,  12.6305,    7.7073101997,   7.9166665077,   35.0999984741,  77.5378036499,  126.3300018311, 147.0584411621, 18.4896831512,  110.5167007446, 139.1999969482, -81.5166702271, 126.1699981689, 76.9833297729,  -58.6660003662, 62.8705558777,  153.9833,   158.9385070801, 141.8222,   -60.008608, 123.0106,   37.2971992493,  100.3180541992, -68.3106918335, -124.1510009766,    -38.16666794,   128.889,    -62.34152603,   73.83,  110.5167,   -126.5333,  62.8667,    158.9667,   -1.2553,    -27.375,    28.6647,    11.0245)
Type      <- c(2,   1,  13, 1,  1,  4,  1,  1,  1,  3,  2,  8,  1,  2,  1,  1,  1,  1,  1,  1,  1,  1,  4,  1,  1,  1,  10, 1,  3,  2,  1,  2,  1,  1,  1,  1,  1,  1,  1,  1,  2,  11, 3,  1,  1,  1,  1,  1,  15, 1,  7,  6,  2,  1,  1,  1,  5,  9,  2,  1,  2,  1,  1,  1,  3,  4,  1,  1,  4,  1,  1,  12, 2,  1,  2,  2,  1,  1,  14, 1)

data_stations = as.data.frame(cbind(Latitude, Longitude, Type))


p <- ggplot(data = data_stations, aes(x = data_stations$Longitude, y = data_stations$Latitude,color = as.factor(data_stations$Type)))+ coord_fixed() + xlab("") + ylab("")
base_world_messy <- p + geom_polygon(data=world_map, aes(x=long, y=lat, group=group), colour="light green", fill="light green")
cleanup <- theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_rect(fill = 'white', colour = 'white'), axis.line = element_line(colour = "white"), axis.ticks=element_blank(), axis.text.x=element_blank(),axis.text.y=element_blank())
base_world <- base_world_messy + cleanup
final_map=base_world +geom_point(shape = data_stations$Type,colour = as.factor(data_stations$Type),size=3) +scale_shape_identity()

iT was good:

final_map

But when I tried to add labels on it

Latitude  <-  c(-40.68222046,   82.49914551,    -7.96999979,    23.26666641,    38.7700004578,  -41.4081916809, 32.2700004578,  71.3230133057,  55.2000007629,  1.7000000477,   -46.4333381653, -27.1666660309, 13.4300003052,  -75.571509, 46.9500007629,  28.3090000153,  25.6666660309,  19.5200004578,  45.9300003052,  53.3258323669,  28.2199993134,  19.5362300873,  -23.5699996948, 40.0499992371,  -64.7743301392, 13.1700000763,  -4.6700000763,  52.7200012207,  -14.2474746704, -89.9969482422, 72.5800018311,  -69.005,    36.7299995422,  39.9000015259,  44.4500007629,  31.1299991608,  36.2874984741,  78.906688,  48.8166656494,  44.1666679382,  49.7666664124,  54.1500015259,  53.3333320618,  46.9666671753,  35.5182,    45.9353408813,  47.9000015259,  69.1999969482,  -37.7983016968, 36.5383338928,  -19.2773323059, -34.3534812927, -66.2833023071, 36, 49.8400001526,  33.2799987793,  42.6166648865,  -62.2382011414, -67.6047210693, 24.2883,    -54.498500824,  39.0319,    43.932638,  24.4667,    -0.0621999986,  -0.2019443959,  -54.8484649658, 41.0541000366,  -12.76666641,   71.596, 82.49914551,    15.08,  -66.2833,   49.3833,    -67.6167,   -54.4833,   60.0889,    38.766, 44.1776,    47.8011)
Longitude <- c(144.6883392, -62.34152603,   -14.39999962,   5.633333206,    -27.3799991608, 174.870803833,  -64.8799972534, -156.6114654541,    -162.7166595459,    -157.1699981689,    51.8335800171,  -109.4167022705,    144.7799987793, -25.50386,  16.6499996185,  -16.4993991852, -80.1999969482, -154.8200073242,    -90.2699966431, -9.8994445801,  -177.3699951172,    -155.5761566162,    15.029999733,   -105.5899963379,    -64.0544204712, -59.4300003052, 55.1699981689,  174.1000061035, -170.5645141602,    -24.7999992371, -38.4799995422, 39.58,  126.1299972534, -113.7200012207,    111.0999984741, 34.8800010681,  100.8963012695, 11.889342,  13.2166662216,  10.6833333969,  7.0500001907,   22.0666675568,  6.2666668892,   19.5833339691,  12.6305,    7.7073101997,   7.9166665077,   35.0999984741,  77.5378036499,  126.3300018311, 147.0584411621, 18.4896831512,  110.5167007446, 139.1999969482, -81.5166702271, 126.1699981689, 76.9833297729,  -58.6660003662, 62.8705558777,  153.9833,   158.9385070801, 141.8222,   -60.008608, 123.0106,   37.2971992493,  100.3180541992, -68.3106918335, -124.1510009766,    -38.16666794,   128.889,    -62.34152603,   73.83,  110.5167,   -126.5333,  62.8667,    158.9667,   -1.2553,    -27.375,    28.6647,    11.0245)
Type      <- c(2,   1,  13, 1,  1,  4,  1,  1,  1,  3,  2,  8,  1,  2,  1,  1,  1,  1,  1,  1,  1,  1,  4,  1,  1,  1,  10, 1,  3,  2,  1,  2,  1,  1,  1,  1,  1,  1,  1,  1,  2,  11, 3,  1,  1,  1,  1,  1,  15, 1,  7,  6,  2,  1,  1,  1,  5,  9,  2,  1,  2,  1,  1,  1,  3,  4,  1,  1,  4,  1,  1,  12, 2,  1,  2,  2,  1,  1,  14, 1)

data_stations = as.data.frame(cbind(Latitude, Longitude, Type  ))


p <- ggplot(data = data_stations, aes(x = data_stations$Longitude, y = data_stations$Latitude,color = as.factor(data_stations$Type),label = data_stations$Type ))+ coord_fixed() + xlab("") + ylab("")
base_world_messy <- p + geom_polygon(data=world_map, aes(x=long, y=lat, group=group), colour="light green", fill="light green")
cleanup <- theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_rect(fill = 'white', colour = 'white'), axis.line = element_line(colour = "white"), axis.ticks=element_blank(), axis.text.x=element_blank(),axis.text.y=element_blank())
base_world <- base_world_messy + cleanup
final_map=base_world +geom_point(shape = data_stations$Type,colour = as.factor(data_stations$Type),size=3) +scale_shape_identity()+geom_text()

I have failed

Error: Aesthetics must be either length 1 or the same as the data (99338): label

How should I solve this problem?

  • Gonna guess it's [the `$` inside `aes`](https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio) – camille Jul 26 '19 at 18:13
  • I tried to delete - the same problem – Владимир Кузовкин Jul 26 '19 at 18:19
  • If you run your code line by line (after deleting all the `$` inside `aes` calls) you'll see that the error comes on the line where you add the `geom_polygon`; I'm guessing because you have variables included in your first `aes` that aren't in the second data frame. The `aes` in `ggplot` carries down to any layers added, unless you set `inherit.aes = FALSE` in a layer – camille Jul 26 '19 at 18:30
  • I understood that solving this problem is to set up in geom_polygon() and in geom_point() phrase "inherit.aes=False",didn't I? – Владимир Кузовкин Jul 26 '19 at 19:09

0 Answers0