我正在尝试旋转绘图以将其合并到闪亮的应用程序中。 我找到了如何旋转绘图。为了让它正确,你似乎需要摆弄
height
的 width
和 viewport
,使其不是一个你事先不知道 height
和 width
的动态环境的解决方案.
我现在的问题是如何旋转我的绘图,使其保持动态并响应绘图区域的变化?
我的例子:
p <- ggplot2::qplot(1:10)
library(grid)
grid.newpage()
print(p, vp=viewport(0, 0, width = unit(0.5, "npc"), just = c('left', 'bottom')))
print(p, vp=viewport(0.5, 0, angle = 90, height = unit(0.8, "npc"), width = 0.55, just = c('left', 'top')))
取决于绘图区域,它看起来好还是坏:
观察到:缺少一半的旋转图
预期:
根据 OP 的 cpmment 进行编辑。
每次绘制图形时,我们都需要动态检查图形设备的尺寸。
library(grid)
grid.newpage()
dev.size(units = "px")
print(p, vp=viewport(0, 0,
width = unit(dev.size(units = "in")[1]*0.45, "inches"),
height = unit(dev.size(units = "in")[2]*0.975, "inches"),
just = c('left', 'bottom')))
print(p, vp=viewport(0.5, 0, angle = 90,
height = unit(dev.size(units = "in")[1]*0.45, "inches"),
width = unit(dev.size(units = "in")[2]*0.975, "inches"),
just = c('left', 'top')))
请注意,每当您在 rstudio 设备窗口中拉伸图形时,它都不会影响图形的更改,但它每次都会根据现有设备的大小更改大小。