在ggplot2中获取绘图区域的宽度 [英] Get width of plot area in ggplot2

查看:133
本文介绍了在ggplot2中获取绘图区域的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在网格窗口中获得绘图区域的宽度?例如,如果 plot.margin 发生更改,或者y轴标签的字体大小增加,它会增大或缩小。是隐藏在 str(p)



的任何位置。我需要能够在不同场景下测量绘图区域宽度的相对变化,例如更改y轴标签的字体大小。

 <$ c $ (13,7,11),3 = c(11,9,11))$ df = data.frame(x =(1:3) b $ b df.melt = melt(df,id.vars =x)

p = ggplot(df.melt,aes(x = x,y = value,color = variable))+
geom_line()+
coord_cartesian(xlim = c(min(df.melt $ x),max(df.melt $ x)))+
theme(legend.position =none ,plot.margin = unit(c(1,4,1,1),cm))
p






更新 - 澄清:请帮我计算 a / b



  p = ggplot(df.melt,aes(x = x,y = value,color = variable))+ 
geom_line()+ coord_cartesian(xlim = c (分钟(df.melt $ X),MAX(DF .melt $ x)))+
theme(legend.position =none)

p1 = p + theme(plot.margin = unit(c(1,1,1, 1),cm),axis.text.y = element_text(size = 10))
p2 = p + theme(plot.margin = unit(c(1,1,1,2),cm ),axis.text.y = element_text(size = 30))
grid.arrange(p1,p2,ncol = 2)


解决方案

这足以让我更深入地研究它。我希望 grid.ls 函数可以让信息导航到正确的视口以获取信息,但对于您的示例,有一些步骤可以获得替换为'...',我看不出如何改变这种情况,以提供一些易于使用的东西。但是使用> grid.ls 或其他工具可以看到不同视口的名称。感兴趣的视口都以您的示例命名为panel.3-4-3-4,下面是一些代码,将导航到1st,以英寸为单位查找宽度,导航到第二个并查找该宽度英寸。

  grid.ls(view = TRUE,grob = FALSE)
current.vpTree()
seekViewport('panel.3-4-3-4')
a < - convertWidth(unit(1,'npc'),'inch',TRUE)
popViewport(1)
seekViewport('panel.3-4-3-4')
b< - convertWidth(unit(1,'npc'),'inch',TRUE)
a / b

我找不到一个简单的方法来到第二个面板而不弹出第一个面板。这可以工作并提供您需要的信息,不幸的是,因为它将第一个面板从列表中弹出,您不能返回该面板并查找其他信息或进行修改。但是,这确实会提供您要求的信息,可以在未来的情节中使用。可能有人知道如何导航到第二个面板,而不会弹出第一个面板,或者获取他们每个人的完整vpPath直接导航。


Is there any way to get the width of the plot area in the grid window? It grows or shrinks, for instance, if plot.margin is changed or if the y-axis labels' font-size is increased. Is is hidden somewhere in str(p)?

Any size measure would work. I need to be able to measure the relative change in the width of the plot area in different scenarios such as change of y-axis labels' font-size.

df = data.frame(x = (1:3),One=c(12, 8, 13),Two=c(13, 7, 11),Three=c(11, 9, 11))
df.melt = melt(df, id.vars="x")

p = ggplot(df.melt, aes(x=x, y=value, color=variable)) + 
  geom_line() +
  coord_cartesian(xlim=c(min(df.melt$x),max(df.melt$x))) +
  theme(legend.position="none", plot.margin = unit(c(1, 4, 1, 1), "cm")) 
p


UPDATE – To clarify: Please help me calculate a/b.

p = ggplot(df.melt, aes(x=x, y=value, color=variable)) + 
  geom_line() + coord_cartesian(xlim=c(min(df.melt$x),max(df.melt$x))) +
  theme(legend.position="none")

p1 = p + theme(plot.margin=unit(c(1,1,1,1),"cm"), axis.text.y=element_text(size=10))
p2 = p + theme(plot.margin=unit(c(1,1,1,2),"cm"), axis.text.y=element_text(size=30))
grid.arrange(p1, p2, ncol=2)

解决方案

This intrigued me enough to look into it deeper. I was hoping that the grid.ls function would give the information to navigate to the correct viewports to get the information, but for your example there are a bunch of the steps that get replaced with '...' and I could not see how to change that to give something that is easily worked with. However using grid.ls or other tools you can see the names of the different viewports. The viewports of interest are both named 'panel.3-4-3-4' for your example, below is some code that will navigate to the 1st, find the width in inches, navigate to the second and find the width of that one in inches.

grid.ls(view=TRUE,grob=FALSE)
current.vpTree()
seekViewport('panel.3-4-3-4')
a <- convertWidth(unit(1,'npc'), 'inch', TRUE)
popViewport(1)
seekViewport('panel.3-4-3-4')
b <- convertWidth(unit(1,'npc'), 'inch', TRUE)
a/b

I could not figure out an easy way to get to the second panel without poping the first one. This works and gives the information that you need, unfortunately since it pops the 1st panel off the list you cannot go back to it and find additional information or modify it. But this does give the info you asked for that could be used in future plots.

Maybe someone else knows how to navigate to the second panel without popping the first, or getting the full vpPath of each of them to navigate directly.

这篇关于在ggplot2中获取绘图区域的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆