0

I want to knit an .Rmd document to PDF, and the document contains a code snippet where I use plotly to build a "scatterpolar" plot of two groups from a dataset, which looks like this:

category  | group1 | group2
----------|--------|-------
category1 |   25.2 |   53.8
category2 |   14.6 |   34.1
category3 |   35.7 |   35.5
...       |    ... |    ...

Because of the warning of "less than 3 observations", the execution is halted when I try to knit it to PDF, no matter the suppressWarnings around the code block or warning=FALSE in the chunk options.

Code:

library(plotly)
suppressWarnings(plot_ly(
  data[data$category != "category5",],
  type = "scatterpolar",
  mode = "lines+markers"
) %>% add_trace(
    r = ~group1,
    theta = ~category,
    color = ~"Group 1",
    line = list(color="#000088",width=2),
    marker = list(color="#000088",width=2)
) %>% add_trace(
    r = ~group2,
    theta = ~category,
    color = ~"Group 2",
    line = list(color="#FF0000", width=2),
    marker = list(color="#FF0000", width=2)
) %>% layout(
  title="Title",
  annotations=list(x=1, y=-.1, text="Source", showarrow=FALSE, xanchor="right"),
  legend=list(x=.9),
  margin=list(t=100, b=50, r=-100)
))

How can I fix this?

  • Including a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610) makes it easier for others to reproduce the problem and thus help you. – Jaap Aug 26 '18 at 14:32

1 Answers1

0

I figured it out now: the colors-attribute in the plot_ly-function fixes the problem.

library(plotly)
plot_ly(
  data[data$category != "category5",],
  type = "scatterpolar",
  mode = "lines+markers",
  colors = c("#000088","#FF0000")
) %>% add_trace(
    r = ~group1,
    theta = ~category,
    color = ~"Group 1",
    line = list(color="#000088",width=2),
    marker = list(color="#000088",width=2)
) %>% add_trace(
    r = ~group2,
    theta = ~category,
    color = ~"Group 2",
    line = list(color="#FF0000", width=2),
    marker = list(color="#FF0000", width=2)
) %>% layout(
  title="Title",
  annotations=list(x=1, y=-.1, text="Source", showarrow=FALSE, xanchor="right"),
  legend=list(x=.9),
  margin=list(t=100, b=50, r=-100)
)