R ggplot2 - facet conservation coefficient, but redefines / determines the size of the output graph

I am currently using ggplot2 to compare statistics from different groups, each of which belongs to a different region. This is done through a web application (tikiwiki CMS + R plugin) that runs the R script. In a region, I can have from 2 to 30 or more groups. The same R script works for all data on a unique web page, the adaptation result depending on the selected area is like a page parameter.

I currently have the following:

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 ... 

Region 2

Less groups

Region 3

More groups

What result would I like to get:

Region 2

enter image description here

Region 3 Remains the same

I make a facet in a group with facet_wrap (~ data, ncol = 4), so I can have either 2 or 30 + faces. My problem is the output: either 30 faces are filled in a tiny box, or two of them are large in a box of the same size β†’ See the pictures above ... I can’t find how to fix the ratio (or the image is maximum width), but keep final image size free.

Is it possible in ggplot2 / R to fix the default size for faces and have the size of the resulting image adapted to the number of facets?

If this is not possible in ggplot2 / R, is there a javascript / jquery library that can get the code and show the results correctly (d3, ...)?

+6
source share
1 answer

you can edit gtable to set heights for physical units (like cm) instead of relative ("null")

 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() 

enter image description here

Change As a subsequent function, the height and width of all panels with a fixed value are set here,

 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)) 
+5
source

Source: https://habr.com/ru/post/955903/


All Articles