简体   繁体   English

使用ggplot自动调整图标题宽度

[英]Automatically adjust plot title width using ggplot

I am fairly new to R/ggplot2 and still learning on the go. 我是R / ggplot2的新手,仍然在旅途中学习。 Hopefully I am not missing something obvious! 希望我不会错过任何明显的东西!

I am trying to create several different plots using ggplot2 that I am layouting using the function plot_grid from the cowplot package to make the plots visible side by side and add plot numeration and captions. 我正在尝试使用ggplot2创建几个不同的图,这些图是使用来自cowplot包的功能plot_grid进行布局的,以使图并排可见,并添加图的编号和标题。 The problem is that if the generated plots are displayed in a small window or I have many plots beside one another then the titles of the two plots sometimes overlap. 问题在于,如果生成的图显示在一个小窗口中,或者我有很多相邻的图,那么这两个图的标题有时会重叠。 To solve this problem I tried to automatically insert line breaks in my too long titles using code I found in another thread since I wanted the text size of the titles to stay constant. 为了解决此问题,我尝试使用在另一个线程中找到的代码在太长的标题中自动插入换行符,因为我希望标题的文本大小保持恒定。

Using the following code I can easily automatically insert the necessary line breaks to make my title a specific width, but the problem is that I always need to enter a numeric value for the width. 使用以下代码,我可以轻松地自动插入必要的换行符,以使标题具有特定的宽度,但是问题是我始终需要为宽度输入一个数值。 Depending on the number of plots I am inserting this value would of course change. 根据插入的地块数量,此值当然会发生变化。 I could of course go through my code and manually set the width for each set of plots until it is the correct value, but I was hoping to automate this process so that the title width is adjusted automatically to match the width of the x-axis. 我当然可以遍历代码并手动设置每组图的宽度,直到它是正确的值为止,但是我希望实现这一过程的自动化,以便自动调整标题宽度以匹配x轴的宽度。 。 Is there anyway to implement this in R? 反正有在R中实现吗?

    #automatically line break and add titles 
    myplot_theme1 = function (plot, x.title = NULL, y.title = NULL, plot.title = NULL) {
        plot +   
        labs(title = paste(strwrap(plot.title, width = 50), collapse = "\n"),
           x = x.title, 
           y = y.title)
    }


    # generate an example plot
    data_plot <- data.frame(x = rnorm(1000), y = rnorm (1000))
    plot1 <- ggplot(data_plot, aes(x = x, y = y)) + geom_point() 
    title <- "This is a title that is very long and does not display nicely"
    myplot_theme1(plot1, plot.title = title)

My test plot 我的测试图

I have tried searching but I haven't found any solutions that seem to address what I am looking for. 我曾尝试搜索,但未找到任何能解决我所寻找问题的解决方案。 The only solution I did find that looked promising was based on the package gridDebug. 我确实发现唯一看起来很有前途的解决方案是基于软件包gridDebug。 This packages doesn't seem to be supported by my operating system anymore though (macOS Sierra Version 10.12.6) since when I try to install it I get the following error message: 我的操作系统似乎不再支持此软件包(macOS Sierra版本10.12.6),因为当我尝试安装它时,出现以下错误消息:

Warning in install.packages: dependencies 'graph', 'Rgraphviz' are not available install.packages中的警告:依赖项“ graph”,“ Rgraphviz”不可用

And on the CRAN package documentation it states that the package is not even available for macOS El Capitan which was my previous operating system. 并且在CRAN软件包文档中指出,该软件包甚至不适用于我以前的操作系统macOS El Capitan。 If someone knows what is causing this issue so that I could try the solution from the above thread that would of course be great as well. 如果有人知道导致此问题的原因,那么我可以尝试上述线程中的解决方案,那当然也很好。

One idea (but perhaps not an ideal solution) is to adjust the size of text based on the number of characters in the title. 一个想法(但可能不是理想的解决方案)是根据标题中的字符数来调整文本的大小。 You can adjust ggplot properties using theme and in this case you want to adjust plot.title (the theme property, not your variable). 您可以使用theme调整ggplot属性,在这种情况下,您需要调整plot.title (主题属性,而不是变量)。 plot.title has elements size and horizontal justification hjust , the latter is in range [0,1]. plot.title具有元素size和水平对齐方式hjust ,后者在[0,1]范围内。

# generate an example plot
data_plot <- data.frame(x = rnorm(1000), y = rnorm (1000))
plot1 <- ggplot(data_plot, aes(x = x, y = y)) + geom_point() 
title1 <- "This is a title that is very long and does not display nicely"
title2 <- "I'm an even longer sentence just test me out and see if I display the way you want or you'll be sorry"

myplot_theme1 = function (plot, x.title = NULL, y.title = NULL, plot.title = NULL) {
        plot +   
        labs(title = plot.title,
           x = x.title, 
           y = y.title) +
        theme(plot.title = element_text(size=800/nchar(plot.title), hjust=0.5))  # 800 is arbitrarily chosen
    }

myplot_theme1(plot1, plot.title = title1)
myplot_theme1(plot1, plot.title = title2)

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

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