R “设置”;“空间”;在镶嵌面_中像在镶嵌面_网格中一样包裹
我需要不同的宽度面;左图显示了实验的动态范围,右图显示了测试条件。有没有一种方法可以让自由的x和y标度都有刻面_包裹?这在刻面网格中是可能的,但即使使用scale=“free”,也有一个固定的y比例。镶嵌面_包裹允许自由y比例,但x比例似乎是固定的。几年前,同样的问题也出现在谷歌网页上,但答案并不令人满意。 对不起,如果这是重复这里太;任何帮助都将不胜感激R “设置”;“空间”;在镶嵌面_中像在镶嵌面_网格中一样包裹,r,ggplot2,facet,R,Ggplot2,Facet,我需要不同的宽度面;左图显示了实验的动态范围,右图显示了测试条件。有没有一种方法可以让自由的x和y标度都有刻面_包裹?这在刻面网格中是可能的,但即使使用scale=“free”,也有一个固定的y比例。镶嵌面_包裹允许自由y比例,但x比例似乎是固定的。几年前,同样的问题也出现在谷歌网页上,但答案并不令人满意。 对不起,如果这是重复这里太;任何帮助都将不胜感激 mdf <- read.table(text=" strain val type 1 1 0.0000
mdf <- read.table(text="
strain val type
1 1 0.0000 sample
2 1 0.0140 sample
3 1 0.0175 sample
4 2 0.0025 sample
5 2 0.0260 sample
6 2 0.0105 sample
7 3 0.0190 sample
8 3 0.0725 sample
9 3 0.0390 sample
10 4 0.0560 sample
11 4 0.0695 sample
12 4 0.0605 sample
13 5 0.0735 sample
14 5 0.1065 sample
15 5 0.0890 sample
16 6 0.1135 sample
17 6 0.2105 sample
18 6 0.1410 sample
19 7 0.1360 sample
20 7 0.2610 sample
21 7 0.1740 sample
22 8 0.3850 control
23 8 0.7580 control
24 8 0.5230 control
25 9 0.5230 control
26 9 0.5860 control
27 9 0.7240 control")
library(ggplot2)
p<-ggplot(mdf, aes(reorder(strain, val), val))+
labs(x="Strain", y="intensity")+
geom_boxplot()+
geom_point()+
facet_grid(~type, scales ="free", space="free_x")
p
## free x, fixed y. why?
q<-ggplot(mdf, aes(reorder(strain, val), val))+
labs(x="Strain", y="intensity")+
geom_boxplot()+
geom_point()+
facet_wrap(~type, scales ="free")
q
## free y, fixed x. why?
mdf我不能绝对肯定,但我认为答案是否定的-使用ggplot2命令。我也不认为这是一个好主意,因为对于读者来说,y轴上的刻度不同可能并不明显。但是,如果必须拥有绘图,可以使用ggplot grob布局调整q绘图的面板宽度。请注意,第一个面板有两个x值,第二个面板有七个x值。因此,将面板的默认宽度分别更改为2null和7null
编辑:更新到ggplot2 2.2.0
library(ggplot2)
library(grid)
# get mdf data frame from the question
# Your q plot
q <- ggplot(mdf, aes(factor(strain), val)) +
labs(x = "Strain", y = "intensity") +
geom_boxplot() +
geom_point() +
facet_wrap( ~ type, scales = "free")
q
# Get the ggplot grob
gt = ggplotGrob(q)
# Check for the widths - you need to change the two that are set to 1null
gt$widths
# The required widths are 4 and 8
# Replace the default widths with relative widths:
gt$widths[4] = unit(2, "null")
gt$widths[8] = unit(7, "null")
# Draw the plot
grid.newpage()
grid.draw(gt)
# I think it is better to have some extra space between the two panels
gt$widths[5] = unit(1, "cm")
grid.newpage()
grid.draw(gt)
库(ggplot2)
图书馆(网格)
#从问题中获取mdf数据帧
#你的q图
q%
总结(计数=长度(唯一(应变)))%>%
`[[`(2)
#获取gt布局中与面板对应的列索引。
panelI同样,对于那些试图在dplyr中使用@Sandy答案的人:
library(dplyr)
N<-mdf%>% group_by(type)%>% summarise(count = length(unique(strain)))
# Get the column index in the gt layout corresponding to the panels.
panelI <- gt$layout$l[grepl("panel", gt$layout$name)]
# Replace the default panel widths with relative heights.
gt$widths[panelI] <- lapply(N$count, unit, "null")
库(dplyr)
N%按(类型)分组%>%总结(计数=长度(唯一(应变)))
#获取gt布局中与面板对应的列索引。
谢谢,这真的很理想。这是我见过的“ggplot中的双尺度”或“可变面”问题的最平滑的解决方法;我希望其他人也觉得这很有帮助。
library(dplyr)
N<-mdf%>% group_by(type)%>% summarise(count = length(unique(strain)))
# Get the column index in the gt layout corresponding to the panels.
panelI <- gt$layout$l[grepl("panel", gt$layout$name)]
# Replace the default panel widths with relative heights.
gt$widths[panelI] <- lapply(N$count, unit, "null")