Skip to content

Commit 86c1f74

Browse files
committed
doit
1 parent 2ed1713 commit 86c1f74

File tree

9 files changed

+644
-504
lines changed

9 files changed

+644
-504
lines changed

src/library/grid/vignettes/frame.Rnw

Lines changed: 228 additions & 163 deletions
Large diffs are not rendered by default.
Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
%\VignetteEncoding{UTF-8}
22
% File src/library/grid/vignettes/interactive.Rnw
33
% Part of the R package, https://www.R-project.org
4-
% Copyright 2001–2026 Paul Murrell and the R Core Team
4+
% Copyright 2001-13 Paul Murrell and the R Core Team
55
% Distributed under GPL 2 or later
66

77
\documentclass[a4paper]{article}
88
%\VignetteIndexEntry{Editing grid Graphics}
99
%\VignettePackage{grid}
10-
1110
\newcommand{\code}[1]{\texttt{#1}}
1211
\newcommand{\pkg}[1]{{\normalfont\fontseries{b}\selectfont #1}}
1312
\newcommand{\grid}{\pkg{grid}}
1413
\newcommand{\R}{{\sffamily R}}
15-
1614
\setlength{\parindent}{0in}
1715
\setlength{\parskip}{.1in}
1816
\setlength{\textwidth}{140mm}
1917
\setlength{\oddsidemargin}{10mm}
20-
2118
\title{An Example of Interactive Graphics Editing in \grid}
2219
\author{Paul Murrell}
2320

@@ -30,22 +27,22 @@ library(grid)
3027
ps.options(pointsize = 12)
3128
options(width = 60)
3229
@
33-
34-
\section*{Creating and Naming Grobs}
35-
We begin by creating an x‑axis and drawing it on the device. It is
36-
important to assign a \code{name} to the grob so that it can be
37-
referenced later for editing.
30+
First of all, we create an x-axis and draw it on the device.
31+
It is very important that we specify a \code{name} for the object
32+
so that we can refer to it later.
3833

3934
<<fig1, results=hide, fig=TRUE, width=6, height=2, include=FALSE>>=
4035
grid.xaxis(at = 1:4/5, vp = viewport(w = .5, h = 0.01), name = "gxa")
4136
@
4237
\begin{center}
38+
{
4339
\includegraphics[width=3in, height=1in]{interactive-fig1}
40+
}
4441
\end{center}
42+
@
4543

46-
\section*{Editing Grobs}
47-
Next, we edit the axis by changing its colour to red. The grob is
48-
referenced by its assigned \code{name}.
44+
Now we edit the axis, changing the colour of the entire axis to red.
45+
Notice that we refer to the x-axis by its name.
4946

5047
<<edit1, eval=FALSE>>=
5148
grid.edit("gxa", gp = gpar(col = "red"))
@@ -55,12 +52,17 @@ gxa <- editGrob(gxa, gp = gpar(col = "red"))
5552
grid.draw(gxa)
5653
@
5754
\begin{center}
55+
{
5856
\includegraphics[width=3in, height=1in]{interactive-fig2}
57+
}
5958
\end{center}
6059

61-
We can also edit specific components of a grob. For example, changing
62-
only the labels to green. The \code{gPath()} function is recommended
63-
for reusable code because it avoids hard‑coding grob names.
60+
@
61+
62+
Now we change just the labels of the x-axis to green. We use the
63+
\code{gPath()} function to concatenate the grob names (we could have used
64+
\code{"gxa::labels"} here, but \code{gPath()} is recommended for
65+
writing code that will be reused).
6466

6567
<<edit2, eval=FALSE>>=
6668
grid.edit(gPath("gxa", "labels"), gp = gpar(col = "green"))
@@ -71,12 +73,18 @@ gxa <- editGrob(gxa, gPath = "labels", gp = gpar(col = "green"))
7173
grid.draw(gxa)
7274
@
7375
\begin{center}
76+
{
7477
\includegraphics[width=3in, height=1in]{interactive-fig3}
78+
}
7579
\end{center}
80+
@
7681

77-
Editing can also change structural aspects, such as the number of tick
78-
marks. When new labels are created, they inherit the default colour of
79-
the axis, replacing previous label edits.
82+
It is also possible to change the number of tick marks.
83+
Notice that the labels all change back to red; this happens
84+
because new labels are created by the axis and these ``inherit''
85+
the colour of the axis by default. In other words, the
86+
colour specification of the old labels was specific to the old labels
87+
and was discarded when the old labels were discarded.
8088

8189
<<edit3, eval=FALSE>>=
8290
grid.edit("gxa", at = c(0.0, 0.5, 1.0))
@@ -88,10 +96,13 @@ gxa <- editGrob(gxa, at = c(0.0, 0.5, 1.0))
8896
grid.draw(gxa)
8997
@
9098
\begin{center}
99+
{
91100
\includegraphics[width=3in, height=1in]{interactive-fig4}
101+
}
92102
\end{center}
103+
@
93104

94-
Finally, we change the labels back to black and rotate them by $30^\circ$.
105+
Finally, we change the labels back to black and rotate them all $30\deg$.
95106

96107
<<edit4, eval=FALSE>>=
97108
grid.edit("gxa::labels", gp = gpar(col = "black"), rot = 30)
@@ -104,14 +115,16 @@ gxa <- editGrob(gxa, gPath = "labels", gp = gpar(col = "black"), rot = 30)
104115
grid.draw(gxa)
105116
@
106117
\begin{center}
118+
{
107119
\includegraphics[width=3in, height=1in]{interactive-fig5}
120+
}
108121
\end{center}
122+
@
109123

110-
\section*{Off‑Screen Editing}
111-
All of the above examples describe editing grobs and updating them on
112-
screen. Equivalent operations can be performed entirely off‑screen by
113-
working with grob objects directly. This approach is useful for
114-
reproducibility and scripted graphics pipelines.
124+
The above examples describe how to perform editing on a grid object
125+
and have the changes updated on screen. The equivalent can be done
126+
entirely ``off-screen'', by just working with the grid object.
127+
The off-screen equivalent in this case would look like:
115128

116129
<<results=hide>>=
117130
gxa <- xaxisGrob(at = 1:4/5, vp = viewport(w = .5, h = .01))
@@ -121,5 +134,4 @@ gxa <- editGrob(gxa, at = c(0.0, 0.5, 1.0))
121134
gxa <- editGrob(gxa, gPath = "labels", gp = gpar(col = "black"), rot = 30)
122135
grid.draw(gxa)
123136
@
124-
125-
\end{document}
137+
\end{document}
Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
%\VignetteEncoding{UTF-8}
22
% File src/library/grid/vignettes/locndimn.Rnw
33
% Part of the R package, https://www.R-project.org
4-
% Copyright 2001–2026 Paul Murrell and the R Core Team
4+
% Copyright 2001-13 Paul Murrell and the R Core Team
55
% Distributed under GPL 2 or later
66

77
\documentclass[a4paper]{article}
88
%\VignetteIndexEntry{Locations versus Dimensions}
99
%\VignettePackage{grid}
10-
1110
\setlength{\parindent}{0in}
1211
\setlength{\parskip}{.1in}
1312
\setlength{\textwidth}{140mm}
@@ -17,10 +16,11 @@
1716
\newcommand{\pkg}[1]{{\normalfont\fontseries{b}\selectfont #1}}
1817
\newcommand{\grid}{\pkg{grid}}
1918

20-
\title{The Difference Between Locations and Dimensions in \grid{}}
19+
\title{The difference between Locations and Dimensions in \grid{}}
2120
\author{Paul Murrell}
2221

2322
\begin{document}
23+
2424
\maketitle
2525

2626
<<echo=FALSE, results=hide>>=
@@ -29,31 +29,31 @@ library(grid)
2929
ps.options(pointsize = 12)
3030
options(width = 60)
3131
@
32-
33-
\section*{Introduction}
34-
The \grid{} package makes use of unit objects to express both the
35-
locations and dimensions of graphical components. For example,
36-
\code{grid.rect()} interprets its \code{x} and \code{y} arguments as
37-
locations within the current viewport, while its \code{width} and
38-
\code{height} arguments are treated as dimensions.
39-
40-
These interpretations are usually implicit: if the argument is an
41-
\code{x} or \code{y} value, it represents a location; if it is a
42-
\code{width} or \code{height} value, it represents a dimension.
43-
44-
The distinction matters because, in some coordinate systems —
45-
notably \code{"native"} coordinates — a location value can have a
46-
different meaning from a dimension value. This occurs whenever the
47-
minimum value of the coordinate system is not zero.
48-
49-
In simple unit specifications, the difference may not be noticeable.
50-
However, there are two areas where the distinction is critical.
51-
52-
\section*{1. Arithmetic on Units}
53-
When adding or performing arithmetic operations on units, it is
54-
important to remember that locations behave like vectors, while
55-
dimensions behave like lengths. The following diagram demonstrates
56-
the difference:
32+
\grid{} makes use of unit objects to express both the locations and
33+
dimensions of graphical components. For example, the \code{grid.rect()}
34+
function treats its x- and y-arguments as locations within the current
35+
viewport, and its width- and height-arguments as dimensions within the
36+
current viewport.
37+
38+
These different interpretations of units are usually implicit like this;
39+
if its an x- or y-value then its a location, if its a width- or height-value
40+
then its a dimension.
41+
42+
The distinction is made at all because, in some coordinate systems, notably
43+
\code{"native"} coordinates, the location $x$ can have a very different
44+
meaning
45+
from the dimension $x$ -- basically, whenever the minimum value of the
46+
coordinate system is not zero.
47+
48+
In the specification of simple units, the difference between locations
49+
and dimensions is often not noticeable. However, there are a couple
50+
of tricky areas:
51+
52+
\begin{enumerate}
53+
\item When adding (or performing any arithmetic operation on) units,
54+
it is important to keep in mind that locations are added like vectors
55+
and dimensions are added like lengths. The following
56+
diagram demonstrates the difference:
5757

5858
<<fig=TRUE, echo=FALSE, results=hide>>=
5959
diagram.locn <- function(i, n, locn) {
@@ -96,28 +96,28 @@ diagram.dimn(8, n, unit(0.4, "native"))
9696
diagram.dimn(9, n, unit(1, "native") + unit(-0.4, "native"))
9797
diagram.dimn(10, n, unit(1, "native") - unit(0.4, "native"))
9898
@
99-
100-
\section*{2. Converting Units}
101-
The functions \code{convertX}, \code{convertY}, \code{convertWidth},
102-
and \code{convertHeight} are used to convert between coordinate
103-
systems. It is essential to distinguish whether the conversion is for
104-
a location or for a dimension.
105-
106-
The following code demonstrates the difference:
99+
\item The functions \code{convertX},
100+
\code{convertY}, \code{convertWidth}, \code{convertHeight}
101+
are used to convert from one coordinate system to another.
102+
Again, it is important whether the conversion is for a location or
103+
for a dimension.
104+
The following code
105+
demonstrates some results from these functions based on a similar
106+
situation to that in the preceding diagram:
107107

108108
<<fig=FALSE>>=
109109
pushViewport(viewport(yscale = c(-0.6, 1.3)))
110-
# Location conversions
110+
# Unexpected results?
111111
convertY(unit(1,'native'), "native")
112112
convertY(unit(-.4,'native'), "native")
113113
convertY(unit(1,'native')+unit(-.4,'native'), "native")
114114
convertY(unit(1,'native')-unit(.4,'native'), "native")
115-
# Dimension conversions
115+
# Expected results
116116
convertHeight(unit(1,'native'), "native")
117117
convertHeight(unit(-.4,'native'), "native")
118118
convertHeight(unit(1,'native')+unit(-.4,'native'), "native")
119119
convertHeight(unit(1,'native')-unit(.4,'native'), "native")
120120
popViewport()
121121
@
122-
123-
\end{document}
122+
\end{enumerate}
123+
\end{document}

src/library/grid/vignettes/moveline.Rnw

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
%\VignetteEncoding{UTF-8}
22
% File src/library/grid/vignettes/moveline.Rnw
33
% Part of the R package, https://www.R-project.org
4-
% Copyright 2001–2026 Paul Murrell and the R Core Team
4+
% Copyright 2001-13 Paul Murrell and the R Core Team
55
% Distributed under GPL 2 or later
66

77
\documentclass[a4paper]{article}
88
%\VignetteIndexEntry{Demonstrating move-to and line-to}
99
%\VignettePackage{grid}
10-
1110
\newcommand{\code}[1]{\texttt{#1}}
1211
\newcommand{\pkg}[1]{{\normalfont\fontseries{b}\selectfont #1}}
1312
\newcommand{\grid}{\pkg{grid}}
1413
\newcommand{\R}{{\sffamily R}}
15-
1614
\setlength{\parindent}{0in}
1715
\setlength{\parskip}{.1in}
1816
\setlength{\textwidth}{140mm}
1917
\setlength{\oddsidemargin}{10mm}
20-
2118
\title{Drawing Lines Between Viewports: \\
2219
\code{grid.move.to} and \code{grid.line.to}}
2320
\author{Paul Murrell}
@@ -32,17 +29,17 @@ ps.options(pointsize = 12)
3229
options(width = 60)
3330
@
3431

35-
\section*{Introduction}
36-
Many graphics systems provide the concept of a current drawing
37-
location. This functionality has been added to \grid{}, with the
38-
additional benefit that the drawing location can be specified relative
39-
to an arbitrary viewport. This allows drawing operations to span
40-
multiple coordinate systems.
32+
Many graphics systems have the notion of a current drawing location.
33+
This has been added to Grid, with the additional benefit that the
34+
drawing location can be specified relative to an arbitrary viewport.
35+
This allows drawing across coordinate
36+
systems\footnote{There was a request on R-help for exactly this
37+
sort of thing - I will try to find the exact reference - and there has
38+
since been another (From: Ross Darnell; Subject: Problems with
39+
segments and multiple graphs). }.
4140

42-
\section*{Example}
43-
The following code demonstrates a simple example of moving to a
44-
location in one viewport and drawing a line to a location in another
45-
viewport. The output is shown below.
41+
The following code demonstrates a simple example (the output is
42+
given after the code).
4643

4744
<<fig, results=hide, fig=TRUE, width=5, height=4, include=FALSE>>=
4845
pushViewport(
@@ -61,9 +58,10 @@ grid.points(0.5, unit(2, "native"))
6158
grid.line.to(0.5, unit(2,"native"))
6259
@
6360
\begin{center}
61+
{
6462
\includegraphics[width=5in, height=4in]{moveline-fig}
63+
}
6564
\end{center}
6665

66+
@
6767
\end{document}
68-
69-

0 commit comments

Comments
 (0)