简体   繁体   English

是否有一种优雅的方法来使ggplot2中的整个图具有统一的字体大小

[英]Is there an elegant way of having uniform font size for the whole plot in ggplot2

I read online that the size of the fonts in a plot plotted with ggplot2 is relative to the base_size specified in the theme() . 我在网上阅读到,用ggplot2绘制的图中字体的大小是相对于theme()指定的base_size

I also found that the size of the specific elements can be modified by doing something like: 我还发现,可以通过执行以下操作来修改特定元素的大小:

theme(axis.title.y = element_text(size = rel(1.5) )

I am using the theme_bw() and would like all of the text in my plot: the labels of the axis, the title of the legend, the items in the legend, the breaks in the axis, to have the same font size. 我正在使用theme_bw()并且希望绘图中的所有文本:轴的标签,图例的标题,图例中的项目,轴中的断点都具有相同的字体大小。 How can this be done? 如何才能做到这一点?

EDIT: 编辑:

I (almost) achieve what I want by using the theme_tufte() as suggester by @lawyeR. 我(几乎)通过使用theme_tufte()作为建议者来实现我想要的。

g + theme_tufte() + theme(axis.rect=element_line() )

gives me a plot with x and y axis drawn as lines. 给我一个以x和y轴绘制为线的图。 However I would like to have the plot where x and y axis form a box. 但是我想得到x和y轴形成一个盒子的图。

How can I draw a box for the x and y axis? 如何为x和y轴绘制一个框?

I solved my problem creating a theme of my own, which combines theme_bw() as its basic theme + the suggestion form @lukeA, + some custom stuff which I need. 我解决了创建自己的主题的问题,该主题将theme_bw()作为其基本主题+建议表单@lukeA +我需要的一些自定义内容。

theme_my <- function(base_size = 14, base_family = "Palatino")
{
  txt <- element_text(size = 14, colour = "black", face = "plain")
  bold_txt <- element_text(size = 14, colour = "black", face = "bold")

  theme_bw(base_size = base_size, base_family = base_family) +
  theme(
    legend.key = element_blank(), 
    strip.background = element_blank(), 

    text = txt, 
    plot.title = txt, 

    axis.title = txt, 
    axis.text = txt, 

    legend.title = bold_txt, 
    legend.text = txt ) 
}

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

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