Color in qplot

chettyvk's Avatar

chettyvk

10 Aug, 2012 12:07 AM

The plot does not have colors. I would appreciate help to modify the following script. I use the latest RStudion and R versions. Here is the script I used. Thanks

\documentclass{article} \usepackage{alltt} \begin{document} <<>>=
library(ggplot2)
library(RColorBrewer)
attach(mpg)
qplot(displ,hwy,data=mpg,color=factor(cyl))
@ \end{document}

  1. Support Staff 2 Posted by Josh Paulson on 10 Aug, 2012 02:36 PM

    Josh Paulson's Avatar

    Hello,

    Take a look at the following Knowledge Base article. This should give you some direction on how to get help with this:

    http://support.rstudio.org/help/kb/troubleshooting/getting-help-with-r

    Josh

  2. Josh Paulson closed this discussion on 10 Aug, 2012 02:36 PM.

  3. chettyvk re-opened this discussion on 10 Aug, 2012 02:47 PM

  4. 3 Posted by chettyvk on 10 Aug, 2012 02:47 PM

    chettyvk's Avatar

    Thanks Josh. It works perfectly in R and also in Markdown. The problem
    occurs only when use knitr and compile the PDF. SO does this not suggest it
    is an RStudio related issue? I have also tried some suggestions in the
    discussion such as "dev.off()" several times. It does not help. I think it
    has something to do with opening the right plotting device.
    Thanks.
    Chetty

  5. Support Staff 4 Posted by Josh Paulson on 10 Aug, 2012 04:31 PM

    Josh Paulson's Avatar

    Chetty,

    I'm actually unable to reproduce this problem using your exact document and compiling with knitr. Can you add the following to the R code chunk in your document and attach the pdf that is created:

    > sessionInfo()
    > RStudio.version()
    

    Josh

  6. Support Staff 5 Posted by Josh Paulson on 10 Aug, 2012 04:38 PM

    Josh Paulson's Avatar

    See the attached file for what I'm looking for.

  7. 6 Posted by chettyvk on 10 Aug, 2012 04:48 PM

    chettyvk's Avatar

    Thanks Josh. I have attached the pdf.
    Chetty

  8. Support Staff 7 Posted by Josh Paulson on 10 Aug, 2012 06:08 PM

    Josh Paulson's Avatar

    Chetty,

    Thanks for uploading this and there is definitely something strange going on here. If you look at your plot, it actually plotted the following:

    > qplot(speed, dist, data=cars)
    

    This doesn't show up anywhere in your code, but is clearly in your pdf. This explains why the color is void from the plot since it is the wrong plot entirely. I honestly don't know what would be doing this, but am fairly certain it is not RStudio. We're not doing anything other than the following (you could try replicating from another R interface):

    > grDevices::pdf.options(useDingbats = FALSE)
    > require(knitr)
    > opts_knit$set(concordance = TRUE)
    > knit('document.Rnw')
    

    None of this (as far as I know) would cause the behavior that you are seeing. Nevertheless, it would be good to get to the bottom of what is going wrong. Can you try a more simple plot such running the following in a new Sweave document:

    \documentclass{article}
    \begin{document}
    
    <<test>>=
    plot(pressure, col="red")
    @
    
    \end{document}
    

    Also can you think of any other reason why your session might be injecting code from somewhere else? Does this happen inside/outside a RStudio project? Have you customized any options for latex or markdown?

    http://www.rstudio.org/docs/authoring/latex_program
    http://www.rstudio.org/docs/authoring/markdown_custom_rendering

    Josh

  9. 8 Posted by chettyvk on 11 Aug, 2012 01:30 AM

    chettyvk's Avatar

    Thanks Josh. I tired in another PC. I got the same outcome. Then I tried
    the simple plot "plot(pressure,col="red") and it worked Then I tried my
    qplot. This time it worked correctly ( See the attached output).
    I am not an expert. My guess is it has something to do with closing and
    opening devices.
    Now it works in all my computers.
    Thanks again for your help.
    Chetty

  10. Support Staff 9 Posted by Josh Paulson on 11 Aug, 2012 03:53 AM

    Josh Paulson's Avatar

    Chetty,

    Glad to hear that it's working now and my guess is that there was some caching involved. Perhaps somehow a former plot was being used rather than the current.

    Josh

  11. Josh Paulson closed this discussion on 11 Aug, 2012 03:53 AM.

Comments are currently closed for this discussion. You can start a new one.