gpt4 book ai didi

r - 更改ggplot中的线宽,而不是大小

转载 作者:行者123 更新时间:2023-12-04 01:12:14 24 4
gpt4 key购买 nike

我看到几篇关于 changing line width 的帖子在ggplot中。答案虽然对 OP 来说信息丰富且有效,但会改变行的大小。也就是说,ggplot 似乎将线条视为一系列单位,而 size 增加了每个单位的长度和宽度,使其他调整更粗略。

我希望有一种方法可以使线条更宽(更厚),而不会影响长度,进而影响破折号的比例。

我从 https://cran.r-project.org/web/packages/ggplot2/vignettes/ggplot2-specs.html 借了一些代码来说明我的意思:

library(ggplot2)

#A simple plot with manually set dashes.
#The first numeral is units of dash length, the second units in the gap in hexadecimal.
lty <- c("11", "18", "1f", "81", "88", "8f", "f1", "f8", "ff")
linetypes <- data.frame(y = seq_along(lty), lty = lty)

ggplot(linetypes, aes(0, y)) +
geom_segment(aes(xend = 5, yend = y, linetype = lty)) +
scale_linetype_identity() +
geom_text(aes(label = lty), hjust = 0, nudge_y = 0.2) +
scale_x_continuous(NULL, breaks = NULL) +
scale_y_reverse(NULL, breaks = NULL)

Default size dashed lines.
#Altering the size changes the line width AND dash spacing.

ggplot(linetypes, aes(0, y)) +
geom_segment(aes(xend = 5, yend = y, linetype = lty),
size = 3) +
scale_linetype_identity() +
geom_text(aes(label = lty), hjust = 0, nudge_y = 0.3) +
scale_x_continuous(NULL, breaks = NULL) +
scale_y_reverse(NULL, breaks = NULL)

Size 3 dashed lines.

本质上,我想要的是粗线,相对较细和微调的间隙。我认为这是以另一种方式提出的同一个问题:有没有办法让所有的线条,比如说,“虚线”在宽度上有所不同,但在破折号的相对位置上没有?像这样(我完全伪造):

Lines of different width, but the same size.

最佳答案

来自 ?par “线型规范”的帮助文件(为强调而添加了粗体):

Line types can either be specified by giving an index into a small built-in table of line types (1 = solid, 2 = dashed, etc, see lty above) or directly as the lengths of on/off stretches of line. This is done with a string of an even number (up to eight) of characters, namely non-zero (hexadecimal) digits which give the lengths in consecutive positions in the string. For example, the string "33" specifies three units on followed by three off and "3313" specifies three units on followed by three off followed by one on and finally three off. The ‘units’ here are (on most devices) proportional to lwd, and with lwd = 1 are in pixels or points or 1/96 inch.



这表明,如果我们指定适当变化的线型和线宽(例如,较粗的线是较细的线的两倍宽,开关延伸的长度是一半),我们可以在不同的情况下实现看似相同的虚线长度的预期效果线宽。

注意:线型规范中允许的字符是 c(1:9, "A":"F")) ,这意味着最短的可能拉伸(stretch)长度是 1 个单位,而最长的是 15 个单位。这限制了可以创建的不同行的数量。

它与基本 R 绘图功能按预期工作:
plot.new()
plot.window(xlim=c(0, 5), ylim=c(1, 3))
abline(h = 3, lty = "22", lwd = 8)
abline(h = 2, lty = "44", lwd = 4)
abline(h = 1, lty = "88", lwd = 2)
axis(2, at = 3:1, labels=c("22", "44", "88"), tick = FALSE, las = 1)

base R

另一方面,对于 ggplot2,据我所知, 图形文件设备的选择很重要 .破折号整齐地对齐基于矢量的设备,但不一定适用于基于位图的设备:
p <- ggplot(data.frame(y = seq(1, 3),
lty = c("11", "22", "44"),
lwd = c(8, 4, 2)),
aes(0, y)) +
geom_segment(aes(xend = 5, yend = y,
linetype = lty,
lwd = lwd)) +
geom_text(aes(label = lty), hjust = 0, nudge_x = -0.3) +

scale_linetype_identity() +
scale_size_identity() +
scale_x_continuous(NULL, breaks = NULL) +
scale_y_reverse(NULL, breaks = NULL, expand = c(0.5, 0))

# bitmap devices
ggsave("test.bmp", p + ggtitle(".bmp"))
ggsave("test.jpg", p + ggtitle(".jpg"))
ggsave("test.png", p + ggtitle(".png"))
ggsave("test.tiff", p + ggtitle(".tiff"))
ggsave("test.wmf", p + ggtitle(".wmf"))

# vector devices
ggsave("test.eps", p + ggtitle(".eps"))
ggsave("test.pdf", p + ggtitle(".pdf"))
ggsave("test.svg", p + ggtitle(".svg"))

output from different devices

我还没有找到任何直接在 ggplot2 中解决这个问题的东西。尽管如此,如果您需要其中一种位图格式,但相同间隔的破折号是重中之重,您可以先将绘图输出到其中一个基于矢量的设备,然后再从一种文件格式转换为另一种文件格式。

关于r - 更改ggplot中的线宽,而不是大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52885265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com