仅通过一点抑制来自geom_line的消息 [英] Suppress message from geom_line with only one point

查看:100
本文介绍了仅通过一点抑制来自geom_line的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历多个数据集以为每个数据集绘制线图.当我使用geom_line超过一分时,如何防止ggplot抱怨?

I'm iterating through multiple data sets to produce line plots for each set. How can I prevent ggplot from complaining when I use geom_line over one point?

例如,获取以下数据:

mydata = data.frame(
  x = c(1, 2),
  y = c(2, 2),
  group = as.factor(c("foo", "foo"))
)

创建折线图的外观和效果很好,因为折线中有两个点:

Creating line graph looks and works just fine because there are two points in the line:

ggplot(mydata, aes(x = x, y = y)) + 
  geom_point() + 
  geom_line(aes(group = group))

但是,仅绘制第一行会显示以下信息:

However, plotting only the fist row give the message:

geom_path:每个组仅包含一个观察值.您需要调整小组的审美吗?

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

ggplot(mydata[1,], aes(x = x, y = y)) + 
      geom_point() + 
      geom_line(aes(group = group))

我的一些数字只有一点,并且消息会在产生这些数字的更大的脚本中导致挂断.我知道这些情节仍然有效,所以我担心的是避免出现此消息.如果可能的话,我还要避免使用suppressWarnings(),以免出现其他合法且意外的问题.

Some of my figures will only have one point and the messages cause hangups in the greater script that produces these figures. I know the plots still work, so my concern is avoiding the message. I'd also like to avoid using suppressWarnings() if possible in case another legitimate and unexpected issue arises.

推荐答案

Per an answer to this question: suppressMessages(ggplot()) fails because you have to wrap it around a print() call of the ggplot object--not the ggplot object itself. This is because the warning/message only occurs when the object is drawn.

因此,要在不显示警告消息的情况下查看图表,请执行以下操作:

So, to view your plot without a warning message run:

p <- ggplot(mydata[1,], aes(x = x, y = y)) + 
  geom_point() + 
  geom_line(aes(group = group))

suppressMessages(print(p))

这篇关于仅通过一点抑制来自geom_line的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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