lao_zhang lao_zhang - 24 days ago 5
R Question

Plotting device with RStudio

I have issue with plotting

lines
over my existing plot in
.Rmd
in RStudio. I ran the code within the code chunk in
.Rmd
(⌘ + return) and the
plot
gives me a graph within the
.Rmd
(new feature of RStudio v1.0), however when I ran the second code
lines
, an error shows up.

plot(density(with$glucose),
ylim = c(0.00, 0.02),
xlab = "Glucose Level",
main = "Figure",
lwd = 2)
lines(density(without$glucose),
col = "red",
lwd = 2)
Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet


On the other hand, if I copy and paste the codes into the console, I could get the plot I want, in the plot viewer within RStudio.
enter image description here

In addition, when I ran some other codes within the
.Rmd
(⌘ + return), my plots in the plot viewer in RStudio disappear. This means I have to do copy-paste into the console instead of using the (⌘ + return) shortcut.

Does anyone have the same problem?

J_F J_F
Answer

This is a known problem, but you can solve it very easy: Press Ctrl+Shift+Enter to run the complete chunk, then everything works fine and you don't have to copy-and-paste all thing to the console. So do all your plots in one chunk and run this chunk. This will produce you the plot within the RMD file (as you mentioned: new feature of RStudio 1.0)