Search code examples
rplotlyr-markdownscatter-plot

Scatter plot points don't appear when run within R Markdown


When I plot a scatter plot with plotly in R, the output differs significantly when I try to plot the same code in R Markdown.

  1. Scatter plot in R Scatter plot in R

  2. Scatter plot in Markdwon Scatter plot in Markdown

the code:
Given that there are more than 8,000 values, I can provide a plot using the first 20 values as an example.

structure(list(indice = 1:20, Media_Recomendacao = c(9, 7, 8, 
8, 7, 7, 9, 8, 0, 0, 9, 9, 6, 7, 4, 7, 8, 8, 9, 7), Media_CSAT = c(8.43, 
6.14, 7.5, 8, 6.79, 7.79, 6.93, 7.79, 5.64, 4.21, 9.29, 7.93, 
6.14, 6.29, 5.14, 6.86, 8, 7.71, 8.64, 7.21)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))
fig <- 
plot_ly(
    data = dispersion, 
    x = ~Media_CSAT,
    y = ~Media_Recomendacao,
    type = 'scattergl',
    mode = "markers",
    marker = list(color = 'grey')) %>%
layout(
    xaxis = list(
        title = "Valor CSAT",
        tickmode = "array",
        tickvals = 1:10,
        ticktext = 1:10), 
    yaxis = list(title = "Recomendação",
        tickmode = "array",
        tickvals = 1:10,
        ticktext = 1:10)
  )

fig

I'm having trouble figuring out how to fix this issue. I suspect that Markdown is plotting the mean of x and y instead.


Solution

  • This worked for me.

    ---
    title: A plot
    params:
      data:
    output: html_document
    ---
    
    
    ```{r, comment=FALSE, message=FALSE, fig.align='center', warning=TRUE, echo = FALSE, warning=FALSE, width=10}
    plot_ly(
        data = params$data, 
        x = ~Media_CSAT,
        y = ~Media_Recomendacao,
        type = 'scattergl',
        mode = "markers",
        marker = list(color = 'grey')) %>%
    layout(
        xaxis = list(
            title = "Valor CSAT",
            tickmode = "array",
            tickvals = 1:10,
            ticktext = 1:10), 
        yaxis = list(title = "Recomendação",
            tickmode = "array",
            tickvals = 1:10,
            ticktext = 1:10)
      )
    ```
    
    dispersion <- structure(list(indice = 1:20, Media_Recomendacao = c(9, 7, 8, 8, 7, 7, 9, 8, 0, 0, 9, 9, 6, 7, 4, 7, 8, 8, 9, 7), Media_CSAT = c(8.43, 6.14, 7.5, 8, 6.79, 7.79, 6.93, 7.79, 5.64, 4.21, 9.29, 7.93, 6.14, 6.29, 5.14, 6.86, 8, 7.71, 8.64, 7.21)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"))
    rmarkdown::render("~/tmp/quux.Rmd", params=list(data=dispersion))
    # processing file: quux.Rmd
    #                                                                                                                                                
    # output file: quux.knit.md
    # /usr/bin/pandoc +RTS -K512m -RTS quux.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output quux.html --lua-filter /home/r2/R/x86_64-pc-linux-gnu-library/4.2/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /home/r2/R/x86_64-pc-linux-gnu-library/4.2/rmarkdown/rmarkdown/lua/latex-div.lua --self-contained --variable bs3=TRUE --section-divs --template /home/r2/R/x86_64-pc-linux-gnu-library/4.2/rmarkdown/rmd/h/default.html --no-highlight --variable highlightjs=1 --variable theme=bootstrap --mathjax --variable 'mathjax-url=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML' --include-in-header /home/r2/tmp/rmarkdown-str6d0913f4c662d.html 
    # Output created: quux.html
    

    rmarkdown with plotly