Luigi Marongiu
2021-Oct-12 20:42 UTC
[R] How to rotate only one panel by 90 degrees in R plot?
Hello,
I would like to show a density plot of the Y axis. To do that, I would
like to split the plot into a panel 2/3 long and a density plot 1/3
long. The problem is that, since the density is on the Y axis, the
density plot should be rotated byb90 degrees. I tried with the package
gridGraphics but it rotates both panels.
```
negative_y <- runif(50, 0, 0.099)
negative_x <- runif(50, 1, 40)
positive_y <- c(runif(30, 0.2, 0.5), runif(20, 0.4, 0.5))
positive_x <- c(runif(30, 25, 40), runif(20, 10, 25))
uncertain_y <- runif(10, 0.099, 0.2)
uncertain_x <- runif(10, 2, 40)
# plot on MR/FCN space
layout(matrix(c(1,2),nrow=1), widths=c(3,1)) # split panels unevenly
plot(negative_x, negative_y, ylim=c(0,0.5), xlim=c(0,41), cex=1.5,
xlab=expression(bold("X")),
ylab=expression(bold("Y")))
points(positive_x, positive_y, pch=16, cex=1.5)
points(uncertain_x, uncertain_y, pch=16, cex=1.5, col="grey")
legend("topleft",
legend = c("Positives", "Negatives",
"Uncertains"),
pch = c(16, 1, 16), col=c("black", "black",
"grey"), cex=0.8)
# plot density
plot(density(c(negative_y, uncertain_y, positive_y)),
yaxt="n", xaxt="n", main=NA, ylab=NA, xlab=NA)
library(gridGraphics)
grab_grob <- function(){
grid.echo()
grid.grab()
}
g <- grab_grob()
grid.newpage()
pushViewport(viewport(width=0.7,angle=270))
grid.draw(g)
```
How can I rotate only the second panel? I tried to assign the second
plot to an object p and then call grid.draw(p), or to assign g to the
second plot, but they did not work...
Thanks
--
Best regards,
Luigi
Bert Gunter
2021-Oct-12 23:17 UTC
[R] How to rotate only one panel by 90 degrees in R plot?
I don't know the gridGraphics package, and I haven't looked closely at
what
you are trying to do. But note that lattice functions construct grid
"grobs" that can be saved and plotted in arbitrary, including rotated,
viewports, using the print.trellis function. I frankly am pretty ignorant
about such things, but this simple little example might give you some
notion of how to proceed. You may also be able to do what you want with
grid.layout() and pushing a suitably rotated viewport onto a layout. Others
would have to advise on such details, if so.
If I'm wrong and this is useless, just ignore without comment.
dp <- densityplot(~y, main = "",
xlab = "", ylab = "")
grid.newpage()
pushViewport(
viewport(width = unit(.5,"npc"),
height = unit(.3,"npc"),
angle = 270))
print(dp, newp = FALSE, ## this is the print.trellis method
panel.width = list(1,"npc"),
panel.height = list(1, "npc")
)
Bert Gunter
"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Tue, Oct 12, 2021 at 1:43 PM Luigi Marongiu <marongiu.luigi at
gmail.com>
wrote:
> Hello,
> I would like to show a density plot of the Y axis. To do that, I would
> like to split the plot into a panel 2/3 long and a density plot 1/3
> long. The problem is that, since the density is on the Y axis, the
> density plot should be rotated byb90 degrees. I tried with the package
> gridGraphics but it rotates both panels.
> ```
> negative_y <- runif(50, 0, 0.099)
> negative_x <- runif(50, 1, 40)
> positive_y <- c(runif(30, 0.2, 0.5), runif(20, 0.4, 0.5))
> positive_x <- c(runif(30, 25, 40), runif(20, 10, 25))
> uncertain_y <- runif(10, 0.099, 0.2)
> uncertain_x <- runif(10, 2, 40)
> # plot on MR/FCN space
> layout(matrix(c(1,2),nrow=1), widths=c(3,1)) # split panels unevenly
> plot(negative_x, negative_y, ylim=c(0,0.5), xlim=c(0,41), cex=1.5,
> xlab=expression(bold("X")),
> ylab=expression(bold("Y")))
> points(positive_x, positive_y, pch=16, cex=1.5)
> points(uncertain_x, uncertain_y, pch=16, cex=1.5, col="grey")
> legend("topleft",
> legend = c("Positives", "Negatives",
"Uncertains"),
> pch = c(16, 1, 16), col=c("black", "black",
"grey"), cex=0.8)
> # plot density
> plot(density(c(negative_y, uncertain_y, positive_y)),
> yaxt="n", xaxt="n", main=NA, ylab=NA, xlab=NA)
> library(gridGraphics)
> grab_grob <- function(){
> grid.echo()
> grid.grab()
> }
> g <- grab_grob()
> grid.newpage()
> pushViewport(viewport(width=0.7,angle=270))
> grid.draw(g)
> ```
> How can I rotate only the second panel? I tried to assign the second
> plot to an object p and then call grid.draw(p), or to assign g to the
> second plot, but they did not work...
> Thanks
>
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
Paul Murrell
2021-Oct-12 23:29 UTC
[R] How to rotate only one panel by 90 degrees in R plot?
Hi
I don't think you need (or want) the 'gridGraphics' package for
this.
You just need to flip the x-/y-data that you get from density() (and
make sure that you align the y-axes of the two plots).
The following code shows an example (and adds a bit of par() control to
eliminate white space).
layout(matrix(c(1,2),nrow=1), widths=c(3,1))
par(mar=c(5, 4, 2, 1))
plot(negative_x, negative_y, ylim=c(0,0.5), xlim=c(0,41), cex=1.5,
xlab=expression(bold("X")),
ylab=expression(bold("Y")))
points(positive_x, positive_y, pch=16, cex=1.5)
points(uncertain_x, uncertain_y, pch=16, cex=1.5, col="grey")
legend("topleft",
legend = c("Positives", "Negatives",
"Uncertains"),
pch = c(16, 1, 16), col=c("black", "black",
"grey"), cex=0.8)
# plot density
d <- density(c(negative_y, uncertain_y, positive_y))
par(mar=c(5, 1, 2, 1))
plot(d$y, d$x, ylim=c(0,0.5), type="l",
yaxt="n", xaxt="n", main=NA, ylab=NA, xlab=NA)
Does that produce the sort of thing you want?
Paul
On 13/10/2021 9:42 am, Luigi Marongiu wrote:> Hello,
> I would like to show a density plot of the Y axis. To do that, I would
> like to split the plot into a panel 2/3 long and a density plot 1/3
> long. The problem is that, since the density is on the Y axis, the
> density plot should be rotated byb90 degrees. I tried with the package
> gridGraphics but it rotates both panels.
> ```
> negative_y <- runif(50, 0, 0.099)
> negative_x <- runif(50, 1, 40)
> positive_y <- c(runif(30, 0.2, 0.5), runif(20, 0.4, 0.5))
> positive_x <- c(runif(30, 25, 40), runif(20, 10, 25))
> uncertain_y <- runif(10, 0.099, 0.2)
> uncertain_x <- runif(10, 2, 40)
> # plot on MR/FCN space
> layout(matrix(c(1,2),nrow=1), widths=c(3,1)) # split panels unevenly
> plot(negative_x, negative_y, ylim=c(0,0.5), xlim=c(0,41), cex=1.5,
> xlab=expression(bold("X")),
> ylab=expression(bold("Y")))
> points(positive_x, positive_y, pch=16, cex=1.5)
> points(uncertain_x, uncertain_y, pch=16, cex=1.5, col="grey")
> legend("topleft",
> legend = c("Positives", "Negatives",
"Uncertains"),
> pch = c(16, 1, 16), col=c("black", "black",
"grey"), cex=0.8)
> # plot density
> plot(density(c(negative_y, uncertain_y, positive_y)),
> yaxt="n", xaxt="n", main=NA, ylab=NA, xlab=NA)
> library(gridGraphics)
> grab_grob <- function(){
> grid.echo()
> grid.grab()
> }
> g <- grab_grob()
> grid.newpage()
> pushViewport(viewport(width=0.7,angle=270))
> grid.draw(g)
> ```
> How can I rotate only the second panel? I tried to assign the second
> plot to an object p and then call grid.draw(p), or to assign g to the
> second plot, but they did not work...
> Thanks
>
>
> --
> Best regards,
> Luigi
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> <https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> <http://www.R-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
--
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/