ggplot和grid.picture具有复杂形状的差异

时间:2019-02-24 02:29:05

标签: r ggplot2 fonts grimport

我希望获取单个字母的x / y坐标,并使用ggplot对其进行绘制。

我正在使用grImport::PostScriptTrace从Postscript文件中获取XML文件。从那里,我从Picture类的S4对象中提取x,y坐标。

grid.picture绘制字母效果很好:

enter image description here

使用我的方法获取x,y坐标并使用ggplot效果不佳:

enter image description here

删除数据框的最后一行会有所帮助:

字母“ g”的XML文件位于Dropbox上。

enter image description here

如何使用ggplot绘制没有错误行的字母?

这是代码。

# Difference between ggplot and grid.picture

library(grImport)
library(tidyverse)

letter_xml <- readRDS("letter_g")

# Plot letter with grid.picture
grid.picture(letter_xml)

####################################

# Extract coordinates from Picture object
x <- letter_xml@paths$text@letters[1]$path@x
y <- letter_xml@paths$text@letters[1]$path@y

one_letter <- tibble(
  x, 
  y,
  id = 1
)


ggplot(one_letter, aes(x = x, y = y)) + 
  geom_polygon()

# Remove last row 
one_letter <- one_letter[1:(nrow(one_letter) - 1),]

ggplot(one_letter, aes(x = x, y = y)) + 
  geom_polygon()

1 个答案:

答案 0 :(得分:2)

尝试一下:

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()

result

说明

当我检查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")

intermediate plot

如上图所示,线段2和3确实对应于线段1绘制的多边形中的孔。我不确定线段4(仅包含一个点)的情况,但看起来像反正不会出现在所需的图片中。我们可以在管道操作中添加一个过滤器,仅保留至少3个点的段(2个点或更少的点不能形成多边形)。

geom_polygon doesn't handle polygons with holes well,但是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)))