具有复杂形状的 ggplot 和 grid.picture 之间的区别

2023-12-14

我希望获得单个字母的 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()

尝试这个:

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 不能很好地处理带孔的多边形,但是ggpolypath包(可在CRAN)几乎是针对这个确切的用例量身定制的,并且可以很好地完成工作。

Data:

> 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)))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

具有复杂形状的 ggplot 和 grid.picture 之间的区别 的相关文章

  • 将summary()写入as.data.frame以在ggplot / R中使用

    请查找 af 数据样本t below 我正在使用以下方法进行竞争风险分析etmCIF来自etm package 产生以下结果 这很好 但需要更好的图形 曾经有一个ggtrans etm函数将数据导入ggplot 然而 这个功能显然被删除了
  • R read_excel:libxls 错误:无法解析文件

    我试图使用 readxl read excel 将 xls 文件读入 R 但它给出了以下错误 Error filepath data xls libxls error Unable to parse file 还尝试了 readxl exc
  • 使用 ggplot2 修改点子集的形状

    我正在尝试绘制一个沿大量维度变化的大型散点图 这是我的起始情节 p lt ggplot mtcars aes wt mpg shape cyl colour gear size carb geom point 使用mtcars数据集 我只是
  • R-了解 akima::interp 结果中的 NA 值

    我有以下数据框 ref dat k Intensity Slope 1 0 021467214 33 16 2 0 012444759 33 8 3 0 006079156 33 4 4 0 003792025 33 2 5 0 02276
  • 使用 Swift 将自定义字体添加到 macOS 应用程序

    我遵循了一堆教程 但它不起作用 我只是想向 macOS 应用程序添加自定义字体 我基本上尝试过 添加了 ttf字体文件到我的项目 目标会员资格已设置 我还确保使用复制文件Copy Files within Bundle Phases 编译后
  • 限制数据框中所有单元格的字符串长度?

    您好 有没有一种方法可以限制 data frame 中所有列的字符串文本大小 而不必循环遍历每一列并一次使用 str trunc 之类的东西 例如下面的数据框 我可以将所有文本大小限制为仅 5 个字符 而不必一次只执行一列吗 如果有 50
  • R:将 readRDS 应用于 .Rds 文件名的列表对象

    我有几个包含数据帧对象的 Rds 文件 我想对每个文件应用一个函数并将数据帧绑定到单个数据帧中 但是 当我尝试从文件名列表中读取多个 Rds 文件时 我收到错误 FUN X i 中的错误 从连接读取时出错 readRDS 不适用于列表吗 R
  • 在 R 中绘制对数正态概率密度

    我正在尝试在 R 中生成对数正态概率密度图 其中包含 3 个不同的均值对数和标准差对数 我尝试了以下方法 但我的图表太丑了 看起来一点也不好看 x lt seq 0 10 length 100 a lt dlnorm x meanlog 0
  • 从 R 环境中删除对象

    我正在阅读 Hadley 的 Advanced R 在第 8 章中 他说我们可以使用以下方法从环境中删除对象 rm 但是 移除该物体后我仍然可以看到该物体 这是我的代码 e lt new env e a lt 1 e b lt 2 e a
  • R.matlab/readMat:readTag(this) 中出错

    我正在尝试使用 R matlab 将 matlab 文件读入 R 但遇到此错误 require R matlab r lt readMat file mat verbose T Trying to read MAT v5 file stre
  • 在另一个 Rmd 中运行选定的块

    我已经在源 Rmd 文件中运行了分析 并且希望仅使用few来自源的块 我已经看到了一些关于从源 Rmd 中提取所有块的答案来自另一个 Rmd 中的 Rmd 文件的源代码 https stackoverflow com questions 4
  • R 编程:如何计算数据框中两个单元格之间的差异并将它们保存在新列中

    尝试学习 R 并陷入自相关示例中 我想将 x 的差异与 y 的差异进行回归 我在数据框中有 x 和 y 并且希望将 x2 x1 的差值保存在新列 例如 dx 中 我不知道该怎么做 我拥有的 data1 x y 5 3 8 9 3 1 1 5
  • 如何更改 Shiny 中 navbarPage 折叠的断点

    我想用shiny navbarPage collapsible TRUE 当在小屏幕上查看我的 Shiny 应用程序时 将导航元素折叠到菜单中 默认情况下 当浏览器宽度小于 940 像素时会触发折叠 有什么方法可以改变这一点 以便在稍大的浏
  • R 更改小数位且不四舍五入

    gt signif 1 89 digits 2 1 1 9 我想要1 8 这有点笨拙 但它会起作用并保持所有数字 x lt 1 829380 trunc dec lt function x n floor x 10 n 10 n Resul
  • fread 将空导入为 NA

    我正在尝试导入带有空白的 csv 读取为 不幸的是他们都读作 NA now 为了更好地演示问题 我还展示了如何NA NA and 都映射到同一事物 除了最底部的示例 这将妨碍简单的解决方法dt is na dt lt gt write cs
  • 如何将此“for”循环转换为向量解

    这个问题与 将嵌入其他文本的长州名称转换为两个字母的州缩写 https stackoverflow com questions 25582518 convert long state names embedded with other te
  • 为什么这些数字不相等?

    下面的代码显然是错误的 有什么问题 i lt 0 1 i lt i 0 05 i 1 0 15 if i 0 15 cat i equals 0 15 else cat i does not equal 0 15 i does not eq
  • 使用predictNLS围绕R中的拟合值创建置信区间?

    我想使用 R 中 propogate 包中的 PredictNLS 围绕一大组拟合值构建置信区间 作为示例 我将使用它们在函数描述中引用的数据集 https rdrr io github anspiess propagate man pre
  • R 中的 Websocket

    我设法在 R 中建立到 Mtgox websocket 的连接 规格如下 url https socketio mtgox com mtgox Currency USD https socketio mtgox com mtgox Curr
  • 如何将 ggrough 图表另存为 .png

    说我正在使用R包裹ggrough https xvrdm github io ggrough https xvrdm github io ggrough 我有这个代码 取自该网页 library ggplot2 library ggroug

随机推荐