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 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):
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:
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
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
Now it works in all my computers.
Thanks again for your help.