在ggplot中更改线宽,而不是大小 [英] Change line width in ggplot, not size

查看:483
本文介绍了在ggplot中更改线宽,而不是大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ggplot中看到有关更改线宽的几篇文章.答案虽然对OP有用且有效,但可以更改 size 行.也就是说,ggplot似乎将线视为一系列单位,并且大小会增加每个单位的长度和宽度,从而使其他调整变得更粗糙.

I see several posts about changing line width in ggplot. The answers, while informative and effective for the OP, change the line size. That is, ggplot seems to treat lines as a series of units, and size increases both the length and width of each unit, making other adjustments coarser.

我希望有一种方法可以使线条更宽(更厚),同时又不影响长度和虚线的比例.

I'm hoping there is a way to make lines wider (thicker) without also affecting length and, in turn, the scale of dashes.

我从 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)

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

基本上,我想要的是带有较细和精细调整间隙的粗线.我认为这是用另一种方式问的同一问题:是否有一种方法可以使全部为虚线"的线的宽度不同,但虚线的相对位置不同?像这样(我完全伪造了):

Essentially what I want is thick lines with relatively thin and finely adjusted gaps. I think this is the same question asked another way: Is there a way to make lines that are all, say, "dashed" vary in width but not in the relative location of the dashes? Like this (which I totally faked):

推荐答案

来自?par帮助文件中的线型规范"(加粗为强调):

From the ?par helpfile for "Line Type Specification" (bold added for emphasis):

行类型可以通过给小索引指定来指定 内置的线型表(1 =实线,2 =虚线等,请参见lty 以上)或直接作为线段的开/关长度. 这是由一个偶数(最多八个)的字符串完成的 字符,即非零(十六进制)数字, 字符串中连续位置的长度.例如, 字符串"33"指定打开三个单位,然后关闭三个,然后 "3313"指定先打开三个单位,然后关闭三个单位,然后再指定 一上,最后三下.这里的单位"(在大多数设备上) 与lwd成正比,与lwd = 1成比例的像素或点或 1/96英寸.

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.

这表明,如果我们指定线型和线宽适当变化(例如,较粗的线是较细的线的两倍,而开关的长度是原来的一半),则可以达到看似相同的破折号的预期效果不同线宽的长度.

This suggests that if we specify linetypes and line widths that vary appropriately (e.g. the thicker line is twice as wide as the thinner line, with on-off stretches half as long), we can achieve the desired effect of seemingly identical dash lengths at different line widths.

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

Note: the allowed characters in the linetype specification are c(1:9, "A":"F")), which means the shortest possible stretch length is 1 unit, while the longest is 15 units. This limits the number of different lines that can be created.

它可以与基本的R绘图功能一起使用:

It works as expected with the base R plotting functions:

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)

对于ggplot2,据我所知,图形文件设备的选择很重要.破折号与基于 vector 的设备整齐对齐,但不一定与基于 bitmap 的设备对齐:

For ggplot2, on the other hand, as far as I've been able to ascertain, the choice of graphics file device matters. The dashes align neatly for vector-based devices, but not necessarily for bitmap-based ones:

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

我没有在ggplot2中找到任何可以直接解决此问题的方法.不过,如果您需要一种位图格式,但等距的短划线是头等大事,则可以先将绘图输出到基于矢量的设备之一,然后再从一种文件格式转换为另一种文件格式.

I haven't found anything that addresses this directly within ggplot2. Still, if you need one of the bitmap formats but identically spaced dashes are a top priority, you can output your plots to one of the vector-based devices first, and convert from one file format to another thereafter.

这篇关于在ggplot中更改线宽,而不是大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆