R 通过右侧面板微调绘图大小和图例位置
我喜欢在一个文档中包含ggplots,该文档的主文本区域(一列)的宽度为130 mm,然后是右侧7 mm的边距和一个用于注释的区域(例如LaTexR 通过右侧面板微调绘图大小和图例位置,r,ggplot2,R,Ggplot2,我喜欢在一个文档中包含ggplots,该文档的主文本区域(一列)的宽度为130 mm,然后是右侧7 mm的边距和一个用于注释的区域(例如LaTex\mpar{})。图例应放置在注释区域中。我面临的问题是,面板的右边缘在此设置中很重要,因为它定义了主要内容的结尾(130mm)和图例的开头(+7mm)。我知道大小调整选项ggsave()以及使用theme()定位图例。但是,我不完全理解定位和尺寸(提供一些帮助) 是否有一种方法可以更详细地访问尺寸,或者另一个绘图包更合适 相关尺寸为红色的绘图示例(取
\mpar{}
)。图例应放置在注释区域中。我面临的问题是,面板的右边缘在此设置中很重要,因为它定义了主要内容的结尾(130mm)和图例的开头(+7mm)。我知道大小调整选项ggsave()
以及使用theme()
定位图例。但是,我不完全理解定位和尺寸(提供一些帮助)
是否有一种方法可以更详细地访问尺寸,或者另一个绘图包更合适
相关尺寸为红色的绘图示例(取自教程):
即使是单面板打印,也有点麻烦,但通过一些工作,您可以计算出正确的大小和视口
library(ggplot2)
library(grid)
p <- qplot(1,1,color=1) + theme(plot.background = element_rect(fill=NA))
g <- ggplotGrob(p)
# get the width of guide and stuff on the right
id_guide <- g$layout$r[grep('guide',g$layout$name)]
gw <- convertWidth(sum(g$widths[id_guide:ncol(g)]), 'cm')
# make a new gtable with fixed panel width
g_fixed <- g
id_panel <- g$layout$l[grep('panel',g$layout$name)]
g_fixed$widths[id_panel] <- unit(130,'mm') - sum(g$widths[-(id_panel:ncol(g))])
grid.newpage()
# viewport smaller than device for illustration
vp <- viewport(width=unit(130,'mm'), height=0.8)
pushViewport(vp=vp)
grid.rect(gp=gpar(fill='gold',lty=2))
# within the current viewport, place fixed grob
g_fixed$vp <- viewport(x=0,width=unit(130,'mm')+gw, just = 'left')
grid.draw(g_fixed)
库(ggplot2)
图书馆(网格)
p即使是单面板打印,也有点麻烦,但通过一些工作,您可以计算出正确的大小和视口
library(ggplot2)
library(grid)
p <- qplot(1,1,color=1) + theme(plot.background = element_rect(fill=NA))
g <- ggplotGrob(p)
# get the width of guide and stuff on the right
id_guide <- g$layout$r[grep('guide',g$layout$name)]
gw <- convertWidth(sum(g$widths[id_guide:ncol(g)]), 'cm')
# make a new gtable with fixed panel width
g_fixed <- g
id_panel <- g$layout$l[grep('panel',g$layout$name)]
g_fixed$widths[id_panel] <- unit(130,'mm') - sum(g$widths[-(id_panel:ncol(g))])
grid.newpage()
# viewport smaller than device for illustration
vp <- viewport(width=unit(130,'mm'), height=0.8)
pushViewport(vp=vp)
grid.rect(gp=gpar(fill='gold',lty=2))
# within the current viewport, place fixed grob
g_fixed$vp <- viewport(x=0,width=unit(130,'mm')+gw, just = 'left')
grid.draw(g_fixed)
库(ggplot2)
图书馆(网格)
P