简体   繁体   English

如何在ggplot2主题(ggtheme)中设置默认行大小?

[英]How to set a default line size in a ggplot2 theme (ggtheme)?

I forked the ggthemes git repo to make my own custom theme. 我分叉了ggthemes git repo以制作自己的自定义主题。 I've figured out how to do almost everything I need, but have one hang up. 我已经弄清楚怎么做几乎所有我需要的东西,但是挂了一个。

I am specifically trying to set the default size for geom_line() in my ggtheme. 我专门尝试在geom_line()设置geom_line()的默认size

Where I'm at now, I have to do something like this: 我现在所在的位置,我必须执行以下操作:

economics %>%
    ggplot(aes(date, uempmed)) +
    geom_line(size = 1.75) +
    theme_mycustomtheme()

When I would prefer to just have to do this: 当我只想这样做时:

economics %>%
    ggplot(aes(date, uempmed)) +
    geom_line() +
    theme_mycustomtheme() # this would set the line size automatically

I have edited my mycustomtheme.R file like so: 我已经编辑了mycustomtheme.R文件,如下所示:

theme(
    # Elements in this first block aren't used directly, but are inherited
    # by others
    line =               element_line(
      color = "black", size = 1.75,
      linetype = 1, lineend = "butt"
    )

Note how the size is now set to 1.75. 请注意大小现在如何设置为1.75。 But it doesn't seem to make a difference when I call the theme in practice. 但是当我在实践中称呼主题时,似乎没有什么不同。

I would appreciate any pointers as to what I may be doing wrong. 对于我可能做错了什么,我将不胜感激。 Thanks! 谢谢!

themes don't affect lines in geoms, only lines in axes, gridlines, etc. But, you can change the default appearance of geoms using update_geom_defaults() . 主题不会影响几何图形中的线,只会影响坐标轴,网格线等中的线。但是,您可以使用update_geom_defaults()更改几何图形的默认外观。

#specify geom to update, and list attibutes you want to change appearance of
update_geom_defaults("line", list(size = 1.75))

#now your graph will plot with the line size you defined as default
economics %>%
  ggplot(aes(date, uempmed)) +
  geom_line() 

If you add update_geom_defaults("line", list(size = 1.75)) to the file where you store your custom theme, your geom defaults will also update when you source() your mycustomtheme.r file, and you'll get the linetype you want. 如果将update_geom_defaults("line", list(size = 1.75))到存储自定义主题的文件中,则当source() mycustomtheme.r文件时,geom默认值也会更新,并且您将获得线型你要。 Note that setting defaults this way only changes the exact geom specified ( line ), and does not affect line elements in other geoms (boxplot borders, error bars, etc.), so you will need to define geom defaults for each individual geom you plan to use. 请注意,以这种方式设置默认设置仅会更改指定的精确几何图形( line ),而不会影响其他几何图形中的线元素(箱线边框,误差线等),因此您需要为计划中的每个几何图形定义几何图形默认设置使用。

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

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