如何在R绘图中插入自定义刻度标记和相应的标签?

huangapple go评论56阅读模式
英文:

How to insert a custom tick mark and a corresponding label on R plot?

问题

在创建图表时,R会为轴分配默认刻度标记。我希望插入自定义刻度标记和标签,以覆盖默认的刻度标记。我还想进一步自定义插入的刻度标记和标签(例如,更改大小和颜色),使它们与默认生成的不同。

这里有一个示例:

library(ggplot2)

x <- c(1,2,3,4,5,6,7)
y <- c(1,2,3,4,5,6,7)
ggplot() + geom_line(aes(x,y)) + geom_point(aes(x=3.27, y=3.33), size=7, col="turquoise4")

在图片中,有色点是图表上最重要的数据点。因此,目标是帮助读者轻松理解该数据点。理想情况下,手绘的红色刻度标记将比默认的刻度标记更粗,与数据点相同的颜色,并伴随相应的标签(即x轴的3.27和y轴的3.33) - 同样与数据点相同的颜色,比默认的刻度标记更大。

在查阅了一些有关轴自定义的资源后,似乎它们都集中于一次性调整所有刻度标记 - 颜色、方向、大小、刻度标记之间的间隔等等。如何创建与其他刻度标记和标签不同的独特刻度标记和标签仍然对我来说是一个谜。非常感谢任何帮助 如何在R绘图中插入自定义刻度标记和相应的标签?

英文:

When creating a plot, R assigns default tick marks for the axes. I wish to insert custom tick marks and labels along the default tick marks. I'd also like to further customise the inserted tick marks and labels (e.g., change size and color) so that they would differ from the default generated ones.

Here's an illustration:

library(ggplot2)

x &lt;- c(1,2,3,4,5,6,7)
y &lt;- c(1,2,3,4,5,6,7)
ggplot() + geom_line(aes(x,y)) + geom_point(aes(x=3.27, y=3.33), size=7, col=&quot;turquoise4&quot;)

enter image description here

In the picture, the coloured point is the most important piece of data on the graph. As such, the goal is to help the reader easily understand that data point. Ideally, the hand-drawn red tick marks would be thicker than the default ones, have the same colour as the data point, and be accompanied with the corresponding labels (i.e., 3.27 for x axis, and 3.33 for y) - again, same colour as the data point, and bigger in size, comparing to the default ones.

After having checked several resources on axis customisation, it seems that they focus on adjusting all tick marks at once - colour, orientation, size, intervals between tick marks, etc. How to create unique tick marks and labels that differ from the rest remains a mystery for me. Any help is highly appreciated 如何在R绘图中插入自定义刻度标记和相应的标签?

答案1

得分: 1

你可以始终通过scalebreaks参数来设置你想要的断点,例如对于你的例子,你可以像下面这样添加自定义的断点和刻度到x轴:

library(ggplot2)

x <- c(1, 2, 3, 4, 5, 6, 7)
y <- c(1, 2, 3, 4, 5, 6, 7)

base <- ggplot() +
  geom_line(aes(x, y)) +
  geom_point(aes(x = 3.27, y = 3.33), size = 7, col = "turquoise4")

base +
  scale_x_continuous(breaks = c(2, 4, 6, 3.27))

对于一个更通用或自动化的方法,你可以将自定义的断点添加到通过scales::breaks_extended()计算的默认断点向量中,例如:

base +
  scale_x_continuous(breaks = c(scales::breaks_extended()(x), 3.27))

类似地,你可以使用labels参数分配一个标签向量,或者使用格式化函数为断点添加标签。对于每种情况,你可以使用ggtext包来设置你自定义刻度的颜色和/或字体大小,例如对于手动方式,你可以这样做:

library(ggtext)

base +
  scale_x_continuous(
    breaks = c(2, 4, 6, 3.27),
    labels = c(2, 4, 6, "<span style='color: turquoise4; font-size: 20pt'>3.27</span>")
  ) +
  theme(axis.text.x = ggtext::element_markdown())

最后,对于一个更通用或自动化的方法,我个人更喜欢使用一些自定义函数。然而,是否值得采用这种方法取决于你希望或需要使用这个功能的频率。这样的方法可能看起来是这样的:

library(ggplot2)
library(ggtext)

label_highlight <- function(x, no_high) {
  x[!x %in% no_high] <- glue::glue(
    "<span style='color: turquoise4; font-size: 20pt'>{x[!x %in% no_high]}</span>"
  )
  x
}

default_breaks <- function(x, expand = .05) {
  scales::breaks_extended()(range(x) + expand * c(-1, 1) * diff(range(x)))  
}

default_x <- default_breaks(x)
default_y <- default_breaks(y)

ggplot() +
  geom_line(aes(x, y)) +
  geom_point(aes(x = 3.27, y = 3.33), size = 7, col = "turquoise4") +
  scale_x_continuous(
    breaks = c(default_x, 3.27),
    label = ~ label_highlight(.x, default_x)
  ) +
  scale_y_continuous(
    breaks =  c(default_y, 3.33),
    label = ~ label_highlight(.x, default_y)
  ) +
  theme(
    axis.text.x = ggtext::element_markdown(),
    axis.text.y = ggtext::element_markdown()
  )
英文:

You can always set your desired breaks via the breaks argument of the scale, e.g. for your example you could add your custom break and tick to the x axis like so:

library(ggplot2)

x &lt;- c(1, 2, 3, 4, 5, 6, 7)
y &lt;- c(1, 2, 3, 4, 5, 6, 7)

base &lt;- ggplot() +
  geom_line(aes(x, y)) +
  geom_point(aes(x = 3.27, y = 3.33), size = 7, col = &quot;turquoise4&quot;)

base +
  scale_x_continuous(breaks = c(2, 4, 6, 3.27))

如何在R绘图中插入自定义刻度标记和相应的标签?<!-- -->

For a more general or automatic approach you could add the custom break to the default vector of breaks which are computed via scales::breaks_extended(), i.e you could do:

base +
  scale_x_continuous(breaks = \(x) c(scales::breaks_extended()(x), 3.27))

如何在R绘图中插入自定义刻度标记和相应的标签?<!-- -->

Similar you could use the labels argument to assign a vector of labels or use a formatter function to add labels to the breaks. For each case one approach to set a color and/or font size for your custom tick only or for a subset of ticks would be to use the ggtext package which allows for styling via Markdown, HTML and/or CSS ,e.g. for the manual approach you could do:

library(ggtext)

base +
  scale_x_continuous(
    breaks = c(2, 4, 6, 3.27),
    labels = c(2, 4, 6, &quot;&lt;span style=&#39;color: turquoise4; font-size: 20pt&#39;&gt;3.27&lt;/span&gt;&quot;)
  ) +
  theme(axis.text.x = ggtext::element_markdown())

如何在R绘图中插入自定义刻度标记和相应的标签?<!-- -->

Finally, for a more general or automatic approach I personally prefer to use some custom functions. Whether such an approach is worthwhile depends however on how often you have or want to use this functionality. Such an approach may look like so:

library(ggplot2)
library(ggtext)

label_highlight &lt;- function(x, no_high) {
  x[!x %in% no_high] &lt;- glue::glue(
    &quot;&lt;span style=&#39;color: turquoise4; font-size: 20pt&#39;&gt;{x[!x %in% no_high]}&lt;/span&gt;&quot;
  )
  x
}

default_breaks &lt;- function(x, expand = .05) {
  scales::breaks_extended()(range(x) + expand * c(-1, 1) * diff(range(x)))  
}

default_x &lt;- default_breaks(x)
default_y &lt;- default_breaks(y)

ggplot() +
  geom_line(aes(x, y)) +
  geom_point(aes(x = 3.27, y = 3.33), size = 7, col = &quot;turquoise4&quot;) +
  scale_x_continuous(
    breaks = c(default_x, 3.27),
    label = ~ label_highlight(.x, default_x)
  ) +
  scale_y_continuous(
    breaks =  c(default_y, 3.33),
    label = ~ label_highlight(.x, default_y)
  ) +
  theme(
    axis.text.x = ggtext::element_markdown(),
    axis.text.y = ggtext::element_markdown()
  )

如何在R绘图中插入自定义刻度标记和相应的标签?<!-- -->

huangapple
  • 本文由 发表于 2023年6月22日 17:44:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定