Grid Graphics Representation Problem in R-Studio - r

I am working with that book and R-Studio. I tried to render a grid graphic as described in the book:
library(grid)
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(1)
grid.rect(gp=gpar(lty="dashed"))
downViewport("plotRegion")
grid.text("Pressure (mm Hg)\nversus\nTemperature (Celsius)",x=unit(150, "native"), y=unit(600, "native"))
When running that code, it looks like the following:
According to the book, the result should look like this
What do I wrong? Was also running dev.off() without any effect....
I have access to the dataset pressure

Related

R: plot grid objects using par(mfrow)

I would like to plot grid objects and base R objects on the same png. The package I am using (meta) uses the grid graphics system.
I would like to use mfrow for this if possible, as it is what I am using elsewhere.
If I plot:
png("test.png",width=297,height=210,units="mm",res=300)
par(mfrow=c(2,2))
plot(1,1)
plot(1,1)
plot(1,1)
plot(1,1)
dev.off()
Everything works fine.
However if I plot:
library(meta)
m <- metaprop(4:1, c(10, 20, 30, 40))
png("test.png",width=297,height=210,units="mm",res=300)
par(mfrow=c(2,2))
plot(1,1)
forest(m,new=F)
plot(1,1)
forest(m,new=F)
dev.off()
Things aren't right as the forest plots try to take up the whole page rather than being restricted to their corners.
Thanks in advance for your help
I bet it behaves similar to heatmap() plots, see here.
Possible example for a solution from the post (didn't test):
--snip // Citation (Dr Paul Murrell)--
library(gridGraphics)
grid.newpage()
pushViewport(viewport(0, .5, .5, .5, just=c("left", "bottom")))
grid.echo(function() { heatmap(test) }, newpage=FALSE)
popViewport()
pushViewport(viewport(.5, 0, .5, .5, just=c("left", "bottom")))
grid.echo(function() { heatmap(test) }, newpage=FALSE)
popViewport()
--snip--
Modifying this answer, you can combine them into a plot, but I think the margins and settings for forest() needs to be optimized so that the titles etc can be seen properly:
library(gridBase)
library(gridExtra)
layout(matrix(c(1,2),nrow=1),widths=c(1,3))
par(mar=c(2.5,2.5,2.5,2.5))
plot(1,1)
plot.new()
vps <- baseViewports()
pushViewport(vps$figure)
vp1 <-plotViewport()
forest(m,new=FALSE)
popViewport()

How to draw rectangle using grid package

I need to draw a rectangle on my diagram to highlight different changes. I need to use grid package. I tried to use the grid.rect but it doesn't work. I want that my rectangle looks like on the picture.
On the left part of the picture you can see my diagram and of the right part of the picture I've added the rectangle (in Paint) like I want it will be.
library(grid)
library(lattice)
library(sandwich)
data("Investment")
Investment <- as.data.frame(Investment)
trellis.par.set(theme = canonical.theme("postscript", color=FALSE))
grid.newpage()
pushViewport(viewport(x=0, width=.4, just="left"))
print(barchart(table(Investment$Interest)),
newpage=FALSE)
popViewport()
pushViewport(viewport(x=.4, width=.5, just="left"))
print(xyplot(Investment ~ Price, data=Investment,
auto.key=list(space="right"),
par.settings=list(superpose.symbol=list(pch=c(1, 3, 16),
fill="white"))),
newpage=FALSE)
popViewport()
It is not completely clear where you are trying to draw the rectangle, but the code below will add the rectangle to approximately match your picture. You can tune the position.
Use your code just as you had it. I will start by repeating your print statement and then adding the rectangle.
print(xyplot(Investment ~ Price, data=Investment,
auto.key=list(space="right"),
par.settings=list(superpose.symbol=list(pch=c(1, 3, 16),
fill="white"))),
newpage=FALSE)
grid.rect(x = unit(0.42, "npc"), y = unit(0.35, "npc"),
width = unit(0.2, "npc"), height = unit(0.2, "npc"),
gp=gpar(col="red"))
popViewport()

How to add arrow using grid package

I want to add the arrow using grid package which will highlight the important part of my diagram. I want that the arrow will look like on the picture on the right side.
On the left part is my diagram created by the code below and on the right part is my diagram with the arrow (I've added it using Paint). It's my goal.
library(grid)
library(lattice)
library(sandwich)
data("Investment")
Investment <- as.data.frame(Investment)
pushViewport(plotViewport(c(5, 4, 2, 2)))
pushViewport(dataViewport(Investment$Investment,
Investment$GNP,
name="Zaleznosc Investment od GNP"))
grid.points(Investment$Investment, Investment$GNP, gp=gpar(cex=0.5))
grid.rect()
grid.xaxis()
grid.yaxis()
grid.text("Investment", y=unit(-3, "line"))
grid.text("GNP", x=unit(-3, "line"), rot=90)
popViewport()
You can use the code that you have, but before popViewport() add the code to add your arrow.
grid.lines(x = unit(c(0.42, 0.74), "npc"),
y = unit(c(0.8, 0.86), "npc"),
gp = gpar(fill="black"),
arrow = arrow(length = unit(0.2, "inches"),
ends="last", type="closed"))
Also, to follow up on a comment of #alistaire grid graphics are a bit hard to use. What you are trying to do is mostly easy in base graphics.
plot(GNP ~ Investment, data=Investment)
arrows(250, 2600, 380, 2750, code = 2, lwd=2)
The only thing that is not quite perfect is the type of arrowhead. The base arrows does not give you much control over that. If you don't mind adding a package, the shape package lets you choose the style of arrowhead.
library(shape)
plot(GNP ~ Investment, data=Investment)
Arrows(250, 2600, 380, 2750, code = 2,
arr.type="triangle", arr.width=0.4)
In his comment, #alistaire has mentioned ggplot2 as an alternative to working with grid directly.
For the sake of completeness, here is a ggplot2 version which uses the annotate() function to place an arrow on the chart.
data(Investment, package = "sandwich")
library(ggplot2)
ggplot(as.data.frame(Investment)) +
aes(Investment, GNP) +
geom_point(shape = 1L) +
theme_bw() +
annotate("segment", x = 250, xend = 380, y = 2600, yend = 2800,
arrow = arrow(length = unit(0.2, "inches"), ends = "last", type = "closed"))
Note that ggplot2 reexports the arrow() function from the grid package.

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
R
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"))

Nested layouts in R

I want to make a plot consisting of multiple plots consisting of multiple plots, say a 5x2 grid with three plots in each cell. To be more precise, what I need is not just one figure but finding a way of using my plotting function multiple times in a single plot.
I have written a function that uses layout to stack plots, with a common axis in outer margin. I actually need it for seqIplot and seqdplot functions from the TraMineR package, but as far as I understand the problem is not related to those, so here is a minimal working example with barplot.
stackedplot <- function(data){
layout(matrix(c(1:3), nrow=3))
par(mar=c(0,0,0,0), oma=c(4,1,1,1), mgp=c(3,0.5,0), cex=1)
barplot(data[[1]], axes=F, xlab="", ylab="", horiz=TRUE)
barplot(data[[2]], axes=F, xlab="", ylab="", horiz=TRUE)
barplot(data[[3]], axes=F, xlab="", ylab="", horiz=TRUE)
axis(1, at=c(0:10)/10, outer=TRUE)
mtext("Label", line=2, side=1)
}
stackedplot(list(1:10, 10:1, rep(1,10)))
What I would like to do is to then use something like layout again and use stackedplot for the grids of the layout, i.e. something like this (which, of course, does not work):
layout(matrix(c(1:2), nrow=1))
stackedplot(list(1:10, 10:1, rep(1,10)))
stackedplot(list(rep(1,10), 1:10, 10:1))
I have tried split.screen, with no success:
split.screen(c(1,2))
screen(1)
stackedplot(list(1:10, 10:1, rep(1,10)))
screen(2)
stackedplot(list(rep(1,10), 1:10, 10:1))
close.screen(all = TRUE)
I also tried grid package, but apparently it is not compatible with base graphics.
grid.newpage()
pushViewport(viewport(x=0, y=0, width=0.5, height=1,
default.units="native"))
print(stackedplot(list(1:10, 10:1, rep(1,10))), newpage=FALSE)
pushViewport(viewport(x=0.5, y=0, width=0.5, height=1,
default.units="native"))
print(stackedplot(list(rep(1,10), 1:10, 10:1)), newpage=FALSE)
After more research and some help I am now answering my own question in case it would be useful to someone else.
Nested layouts can be created with the grid package, which can be used for base graphics using the gridBase package. The function for the stacked plots is written as follows.
library(grid)
library(gridBase)
stackedplot <- function(data, main=""){
top.vp <- viewport(layout=grid.layout(nrow=5, ncol=1,
heights=unit(c(3, 1, 1, 1, 5),
c("lines", "null", "null", "null", "lines"))),
width=unit(0.9, "npc"))
title <- viewport(layout.pos.row=1, layout.pos.col=1, name="title")
p1 <- viewport(layout.pos.row=2, layout.pos.col=1, name="plot1")
p2 <- viewport(layout.pos.row=3, layout.pos.col=1, name="plot2")
p3 <- viewport(layout.pos.row=4, layout.pos.col=1, name="plot3")
xaxis <- viewport(layout.pos.row=5, layout.pos.col=1, name="xaxis")
splot <- vpTree(top.vp, vpList(title, p1, p2, p3, xaxis)) # Defining the hierarchy of the viewports
pushViewport(splot) # Creating viewports for plotting with the definitions of splot
upViewport() # Navigating up in the viewport tree
downViewport("plot1") # Navigating down in the viewport tree, searching for viewport "plot1"
grid.rect() # Plotting a rectangle (borders for the viewport)
par(plt=gridPLT(), new=TRUE) # Taking the dimensions of the viewport for a base graphics plot
# Adding plot to an existing plot
barplot(data[[1]], axes=FALSE, xlab="", ylab="", horiz=TRUE)
upViewport()
downViewport("plot2")
grid.rect()
par(plt=gridPLT(), new=TRUE)
barplot(data[[2]], axes=FALSE, xlab="", ylab="", horiz=TRUE)
upViewport()
downViewport("plot3")
grid.rect()
par(plt=gridPLT(), new=TRUE)
barplot(data[[3]], xlab="", ylab="", horiz=TRUE)
upViewport()
downViewport("xaxis")
grid.text("X label", y = unit(2, "lines"))
upViewport()
downViewport("title")
grid.text(main, y = unit(1, "lines"))
upViewport(2)
}
The function first describes a viewport (of 90% of the window's width) that is divided into a 5x1 grid of viewports with differing heights. Each viewport in the grid is given a name that can be later called. The tree of viewports (splot) is described with vpTree which defines the hierarchical structure of the viewports. After describing the viewports those are actually prepared for plotting with pushViewport.
Now each named viewport is first seeked and then opened for plotting with upViewport (which goes up in the viewport tree) and downViewport (which seeks for the requested viewport down in the viewport tree).
For plotting base graphics, gridPLT is needed here (alternatively gridFIG or gridOMI can be used, see the manual of gridBase for further info). After that any base graphics function can be used to plot into the current viewport.
After the requested plots, upViewport(2) is used to navigate back to the root (2 viewports up in the hierarchy).
Now the stackedplot function can be called multiple times in another grid as follows.
opar <- par(no.readonly=TRUE) # Saving graphical parameters
plot.new() # Needed for par(new=TRUE) in stackedplot()
multitop.vp <- viewport(layout=grid.layout(1,2), width = unit(0.95, "npc"))
pl1 <- viewport(layout.pos.col=1, layout.pos.row=1, name="A")
pl2 <- viewport(layout.pos.col=2, layout.pos.row=1, name="B")
vpall <- vpTree(multitop.vp, vpList(pl1,pl2))
pushViewport(vpall)
upViewport()
downViewport("A")
stackedplot(data=list(1:10,10:1,rep(10,10)),main="A")
upViewport()
downViewport("B")
stackedplot(data=list(10:1,rep(10,10),1:10),main="B")
upViewport(2)
par(opar) # Returning the graphical parameters saved earlier

Resources