【问题标题】:How do I remove the _printed_ output warnings using ggplot2 with knitr如何使用带有 knitr 的 ggplot2 删除 _printed_ 输出警告
【发布时间】:2014-07-18 15:10:50
【问题描述】:

GGplot2 在情节中使用 scale_colour_gradient 两次时会打印出警告,我无法在 knitr 中抑制。这是我的浏览器在编织 RHTML 文件后的屏幕截图:

我需要为渐变着色,一种用于线条(收益率曲线的廉价/昂贵),另一种用于相似但略有不同的工具(彩色点)。

这是我的 ggplot 代码:

ggp <- ggplot(polys, aes(x = xvals, y = yvals)) + 
     #geom_polygon(aes(fill = - value, group = id, alpha = value)) + # lovely blue
     geom_polygon(aes(fill = value, group = id, alpha = value)) + # lovely shiny light blue middle draw me in
     scale_x_log10(breaks = xaxtickpos, minor_breaks = NULL) + 
     theme(legend.position = "none", panel.background = element_rect(fill = "grey85", colour = NA)) + 
     xlab("maturity") + ylab("bps")
ggp <- ggp + geom_line(data = quanmelt[quanmelt[, "percentile"] %in% outerthresh, ], 
                       aes(x = mat, y = value, group = percentile), colour = "white", size = 2)
ggp <- ggp + geom_line(data = quanmelt[quanmelt[, "percentile"] %in% innerthresh, ], 
                       aes(x = mat, y = value, group = percentile), colour = "white", size = 1, 
                       linetype = "dotted")
#add last few days line/today (this doesn't work very well hence commented out)
todayback <- todayline[todayline$daysback == 2, ] # get this historic lines
ggp <- ggp + geom_smooth(data = todayback, aes(x = mat, y = value, group = daysback), 
                         colour = "darkred", linetype = "dashed", 
                         se = FALSE, size = 1, method = "loess", span = (ifelse(smooth, 0.3, 0.1)))
#add boxplot
ggp <- ggp + geom_boxplot(data = meltcdlong, aes(x = mat, y = value, group = bond), outlier.size = NA, 
                          colour = "grey30", alpha = 0.5, size = 0.2, width = 0.025)

# add the latest point
ggp <- ggp + geom_point(data = latestcdpoint, aes(x = mat, y = value, group = bond)) 
# now do labels (twice - one for above, one for below)
ggp <- ggp + geom_text(data = latestcdpoint[latestcdpoint$adjustvertvec == 1, ], aes(x = mat, y = labelposies, label = label), 
                       angle = 90, colour = "grey20", size = 3, hjust = 0, alpha = 0.5)
ggp <- ggp + geom_text(data = latestcdpoint[latestcdpoint$adjustvertvec == 0, ], aes(x = mat, y = labelposies, label = label), 
                       angle = 90, colour = "grey20", size = 3, hjust = 1, alpha = 0.5)
#now print a nice z-score graded colour line for the curve 
todaytoday <- todayline[todayline$daysback == 0, ]
minz <- min(rescale(todaytoday[, "zscore"])) # for scaling of z-score line gradient colours
maxz <- max(rescale(todaytoday[, "zscore"]))
bpspline <- smooth.spline(todaytoday$mat, todaytoday$value, spar = 0.4) # Smooth out the curve with lots of points
zscorespline <- smooth.spline(todaytoday$mat, todaytoday$zscore) # and smooth out the zscores too
xplot <- seq(2, maxmat, by = 0.1)
todayplotter <- data.frame(mat = xplot, value = predict(bpspline, xplot)$y, 
                           zscore = rescale(c(-5, 5, predict(zscorespline, xplot)$y))[-1:-2]) # build the plotter
ggp <- ggp + geom_path(data = todayplotter, aes(x = mat, y = value, colour = zscore), size = 2, linejoin = "bevel") +
             scale_colour_gradientn(colours = gradientcolours, values = gradientscale, limits = c(minz, maxz))
#and the title
ggp <- ggp + ggtitle(cCode)
# now the test chart
mm <<- meltcdrecent[meltcdrecent$daysback == 0, ]
ggp <- ggp + geom_point(data = mm, aes(x = mat, y = value, colour = rescale(c(-5, 5, zscore))[-1:-2]), size = 6) +
       scale_colour_gradientn(colours = gradientcolours, values = gradientscale, limits = c(0, 1))
ggp <- ggp + geom_point(data = mm, aes(x = mat, y = value), colour = "black", size = 4.5)
ggp <- ggp + geom_text(data = mm, aes(x = mat, y = value), label = round(mm$zscore, 1), colour = "white", size = 2, alpha = 0.7)

这很复杂,但你可以看到我有两个scale_colour_gradient(s)。

这是我的 knitr 代码:

  <!--begin.rcode changer, echo=FALSE, fig.height=4.5, fig.width=8
    for(x in ac) {
        g <- ggCD(x, plotit = FALSE)
        suppressWarnings(plot(g$cdChart))
    }
    end.rcode-->

我想要么摆脱这些警告(它们不是真正的真正警告,所以 suppressWarnings 不起作用),或者,以一种首先不会产生此文本的方式使用 scale_colour_gradient .

【问题讨论】:

  • 在错误/修复之外:我对这个图表旨在显示的内容非常着迷。
  • @DaveRGP:使用 Nelson Siegel Svensson 框架拟合收益率曲线的单个债券的 z 分数偏差。那是个别的点。通过 z 分数进行颜色编码,以基点为轴。背景白线是该国家(在本例中为法国)与其他市场的 2-z 偏差。 IE:这张图片告诉固定收益投资者哪些个人工具和哪些曲线部门是便宜或昂贵的。买蓝色,卖红色;-) 黄色是中性的。
  • 不得不发表评论,因为图表看起来很漂亮,感谢分享

标签: r ggplot2 knitr


【解决方案1】:

改变

<!--begin.rcode changer, echo=FALSE, fig.height=4.5, fig.width=8

进入

<!--begin.rcode changer, echo=FALSE, fig.height=4.5, fig.width=8, message=FALSE

【讨论】:

  • 我猜这是一个 message 而不是 warning
猜你喜欢
  • 1970-01-01
  • 2010-09-14
  • 2015-02-23
  • 2014-08-15
  • 1970-01-01
  • 2012-05-12
  • 2019-08-04
  • 2016-07-21
  • 1970-01-01
相关资源
最近更新 更多