如何使用带编织器的ggplot2删除_printed_输出警告

时间:2014-07-18 15:10:50

标签: r ggplot2 knitr

GGplot2在绘图中使用scale_colour_gradient两次打印出警告,我无法在knitr中抑制。以下是编辑RHTML文件后浏览器的屏幕截图:

enter image description here

我需要着色渐变,一个用于线条(便宜/亲爱的屈服曲线),一个用于相似但略有不同的乐器(彩色圆点)。

这是我的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)。

这是我的编织代码:

  <!--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的方式不会产生此文本第一名。

1 个答案:

答案 0 :(得分:8)

更改

<!--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