geom_tile和facet_grid / facet_wrap用于相同高度的图块 [英] geom_tile and facet_grid/facet_wrap for same height of tiles
问题描述
使用ggplot,我想用面板表示一个图形块,但每个面板都有相同的高度块。
我有这张图:
dataSta< - list(sites = rep(paste(S,1 (rep(组1):31),每个= 12),month = rep(1:12,31),value = round(runif(31 * 12,min = 0,max = 3000) (ggplot2)
库(网格,16 * 12),代表(第2组,12 * 12),代表(第3组,3 * 12))
)
base_size< - 9
windows()
ggplot(data.frame(dataSta),aes(factor(month),sites))+
geom_tile (aes(fill = value),color =black)+
facet_wrap(〜panel,scale =free_y,nrow = 3)+
theme_grey(base_size = base_size)+
labs(x =,y =)+
scale_x_discrete(expand = c(0,0))+
scale_y_discrete(expand = c(0,0))+
theme (legend.title = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(size = base_size * 0.8,hjust = 0),
面板。 margin = unit(0,lines),
strip.text = element_text(colou r =red3,size = 10,face = 2))
但面板的高度不同。我尝试使用facet_grid:
windows()
ggplot(data.frame(dataSta),aes(factor月),网站))+
geom_tile(aes(fill = value),color =black)+
facet_grid(panel〜。,scales =free_y,space =free)+
theme_grey(base_size = base_size)+
labs(x =,y =)+
scale_x_discrete(expand = c(0,0))+
scale_y_discrete( expand = c(0,0))+
theme(legend.title = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(size = base_size * 0.8,hjust = 0),
panel.margin = unit(0,lines),
strip.text = element_text(color =red3,size = 10,face = 2) )
瓷砖高度问题已解决,但面板(组1 ...组3)的标签不在面板顶部。是否有可能通过facet_grid更改面板标签的位置?或结合facet_grid和facet_wrap?
感谢您的帮助,并为我的英语感到难过!
您可以在绘图之前查看ggplot包含的内容,并相应地重新缩放面板。
g < - ggplot_build(p)
##找出有多少个y-在每个面板
##中打破间隔以推断拼贴块的数量
vtiles< - sapply(lapply(g $ panel $ ranges,[[,y.major),length) b
$ b ##将图表转换为gtable对象
gt< - ggplot_gtable(g)
##找出布局中的哪些项目对应于面板
# #我们引用布局
面板的t(顶部)索引< - gt $ layout $ t [grepl(panel,gt $ layout $ name)]
##替换默认面板高度(1null)与相对大小
## null单位相对于彼此的缩放比例,所以我们用瓦片数量进行缩放
gt $ heights [panels]< -lapply(vtiles,unit, null)
##在干净的平板上绘制
库(网格)
grid.newpage()
grid.draw(gt)
Using ggplot, I would like represent a graph tile with panel, but with same height tile for each panel. I have this graph :
dataSta <- list(sites=rep(paste("S", 1:31),each=12), month=rep(1:12,31), value=round(runif(31*12, min=0, max=3000)), panel=c(rep("Group 1",16*12),rep("Group 2", 12*12), rep("Group 3", 3*12)))
library(ggplot2)
library(grid)
base_size <- 9
windows()
ggplot(data.frame(dataSta), aes(factor(month), sites)) +
geom_tile(aes(fill = value), colour = "black")+
facet_wrap(~panel, scale="free_y", nrow=3)+
theme_grey(base_size = base_size) +
labs(x = "",y = "") +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
theme(legend.title = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(size = base_size *0.8, hjust = 0),
panel.margin = unit(0,"lines"),
strip.text = element_text(colour="red3", size=10, face=2))
But height of tiles is different between panel. I try to use facet_grid :
windows()
ggplot(data.frame(dataSta), aes(factor(month), sites)) +
geom_tile(aes(fill = value), colour = "black")+
facet_grid(panel~., scales="free_y", space="free")+
theme_grey(base_size = base_size) +
labs(x = "",y = "") +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
theme(legend.title = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_text(size = base_size *0.8, hjust = 0),
panel.margin = unit(0,"lines"),
strip.text = element_text(colour="red3", size=10, face=2))
The problem with height of tiles is resolved, but labels of panel (Group 1 ... Group 3) are not on top of panel. Is it possible to change position of panel labels with facet_grid ? or combine facet_grid and facet_wrap ? Thanks for your help, and sorry for my English !
You can look at what ggplot contains before plotting, and rescale the panels accordingly.
g <- ggplot_build(p)
## find out how many y-breaks are in each panel
## to infer the number of tiles
vtiles <- sapply(lapply(g$panel$ranges, "[[", "y.major"), length)
## convert the plot to a gtable object
gt <- ggplot_gtable(g)
## find out which items in the layout correspond to the panels
## we refer to the "t" (top) index of the layout
panels <- gt$layout$t[grepl("panel", gt$layout$name)]
## replace the default panel heights (1null) with relative sizes
## null units scale relative to each other, so we scale with the number of tiles
gt$heights[panels] <-lapply(vtiles, unit, "null")
## draw on a clean slate
library(grid)
grid.newpage()
grid.draw(gt)
这篇关于geom_tile和facet_grid / facet_wrap用于相同高度的图块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!