Попробуйте:
x <- letter_xml@paths$text@letters[1]$path@x
y <- letter_xml@paths$text@letters[1]$path@y
one_letter <- tibble(
x = x,
y = y,
x.n = names(x)
# id is not necessary here
)
library(ggpolypath)
one_letter %>%
mutate(is.move = x.n == "move") %>%
mutate(section.id = cumsum(is.move)) %>%
group_by(section.id) %>%
mutate(section.length = n()) %>%
ungroup() %>%
filter(section.length >= 3) %>%
ggplot(aes(x = x, y = y, group = section.id)) +
geom_polypath()
Объяснение :
Когда я проверяю letter_xml@paths$text@letters[1]$path
, Я заметил, что x
/ y
- идентичные именованные векторы вида c("move", "line", ..., "line", "move", "line", ..., "line", "move")
.
> all.equal(names(x), names(y))
[1] TRUE
> table(names(x))
line move
169 4
Учитывая форму буквы, с которой мы работаем, я подозревал, что каждый новый "move"
может указывать на начало нового сегмента.Например, первый сегмент соответствует контуру, второй сегмент соответствует отверстию и т. Д.
Я проверил эту теорию, построив последовательность положений (row.id
) и меняя цвет для каждого нового "move"
:
one_letter %>%
mutate(row.id = seq(1, n())) %>% # sequence of x/y coordinates
mutate(is.move = x.n == "move") %>% # TRUE for every new "move", FALSE o/w
mutate(section.id = cumsum(is.move)) %>% # increments by 1 for every new "move"
ggplot(aes(x = x, y = y, group = section.id,
fill = factor(section.id))) +
geom_label(aes(label = row.id)) +
scale_fill_brewer(palette = "Set1")
Как показано на диаграмме выше, отрезки 2 и 3 действительно соответствуют отверстиям в многоугольнике, нарисованном отрезком 1. I 'Я не уверен, что происходит с сегментом 4 (который содержит только одну точку), но кажется, что он все равно не отображается на желаемой картинке.Мы можем добавить фильтр к операциям с трубой, оставляя только сегменты с как минимум 3 точками (2 точки или меньше не могут образовывать многоугольник).
geom_polygon
не обрабатывает многоугольники с отверстиямихорошо , но пакет ggpolypath
(доступный на CRAN ) в значительной степени адаптирован для этого точного случая использования и прекрасно выполняет задание.
Данные :
> dput(letter_xml)
new("Picture", paths = list(text = new("PictureText", string = c(string = "g"),
w = 54.5977, h = 100, bbox = c(292.688, 8032.13, 345.328,
8110.3), angle = 0, letters = list(path = new("PictureChar",
char = c(char = "g"), x = c(move = 317.422, line = 315.605,
line = 310.16, line = 304.367, line = 300.527, line = 299.141,
line = 299.141, line = 299.141, line = 299.797, line = 301.297,
line = 301.719, line = 300.805, line = 298.199, line = 295.684,
line = 294.172, line = 293.672, line = 293.672, line = 293.672,
line = 294.172, line = 295.684, line = 298.199, line = 300.805,
line = 301.719, line = 300.684, line = 297.75, line = 294.93,
line = 293.246, line = 292.688, line = 292.688, line = 292.688,
line = 294.367, line = 299.203, line = 306.891, line = 314.566,
line = 317.125, line = 319.695, line = 327.41, line = 335.234,
line = 340.207, line = 341.953, line = 341.953, line = 341.953,
line = 340.152, line = 334.797, line = 325.941, line = 316.715,
line = 313.641, line = 312.145, line = 307.656, line = 303.695,
line = 301.5, line = 300.828, line = 300.828, line = 300.828,
line = 301.121, line = 301.906, line = 303.047, line = 304.066,
line = 304.406, line = 305.078, line = 306.82, line = 307.094,
line = 308.059, line = 312.82, line = 317.008, line = 318.406,
line = 320.199, line = 325.586, line = 331.316, line = 335.109,
line = 336.484, line = 336.484, line = 336.484, line = 336.016,
line = 334.609, line = 332.25, line = 329.828, line = 328.938,
line = 328.953, line = 329.332, line = 330.355, line = 332.008,
line = 333.723, line = 334.297, line = 334.863, line = 336.563,
line = 338.102, line = 338.375, line = 338.004, line = 336.723,
line = 336.188, line = 336.188, line = 336.188, line = 336.516,
line = 337.395, line = 338.664, line = 339.793, line = 340.172,
line = 340.664, line = 342.148, line = 343.793, line = 344.91,
line = 345.328, line = 345.328, line = 345.328, line = 344.5,
line = 342.234, line = 338.832, line = 335.664, line = 334.609,
line = 333.41, line = 329.813, line = 326.332, line = 324.152,
line = 323.328, line = 323.281, line = 322.734, line = 318.75,
line = 317.422, line = 317.422, move = 317.719, line = 318.82,
line = 322.137, line = 325.664, line = 327.996, line = 328.844,
line = 328.844, line = 328.844, line = 328.023, line = 325.723,
line = 322.172, line = 318.75, line = 317.609, line = 316.52,
line = 313.258, line = 309.871, line = 307.672, line = 306.891,
line = 306.891, line = 306.891, line = 307.727, line = 310.031,
line = 313.469, line = 316.656, line = 317.719, line = 317.719,
move = 317.813, line = 319.559, line = 324.809, line = 330.023,
line = 333.281, line = 334.406, line = 334.406, line = 334.406,
line = 333.215, line = 329.797, line = 324.387, line = 319.008,
line = 317.219, line = 315.516, line = 310.41, line = 305.215,
line = 301.898, line = 300.734, line = 300.734, line = 300.734,
line = 301.906, line = 305.289, line = 310.66, line = 316.023,
line = 317.813, line = 317.813, move = 344.598), y = c(move = 8101.36,
line = 8101.36, line = 8100.18, line = 8096.9, line = 8091.93,
line = 8087.22, line = 8085.66, line = 8084.56, line = 8081.29,
line = 8078.09, line = 8077.52, line = 8077.23, line = 8075.97,
line = 8073.87, line = 8071.21, line = 8068.79, line = 8067.98,
line = 8067.23, line = 8064.98, line = 8062.39, line = 8060.21,
line = 8058.79, line = 8058.44, line = 8058.05, line = 8056.45,
line = 8053.89, line = 8050.75, line = 8047.95, line = 8047.02,
line = 8045.46, line = 8040.79, line = 8036.11, line = 8033.15,
line = 8032.13, line = 8032.13, line = 8032.13, line = 8033.22,
line = 8036.35, line = 8041.29, line = 8046.18, line = 8047.81,
line = 8049.44, line = 8054.32, line = 8059.05, line = 8061.93,
line = 8062.91, line = 8062.91, line = 8062.91, line = 8063.15,
line = 8063.99, line = 8065.55, line = 8067.38, line = 8067.98,
line = 8068.39, line = 8069.6, line = 8070.93, line = 8071.82,
line = 8072.16, line = 8072.16, line = 8072.16, line = 8071.58,
line = 8071.45, line = 8071.03, line = 8069.53, line = 8068.88,
line = 8068.88, line = 8068.88, line = 8070.09, line = 8073.45,
line = 8078.52, line = 8083.29, line = 8084.88, line = 8085.88,
line = 8088.91, line = 8092.53, line = 8095.71, line = 8097.88,
line = 8098.47, line = 8099.19, line = 8101.34, line = 8103.39,
line = 8104.62, line = 8105.03, line = 8105.03, line = 8105.03,
line = 8104.52, line = 8103.37, line = 8103.05, line = 8102.77,
line = 8101.41, line = 8100.18, line = 8099.77, line = 8099.41,
line = 8098.35, line = 8097.18, line = 8096.39, line = 8096.09,
line = 8096.09, line = 8096.09, line = 8096.54, line = 8097.78,
line = 8099.61, line = 8101.3, line = 8101.86, line = 8102.7,
line = 8105.23, line = 8107.9, line = 8109.66, line = 8110.3,
line = 8110.3, line = 8110.3, line = 8109.68, line = 8107.85,
line = 8104.8, line = 8101.63, line = 8100.56, line = 8100.69,
line = 8101.36, line = 8101.36, line = 8101.36, move = 8094.8,
line = 8094.8, line = 8094.05, line = 8092, line = 8088.89,
line = 8085.95, line = 8084.97, line = 8084.01, line = 8081.13,
line = 8078.12, line = 8076.14, line = 8075.44, line = 8075.44,
line = 8075.44, line = 8076.13, line = 8078.1, line = 8081.17,
line = 8084.17, line = 8085.17, line = 8086.12, line = 8088.97,
line = 8092.03, line = 8094.06, line = 8094.8, line = 8094.8,
line = 8094.8, move = 8056.27, line = 8056.27, line = 8055.66,
line = 8053.93, line = 8051.15, line = 8048.35, line = 8047.42,
line = 8046.52, line = 8043.83, line = 8041.07, line = 8039.3,
line = 8038.67, line = 8038.67, line = 8038.67, line = 8039.27,
line = 8041, line = 8043.75, line = 8046.5, line = 8047.42,
line = 8048.34, line = 8051.1, line = 8053.89, line = 8055.65,
line = 8056.27, line = 8056.27, line = 8056.27, move = 8050
), rgb = "#000000", lty = numeric(0), lwd = 10, lineend = 1,
linejoin = 1, linemitre = 10)), x = 290, y = 8050, rgb = "#000000",
lty = numeric(0), lwd = 10, lineend = numeric(0), linejoin = numeric(0),
linemitre = numeric(0))), summary = new("PictureSummary",
numPaths = 1, xscale = c(xmin = 290, xmax = 345.328), yscale = c(ymin = 8032.13,
ymax = 8110.3)))