为什么ggplot不允许禁止显示由其几何图形生成的消息? [英] Why does ggplot not allow suppressing of messages generated by its geoms?

查看:143
本文介绍了为什么ggplot不允许禁止显示由其几何图形生成的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ggridges程序包中的geom_density_ridges几何图形创建了棱线,如果未指定带宽,则它将尝试找到一个合理的值.然后,它使用base R message函数报告该值(请参见 https://twitter.com/ClausWilke/status/921363157553172480 ).

The geom_density_ridges geom from the ggridges package created ridgelines, and if a bandwidth is not specified, it attempts to find a sensible value. It then uses the base R message function to report that value (see https://twitter.com/ClausWilke/status/921363157553172480).

base R函数suppressMessages函数旨在禁止显示此类消息.例如,此代码输出一条消息:

The base R function suppressMessages function is designed to suppress such messages. For example, this code outputs a message:

message('This is a message');

此代码不输出任何内容:

And this code outputs nothing:

suppressMessages(message('This is a message'));

但是,由于某种原因,当将此几何图形添加到ggplot中时,消息的抑制似乎被抑制了,嗯.以下代码确实仍然会产生一条消息:

However, for some reason, the suppressing of messages seems, um, suppressed when this geom is added to a ggplot. The following code does still produce a message:

require('ggplot2');
require('ggridges');
suppressMessages(ggplot(Orange, aes(x=age,y=Tree)) + geom_density_ridges());

(特别是"Picking joint bandwidth of 319".)

这是为什么? ggplot是否执行某些操作以确保无论用户的指定如何都可以传递消息?还是我只是碰巧不知道这实际上是明智的行为?

Why is this? Does ggplot do something to ensure that messages come through regardless of the users' specification? Or is this actually sensible behavior that I just happen to not know about?

生成RMarkdown报告时,可以将块选项message设置为message=FALSE,这将在呈现级别禁止显示所有消息.既然这是我的用例,我的问题就解决了.

When generating RMarkdown reports, the chunk option message can be set to message=FALSE, which suppresses all messages at the rendering level. And since that's my use case, my problem is solved.

ggridges软件包的作者Claus Wilke建议,您始终可以手动设置bandwidth以避免出现此消息(

And as Claus Wilke, the author of the ggridges package, suggested, you can always set the bandwidth of manually to avoid the message (https://twitter.com/ClausWilke/status/921361195231215616).

但是为什么首先不suppressMessages禁止显示消息?

But why doesn't suppressMessages suppress the message in the first place?

这是我刚巧不知道的预期行为吗?

Is this expected behavior that I just happen to not know about?

推荐答案

当您调用ggplot()时,该命令实际上并没有绘制图形-它创建了一个ggplot对象.仅当打印该对象时,才实际绘制图.在R控制台中键入表达式时,默认行为是对结果调用print(),这就是为什么ggplot()绘制图的原因.

When you call ggplot(), that command doesn't actually draw a plot -- it creates a ggplot object. Only when that object is printed is a plot actually drawn. When you type an expression in the R console, the default behavior is to call print() on the result which is why it seems like ggplot() draws a plot.

请注意,创建ggplot对象时不会发生您遇到的警告;它们在打印此对象期间发生.因此,如果您运行

Note that the warnings you are experiencing do not occur during the creation of the ggplot object; they occur during the printing of this object. So if you run

suppressMessages(ggplot(...))

print(suppressMessages(ggplot(...)))

以交互方式运行R时.但是,由于ggplot()不会生成任何消息,因此不会抑制任何消息,并且在打印结果对象时这些消息仍会出现.要禁止打印期间创建的消息,您需要将实际的print()语句包装为suppressMessages().

when running R in interactive mode. But since no messages are generated by ggplot(), nothing is suppressed and those messages still appear when the resulting object is printed. To suppress the messages created during printing, you need to wrap the actual print() statement with suppressMessages().

suppressMessages(print(ggplot(...)))

这篇关于为什么ggplot不允许禁止显示由其几何图形生成的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆