[R] adding line to spinogram/histogram/etc solved
Viktor Tron
v.tron at ed.ac.uk
Wed May 17 02:56:02 CEST 2006
Thanks loads, extremely useful.
So just for the record again.
You have to get into the viewport that contains the actual graphplot with
the axes.
So, how you do it:
1. viewports have names.
In case of histograms the relevant viewport is something like
"plotA.panel.X.Y.vp"
where A is the plot index and X, Y are the x, y indexes of the panel (as
usual, starting from bottom left)
but you can try out the other names shown by the
current.vpTree()
command in case in trouble.
In case of spinograms you have to use the pop=FALSE option for the
graph viewport inside the root to be retained.
Then it is just called "spinelot".
2. Once you figured out the name of the viewport you want to add plot to,
issue
seekViewport("spineplot")
to get the "focus" on the viewport and then you can use the grid primitives
the same way as you use lines, segments on normal plots.
3. If you want to use the scale of the axes (natrually what you want)
you have to tell grid to use the NATIVE scale of the viewport
(default.units="native").
So something like this will do:
grid.segments(,100,,100,gp=gpar(col="red"),default.units="native")
to add a "100 items cut-off line" across a "count" type histogram.
(Note the left out parameters which resolve to start/end as x1 and x2)
Brilliant, cheers Achim.
V
On Tue, 16 May 2006 19:09:14 +0100, Achim Zeileis
<Achim.Zeileis at wu-wien.ac.at> wrote:
> On Tue, 16 May 2006 17:42:22 +0100 Viktor Tron wrote:
>
>> Hello,
>> Thanks for the hint.
>> grid.segments seemed the closest I got.
>> I did manage to draw (well fake) a line with it. I can only address
>> the whole drawing frame, which means I can only adjust the position
>> and length of the line
>> by trial and error. I see no way to address the y axis scale of my
>> spinogram/histogram.
>> Is there a way?
>
> Yes, that's the wonderful thing about grid!
>
> Consider this example with data from vcd
> spine(Fail ~ Temperature, data = SpaceShuttle)
> Then you can look at the viewport tree in which you can navigate:
> current.vpTree()
> which leaves you here only with the ROOT node, hence you had troubles
> adjusting your lines. But looking at ?spine reveals that
> spine(Fail ~ Temperature, data = SpaceShuttle, pop = FALSE)
> does *not* pop away the viewport tree which is here relatively simple
> current.vpTree()
> just shows "viewport[ROOT]->(viewport[spineplot])".
>
> So you can hop into the main picture
> seekViewport("spineplot")
> (which you can also name differently) and do more or less sensible
> things, e.g.
> grid.rect(gp = gpar(col = 2))
> adds a red box around the plot or
> grid.lines(c(0, 1), c(0.3, 0.7), gp = gpar(col = 4))
> adds a blue line. Note that both x- and y-axis are on a probability
> scale, i.e., it plots P(Temperature <= x) vs. P(Fail = "no").
>
> To see a more elaborated example how these graphics can be re-used,
> look at example(mob) in library("party").
>
> Best,
> Z
>
>> Not a huge problem, but I thought someone must have thought of
>> adding lines to their spinograms or histograms before...
>> V
>>
>>
>> On Mon, 15 May 2006 14:13:00 +0100, Prof Brian Ripley
>> <ripley at stats.ox.ac.uk> wrote:
>>
>> > Package vcd is built on grid, not base graphics.
>> >
>> > On Mon, 15 May 2006, Viktor Tron wrote:
>> >
>> >> Dear all,
>> >> I wonder what's special about spinograms {vcd} that prevents me
>> >> from using
>> >> it the way I do with other plots.
>> >>
>> >> I do:
>> >>
>> >>> spine(f.speaker.identity ~ x.log.lengthening,
>> >>> data=ms,breaks=45,gp=gpar(fill=c("red","green")),xlab="length
>> >>> difference
>> >>> (log ms)",ylab="speaker")
>> >>> curve(0*x,add=T)
>> >> Error in plot.xy(xy.coords(x, y), type = type, col = col, lty =
>> >> lty, ...) :
>> >> plot.new has not been called yet
>> >>
>> >>
>> >> OK, if I do
>> >>> curve(0*x,add=)
>> >>> spine(f.speaker.identity ~ x.log.lengthening,
>> >>> data=ms,breaks=45,gp=gpar(fill=c("red","green")),xlab="length
>> >>> difference
>> >>> (log ms)",ylab="speaker")
>> >>> curve(0*x,add=T)
>> >>
>> >> then the plot is what I want, but note that I had to use y=0 to
>> >> get the line put at 0.5!!!! so it is already suspicious.
>> >> But then:
>> >>
>> >>> dev.print(pdf,"mde_speakerration_by_lengthening.pdf")
>> >> Error in dev.copy(device = function (file = ifelse(onefile,
>> >> "Rplots.pdf",
>> >> :
>> >> invalid graphics state
>> >>
>> >> Can anyone suggest a remedy?
>> >
>> > Use grid primitives to add to the plot.
>> >
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide!
>> http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list