interactive.Snw
4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
\documentclass[a4paper]{article}
%\VignetteIndexEntry{Editing grid Graphics}
%\VignettePackage{grid}
\newcommand{\grid}{{\tt grid}}
\newcommand{\R}{{\tt R}}
\setlength{\parindent}{0in}
\setlength{\parskip}{.1in}
\setlength{\textwidth}{140mm}
\setlength{\oddsidemargin}{10mm}
\title{An Example of Interactive Graphics Editing in Grid}
\author{Paul Murrell}
\begin{document}
\maketitle
<<echo=FALSE, results=hide>>=
library(grDevices)
library(grid)
ps.options(pointsize=12)
options(width=60)
@
First of all, we create an x-axis and draw it on the device.
It is very important that we specify a {\tt name} for the object
so that we can refer to it later.
<<fig1, results=hide, fig=TRUE, width=6, height=2, include=FALSE>>=
grid.xaxis(at=1:4/5, vp=viewport(w=.5, h=0.01), name="gxa")
@
\begin{center}
{
\includegraphics[width=3in, height=1in]{interactive-fig1}
}
\end{center}
@
Now we edit the axis, changing the colour of the entire axis to red.
Notice that we refer to the x-axis by its name.
<<edit1, term=FALSE>>=
grid.edit("gxa", gp=gpar(col="red"))
<<fig2, echo=FALSE, results=hide, fig=TRUE, width=6, height=2, include=FALSE>>=
gxa <- xaxisGrob(at=1:4/5, vp=viewport(w=.5, h=.01))
gxa <- editGrob(gxa, gp=gpar(col="red"))
grid.draw(gxa)
@
\begin{center}
{
\includegraphics[width=3in, height=1in]{interactive-fig2}
}
\end{center}
@
Now we change just the labels of the x-axis to green. We use the
{\tt gPath()} function to concatenate the grob names (we could have used
{\tt "gxa::labels"} here, but {\tt gPath()} is recommended for
writing code that will be reused).
<<edit2, term=FALSE>>=
grid.edit(gPath("gxa", "labels"), gp=gpar(col="green"))
<<fig3, echo=FALSE, results=hide, fig=TRUE, width=6, height=2, include=FALSE>>=
gxa <- xaxisGrob(at=1:4/5, vp=viewport(w=.5, h=.01))
gxa <- editGrob(gxa, gp=gpar(col="red"))
gxa <- editGrob(gxa, gPath="labels", gp=gpar(col="green"))
grid.draw(gxa)
@
\begin{center}
{
\includegraphics[width=3in, height=1in]{interactive-fig3}
}
\end{center}
@
It is also possible to change the number of tick marks.
Notice that the labels all change back to red; this happens
because new labels are created by the axis and these ``inherit''
the colour of the axis by default. In other words, the
colour specification of the old labels was specific to the old labels
and was discarded when the old labels were discarded.
<<edit3, term=FALSE>>=
grid.edit("gxa", at=c(0.0, 0.5, 1.0))
<<fig4, echo=FALSE, results=hide, fig=TRUE, width=6, height=2, include=FALSE>>=
gxa <- xaxisGrob(at=1:4/5, vp=viewport(w=.5, h=.01))
gxa <- editGrob(gxa, gp=gpar(col="red"))
gxa <- editGrob(gxa, gPath="labels", gp=gpar(col="green"))
gxa <- editGrob(gxa, at=c(0.0, 0.5, 1.0))
grid.draw(gxa)
@
\begin{center}
{
\includegraphics[width=3in, height=1in]{interactive-fig4}
}
\end{center}
@
Finally, we change the labels back to black and rotate them all $30\deg$.
<<edit4, term=FALSE>>=
grid.edit("gxa::labels", gp=gpar(col="black"), rot=30)
<<fig5, echo=FALSE, results=hide, fig=TRUE, width=6, height=2, include=FALSE>>=
gxa <- xaxisGrob(at=1:4/5, vp=viewport(w=.5, h=.01))
gxa <- editGrob(gxa, gp=gpar(col="red"))
gxa <- editGrob(gxa, gPath="labels", gp=gpar(col="green"))
gxa <- editGrob(gxa, at=c(0.0, 0.5, 1.0))
gxa <- editGrob(gxa, gPath="labels", gp=gpar(col="black"), rot=30)
grid.draw(gxa)
@
\begin{center}
{
\includegraphics[width=3in, height=1in]{interactive-fig5}
}
\end{center}
@
The above examples describe how to perform editing on a grid object
and have the changes updated on screen. The equivalent can be done
entirely ``off-screen'', by just working with the grid object.
The off-screen equivalent in this case would look like:
<<results=hide>>=
gxa <- xaxisGrob(at=1:4/5, vp=viewport(w=.5, h=.01))
gxa <- editGrob(gxa, gp=gpar(col="red"))
gxa <- editGrob(gxa, gPath="labels", gp=gpar(col="green"))
gxa <- editGrob(gxa, at=c(0.0, 0.5, 1.0))
gxa <- editGrob(gxa, gPath="labels", gp=gpar(col="black"), rot=30)
grid.draw(gxa)
@
% Start a new page
% Not echoed, not evaluated
% ONLY here for checkVignettes so that all output doesn't
% end up on one enormous page
<<eval=FALSE, echo=FALSE>>=
grid.newpage()
@
\end{document}