Thomas Browne
Thomas Browne

Reputation: 24928

How do I remove the _printed_ output warnings using ggplot2 with knitr

GGplot2 prints out a warning when using scale_colour_gradient twice in a plot, which I cannot suppress in knitr. Here is a screenshot of my browser after knitting an RHTML file:

enter image description here

I need to colour gradients, one for the line (cheap / dear of yield curve) and one for the instruments which is similar but subtly different (coloured dots).

Here is my ggplot code:

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)

It's quite complex, but you can see I have two scale_colour_gradient(s).

Here is my knitr code:

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

I would like either to get rid of these warnings (they're not actual real warnings, so suppressWarnings doesn't work), or else, use scale_colour_gradient in a way which does not produce this text in the first place.

Upvotes: 4

Views: 6450

Answers (1)

h.l.m
h.l.m

Reputation: 13485

Change

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

into

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

Upvotes: 10

Related Questions