fix_shape: Fixes bad coordinates i z

Description Usage Arguments Value Examples

View source: R/extra_functions.R

Description

fix_shape takes a set of points (x and y coordinates) and fixes them so they are ready for hexamap.

Usage

1

Arguments

z

A data frame containing x, y and id.

Value

The function returns a data frame with coordinates

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Load libraries
library(hexamapmaker)
library(ggplot2)
library(tibble)
library(ggthemes)

# Points on a "normal" grid.
my_points <- tibble::tibble(
  x = c(1, 2, 1, 2, 1, 2, 4, 4),
  y = c(1, 1, 2, 2, 3, 3, 1, 2),
  id = c("test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8")
)

# Plot the points
ggplot(my_points, aes(x = x, y = y, group = id)) +
  geom_point() +
  coord_fixed(ratio = 1) +
  theme_map()

# Turn points into hexagons
hexa_points <- make_polygons(my_points)

# Plot the new hexagons
ggplot(hexa_points, aes(x, y, group = id)) +
  geom_polygon(colour = "black", fill = NA) +
  coord_fixed(ratio = 1) +
  theme_map()

# Oh no! It is way off - lets fix it.
my_points <- fix_shape(my_points)

# Plot points
ggplot(my_points, aes(x = x, y = y, group = id)) +
  geom_point() +
  coord_fixed(ratio = 1) +
  theme_map()

# Turn points into hexagons
hexa_points <- make_polygons(my_points)

ggplot(hexa_points, aes(x, y, group = id)) +
  geom_polygon(colour = "black", fill = NA) +
  coord_fixed(ratio = 1) +
  theme_map()

# Add color by using the fill argument in ggplot.
# Remember to remove it from the geom_polygon then
(p <- ggplot(hexa_points, aes(x, y, group = id, fill = id)) +
    geom_polygon(colour = "black", show.legend = FALSE) +
    coord_fixed(ratio = 1) +
    theme_map())

# Label hexagons
add_hexalabel(hexa_points, p)

56north/hexamapmaker documentation built on Oct. 9, 2017, 8:56 a.m.