김정득
김정득

Reputation: 21

Same code works differently in R and R studio

While I learned how to plot using grid, R studio showed the result differnet from an example in book. So I run the same code in R, and got the same result to an example in book. I don't know this reason...

below is code.

library(grid)
grid.newpage()
pushViewport(plotViewport(c(5, 4, 2, 2)))
pushViewport(dataViewport(pressure$temperature,pressure$pressure,name="plotRegion"))
grid.points(pressure$temperature, pressure$pressure,name="dataSymbols")
grid.rect()
grid.xaxis()
grid.yaxis()
grid.text("temperature", y=unit(-3, "lines"))
grid.text("pressure", x=unit(-3, "lines"), rot=90)
grid.edit("dataSymbols", pch=2)
upViewport(2)
grid.rect(gp=gpar(lty="dashed"))
downViewport("plotRegion")
grid.text("Pressure (mm Hg)\nversus\nTemperature (Celsius)",x=unit(150, "native"), y=unit(600, "native"))

R studio

enter image description here

R

enter image description here

Upvotes: 2

Views: 106

Answers (1)

Roland
Roland

Reputation: 132864

This code uses the defaults defined in gpar. The help says:

The default parameter settings are defined by the ROOT viewport, which takes its settings from the graphics device. These defaults may differ between devices (e.g., the default fill setting is different for a PNG device compared to a PDF device).

With RStudio:

get.gpar()$fill
#[1] "white"

With RGui:

get.gpar()$fill
#[1] "transparent"

Thus, the RStudio device has different settings. You need to specify explicitly that you don't want the rectangles to be filled.

library(grid)
grid.newpage()
pushViewport(plotViewport(c(5, 4, 2, 2)))
pushViewport(dataViewport(pressure$temperature,pressure$pressure,name="plotRegion"))
grid.points(pressure$temperature, pressure$pressure,name="dataSymbols")
grid.rect(gp = gpar(fill = NA))
grid.xaxis()
grid.yaxis()
grid.text("temperature", y=unit(-3, "lines"))
grid.text("pressure", x=unit(-3, "lines"), rot=90)
grid.edit("dataSymbols", pch=2)
upViewport(2)
grid.rect(gp=gpar(lty="dashed", fill = NA))
downViewport("plotRegion")
grid.text("Pressure (mm Hg)\nversus\nTemperature (Celsius)",x=unit(150, "native"), y=unit(600, "native"))

resulting plot

Upvotes: 3

Related Questions