简体   繁体   English

在自定义 ggplot 主题中添加视觉装饰

[英]Adding visual embellishment in a custom ggplot theme

I'm currently prototyping custom ggplot themes for use within my organisation.我目前正在为我的组织中使用的自定义 ggplot 主题制作原型。 Currently, the theme looks like this (using mtcars data):目前,主题看起来像这样(使用 mtcars 数据): 在此处输入图像描述

I would like to add a solid-colored bar (in the org's primary color) underlining the chart title region to act as an easily scalable type of branding (rather than a jpeg logo which restricts the aspect ratio and size of the charts).我想添加一个单色条(以组织的原色),在图表标题区域下划线,作为一种易于扩展的品牌类型(而不是限制图表纵横比和大小的 jpeg 徽标)。 I made a mockup of the effect I'm trying to achieve in paint:我制作了我试图在绘画中实现的效果的模型:

在此处输入图像描述

I'm aware of annotate() but as far as I understand it that function only accepts arguments corresponding to x and y coordinates in the plotting area, so I don't know how to create an annotation that is bound to a point outside of the plotting area我知道 annotate() 但据我了解 function 只接受 arguments 对应于绘图区域中的 x 和 y 坐标,所以我不知道如何创建绑定到外部点的注释绘图区

I would use annotation_custom using a grid::linesGrob here.我会在这里使用grid::linesGrob来使用annotation_custom This allows you to place the line relative to the panel without having to use the plot limits, which can produce inconsistent results.这允许您相对于面板放置线,而不必使用 plot 限制,这会产生不一致的结果。 It also allows the line to extend beyond the left and right limits of the plotting area.它还允许线条延伸到绘图区域的左右界限之外。

Suppose your plot is created a bit like this:假设您的 plot 创建有点像这样:

library(ggplot2)

p <- ggplot(mpg, aes(displ, hwy, col = class)) +
  geom_point() +
  labs(title = "Test 1, theme 1", subtitle = "R default dataset",
       caption = "Organization caption here",
       y = "Fuel efficiency (mpg)",
       x = "Engine displacement (litres)") +
  scale_color_brewer(palette = "Set2", name = NULL) +
  theme(panel.grid = element_line(color = "gray50"),
        panel.border = element_rect(fill = NA),
        legend.position = "top")

p

在此处输入图像描述

To add the line you can do:要添加该行,您可以执行以下操作:

p + coord_cartesian(clip = "off") +
   annotation_custom(grid::linesGrob(
    x = unit(c(-1, 2), "npc"), y = unit(c(1.2, 1.2), "npc"),
    gp = grid::gpar(col = "orange2", lwd = 5)))

在此处输入图像描述

It is a bit hard without reproducible example, but you can use annotate with "segment" and define the x-values with Inf and the y-values with max(y) + some value depending on your theme layout like this:如果没有可重现的示例,这有点困难,但是您可以使用带有“段”的annotate ,并使用Inf定义 x 值,使用max(y) + 一些值定义 x 值,具体取决于您的theme布局,如下所示:

library(ggplot2)
library(dplyr)
mtcars %>%
  ggplot(aes(x = mpg, y = wt)) +
  geom_point() +
  annotate("segment", x = -Inf, xend = Inf, y = max(mtcars$wt) + 0.5, yend = max(mtcars$wt) + 0.5, colour = "orange", size = 2) +
  coord_cartesian(clip = "off", ylim = c(min(mtcars$wt), max(mtcars$wt))) +
  theme(plot.margin = unit(c(3,3,1,1), "lines")) 

Created on 2022-07-27 by the reprex package (v2.0.1)代表 package (v2.0.1) 于 2022 年 7 月 27 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM