R ggplot2刻面保持比例但覆盖/定义输出绘图大小 [英] R ggplot2 facetting keep ratio but override/define output plot size
问题描述
我目前使用ggplot2来比较来自不同组别的统计资料,每组属于不同的地区。这是通过运行R脚本的Web应用程序(tikiwiki CMS +插件R)完成的。每个地区我可以有2到30个或更多的团体。同一个R脚本运行于独特网页中的所有数据,结果根据所选区域进行调整 - 作为页面的参数。
目前,我有这个:
区域1:12组= 12个中等分面
区域2:3组= 3 **巨大**方面
区域3:24组= 24 ** TINY **方面
区域4:16组= 16个中等方面
...
区域2 Region 3 b 区域2 地区3 我用facet_wra p(〜data,ncol = 4),所以我可以有2个或30个以上的方面。我的问题是输出:要么将30个面填充到一个小盒子中,要么将两个面放在相同大小的盒子中 - >请参阅上面的图片...我无法找到如何修复比例(或图像输出最大宽度),但保留最终的图片尺寸。 在ggplot2 / R中,是否可以修复facet的默认大小,并让结果图片的大小适应facet的数量? 如果在ggplot2 / R中不可行,那么是否有任何可以获取代码并正确显示结果的javascript / jquery库(d3,...)? 您可以编辑gtable设置高度为物理单位(例如cm)而不是相对(null)。 编辑作为后续,这里有一个函数来设置所有面板的高度和宽度为固定值, I'm currently using ggplot2 to compare statistics from different groups, each belonging to a different region. This is done via a web application (tikiwiki CMS + plugin R), who runs the R script. Per region I can have 2 to 30 or more groups. A same R script runs for all the data in a unique web page, the result adapting depending on the chosen region - as parameter of the page. Currently, I have this : Region 2 Region 3 Region 2 Region 3
Stays the same I'm facetting per group with facet_wrap(~data, ncol=4), so I can have either 2 or 30+ facets. My problem is the output: either the 30 facets are stuffed into a tiny box, or the two ones are oversized in the same sized box -> See pictures above... I can't find how to fix a ratio (or image output max width) but keep the final picture size free. Is it possible in ggplot2 / R to fix a default size for the facets and to have the size of the resulting picture adapting to the numbers of facets ? If this is not possible in ggplot2 / R is there any javascript/jquery library that can pick up the code and show the results correctly (d3, ...) ? you can edit the gtable setting the heights to physical units (e.g. cm) instead of relative ("null") Edit As a follow-up, here's a function to set the height and width of all panels to a fixed value,
这篇关于R ggplot2刻面保持比例但覆盖/定义输出绘图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b
我希望得到什么样的结果是:
保持不变
$ b
require(ggplot2)
p = qplot(Sepal.Width,Sepal.Length,data = iris)+ facet_wrap(〜Species,ncol = 1 )
g = ggplotGrob(p)
panels = which(sapply(g [[heights]],attr,unit)==null)
g [[heights]] [panels] = list(unit(4,cm),unit(8,cm),unit(2,cm))
device .height = convertHeight(sum(g [[heights]]),in,valueOnly = TRUE)
pdf(test.pdf,height = device.height)
grid.draw(g)
dev.off()
freeze_panels< ;函数(p,draw = TRUE,
width = unit(5,cm),
height = unit(1,in)){
require(grid)
g < - ggplotGrob(p)
vertical_panels< -which(sapply(g [[heights]],attr,unit)==null)
horizontal_panels< -which(sapply(g [[widths]],attr,unit)==null)
g [[heights]] [ vertical_panels]< - replicate(length(vertical_panels),
height,simplify = FALSE)
g [[widths]] [horizontal_panels]< - replicate(length(horizontal_panels),
width,simplify = FALSE)
device.height< - convertHeight(sum(g [[ (),in,valueOnly = TRUE)
device.width< - convertWidth(sum(g [[widths]]),in,valueOnly = TRUE)
if(draw){
dev.new(height = device.height,width = device.width)
grid.newpage()
grid.draw(g)
}
隐形(g)
}
要求(ggplot2)
d1 < - 子集(mtcars,carb!= 8)
d2 < - 子集(mtcars,carb%,%c(1,2,3))
p = qplot(vs,am,data = d1)+ facet_wrap(〜carb)
freeze_panels(p)
freeze_panels(p%+%d2)
freeze_panels(p%+%d2 + facet_wrap(〜carb,ncol = 1))
region 1: 12 groups = 12 medium facets
region 2: 3 groups = 3 **HUGE** facets
region 3: 24 groups = 24 **TINY** facets
region 4: 16 groups = 16 medium facets
...
What kind of result I would like to have is :
require(ggplot2)
p = qplot(Sepal.Width, Sepal.Length, data=iris) + facet_wrap(~Species, ncol=1)
g = ggplotGrob(p)
panels = which(sapply(g[["heights"]], "attr", "unit") == "null")
g[["heights"]][panels] = list(unit(4, "cm"), unit(8, "cm"), unit(2, "cm"))
device.height = convertHeight(sum(g[["heights"]]), "in", valueOnly=TRUE)
pdf("test.pdf", height = device.height)
grid.draw(g)
dev.off()
freeze_panels <- function(p, draw=TRUE,
width=unit(5,"cm"),
height=unit(1,"in")){
require(grid)
g <- ggplotGrob(p)
vertical_panels <-which(sapply(g[["heights"]], "attr", "unit") == "null")
horizontal_panels <-which(sapply(g[["widths"]], "attr", "unit") == "null")
g[["heights"]][vertical_panels] <- replicate(length(vertical_panels),
height, simplify=FALSE)
g[["widths"]][horizontal_panels] <- replicate(length(horizontal_panels),
width, simplify=FALSE)
device.height <- convertHeight(sum(g[["heights"]]), "in", valueOnly=TRUE)
device.width <- convertWidth(sum(g[["widths"]]), "in", valueOnly=TRUE)
if(draw){
dev.new(height=device.height, width=device.width)
grid.newpage()
grid.draw(g)
}
invisible(g)
}
require(ggplot2)
d1 <- subset(mtcars, carb != 8)
d2 <- subset(mtcars, carb %in% c(1,2,3))
p = qplot(vs, am, data=d1) + facet_wrap(~carb)
freeze_panels(p)
freeze_panels(p %+% d2)
freeze_panels(p %+% d2 + facet_wrap(~carb, ncol=1))