- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以控制 ggplot 中每行的面板数量?我只能在每行上获得相同数量的面板,如下面的示例图所示。
例如,我的数据由“标记”自然分组为 22 个块,而“标记”又由“染料”组织(参见下面的示例代码和数据)。在这个例子中,我想将面板排列成 4 行,每行分别有 5、6、6 和 5 个面板(如最后的图片所示,但 22 个块可以等宽)。
示例代码:
df$Type <- factor(round(df$Type, 2))
df$Allele <- factor(df$Allele)
gp <- ggplot(df, aes_string(x = "Allele", y = "Ratio", colour = "Type"))
gp <- gp + geom_point(alpha = 0.8, position = position_jitter(width = 0.1))
gp <- gp + facet_grid(Dye ~ Marker) + facet_wrap(~Marker, ncol = 5, drop = FALSE, scales = "free_x")
gp <- gp + guides(fill = guide_legend(reverse = TRUE))
gp <- gp + labs(title = "Stutter ratios")
print(gp)
Marker Allele Ratio Type Dye
DYS576 18 0.116157205 -1 B
DYS389 I 14 0.043252595 -1 B
DYS448 19 0.018236074 -1 B
DYS389 II 31 0.102169982 -1 B
DYS19 14 0.058139535 -1 B
DYS19 14 0.078224101 -0.2 B
DYS391 10 0.090035245 -1 G
DYS481 22 0.013492063 -2 G
DYS481 22 0.179365079 -1 G
DYS549 13 0.0625 -1 G
DYS533 12 0.07564495 -1 G
DYS437 14 0.04757085 -1 G
DYS570 17 0.071079867 -1 Y
DYS570 17 0.007420426 1 Y
DYS635 21 0.192561983 -1 Y
DYS390 24 0.073079325 -1 Y
DYS439 12 0.084817642 -1 Y
DYS392 13 0.125965997 -1 Y
DYS393 13 0.009672831 -2 R
DYS393 13 0.079374111 -1 R
DYS393 13 0.013371266 1 R
DYS458 17 0.126099707 -1 R
DYS385 13 0.059782609 -1 R
DYS385 16 0.092356688 -1 R
DYS456 17 0.12 -1 R
YGATAH4 11 0.07203718 -1 R
DYS576 18 0.094989562 -1 B
DYS389 I 14 0.044955045 -1 B
DYS448 19 0.017171717 -1 B
DYS389 II 31 0.124137931 -1 B
DYS391 10 0.052903833 -1 G
DYS481 22 0.198726115 -1 G
DYS549 13 0.08853967 -1 G
DYS533 12 0.106617647 -1 G
DYS438 9 0.017562533 -1 G
DYS570 17 0.006710002 -2 Y
DYS570 17 0.076326274 -1 Y
DYS570 17 0.007339065 1 Y
DYS635 21 0.132272501 -1 Y
DYS390 24 0.078853047 -1 Y
DYS439 12 0.06980198 -1 Y
DYS392 13 0.104508197 -1 Y
DYS393 13 0.083853995 -1 R
DYS393 13 0.014140085 1 R
DYS458 17 0.094651285 -1 R
DYS385 13 0.076977401 -1 R
DYS385 13 0.076977401 -1 R
DYS385 16 0.059866962 -1 R
DYS385 16 0.059866962 -1 R
DYS456 17 0.151162791 -1 R
YGATAH4 11 0.09254902 -1 R
DYS576 18 0.126856684 -1 B
DYS389 I 14 0.052631579 -1 B
DYS389 II 31 0.102253033 -1 B
DYS19 14 0.056882821 -1 B
DYS19 14 0.080773606 -0.2 B
DYS391 10 0.053362122 -1 G
DYS481 22 0.033595801 -2 G
DYS481 22 0.164829396 -1 G
DYS549 13 0.123548922 -1 G
DYS533 12 0.06750174 -1 G
DYS437 14 0.041118421 -1 G
DYS570 17 0.097141001 -1 Y
DYS570 17 0.010071475 1 Y
DYS635 21 0.070416095 -1 Y
DYS390 24 0.075715605 -1 Y
DYS439 12 0.077648766 -1 Y
DYS392 13 0.116974494 -1 Y
DYS643 10 0.017945781 -1 Y
DYS393 13 0.011755878 -2 R
DYS393 13 0.121810905 -1 R
DYS393 13 0.017008504 1 R
DYS458 17 0.097028366 -1 R
DYS385 13 0.083820663 -1 R
DYS385 16 0.124661247 -1 R
DYS456 17 0.11167002 -1 R
DYS576 18 0.102416918 -1 B
DYS448 19 0.021699819 -1 B
DYS19 14 0.064239829 -0.2 B
DYS391 10 0.054468085 -1 G
DYS481 22 0.048726467 -2 G
DYS481 22 0.182724252 -1 G
DYS549 13 0.091326105 -1 G
DYS533 12 0.074295474 -1 G
DYS438 9 0.059535822 -1 G
DYS437 14 0.044034091 -1 G
DYS570 17 0.02547279 -2 Y
DYS570 17 0.129293709 -1 Y
DYS570 17 0.012350444 1 Y
DYS635 21 0.09912927 -1 Y
DYS390 24 0.086936937 -1 Y
DYS439 12 0.060550459 -1 Y
DYS392 13 0.149750416 -1 Y
DYS393 13 0.08388521 -1 R
DYS393 13 0.016188374 1 R
DYS458 17 0.009228937 -2 R
DYS458 17 0.092289372 -1 R
DYS458 17 0.062816314 1 R
DYS385 13 0.068504595 -1 R
DYS385 16 0.077120823 -1 R
DYS456 17 0.131855309 -1 R
YGATAH4 11 0.070570571 -1 R
DYS576 18 0.108604407 -1 B
DYS389 I 14 0.053097345 -1 B
DYS389 II 31 0.122986823 -1 B
DYS19 14 0.044878049 -1 B
DYS19 14 0.069268293 -0.2 B
DYS391 10 0.057256368 -1 G
DYS481 22 0.029480217 -2 G
DYS481 22 0.171450737 -1 G
DYS549 13 0.078275862 -1 G
DYS533 12 0.062146893 -1 G
DYS437 14 0.037869063 -1 G
DYS570 17 0.0956807 -1 Y
DYS570 17 0.021323127 1 Y
DYS635 21 0.076858108 -1 Y
DYS390 24 0.099143207 -1 Y
DYS439 12 0.057610242 -1 Y
DYS439 12 0.028449502 1 Y
DYS392 13 0.101621622 -1 Y
DYS393 13 0.012474012 -2 R
DYS393 13 0.117463617 -1 R
DYS393 13 0.01039501 1 R
DYS458 17 0.081623347 -1 R
DYS385 13 0.068003487 -1 R
DYS385 16 0.066376496 -1 R
DYS456 17 0.149382716 -1 R
# Prepare data.
df$Marker <- factor(df$Marker, levels = c("DYS576", "DYS389 I", "DYS448", "DYS389 II", "DYS19",
"DYS391", "DYS481", "DYS549", "DYS533", "DYS438", "DYS437",
"DYS570", "DYS635", "DYS390", "DYS439", "DYS392",
"DYS643", "DYS393", "DYS458", "DYS385", "DYS456", "YGATAH4" ))
df$Type <- factor(round(df$Type, 2))
df$Dye <- factor(df$Dye, levels = c("B", "G", "Y", "R"))
df$Allele <- factor(df$Allele)
# Get y max to use same scale.
yMax <- max(df$Ratio)
# Get dyes.
dyes <- levels(df$Dye)
# start new page
plot.new()
# setup layout
gl <- grid.layout(nrow=length(dyes) , ncol=1)
# grid.show.layout(gl) # To inspect layout.
# Init layout
pushViewport(viewport(layout=gl))
# Loop over all dyes.
for(d in seq(along=dyes)){
# Move to the next viewport
pushViewport(viewport(layout.pos.col=1, layout.pos.row=d))
# Create a plot for the current subset.
gp <- ggplot(subset(df, Dye==dyes[d]), aes_string(x = "Allele", y = "Ratio"))
gp <- gp + geom_point(aes_string(colour = "Type"), alpha = 0.8, position = position_jitter(width = 0.1))
gp <- gp + facet_grid(Dye ~ Marker, scales="free_x")
gp <- gp + ylim(0, yMax)
# If first dye channel.
if(d == 1){
# Plot title only.
gp <- gp + labs(title = "Stutter ratios")
gp <- gp + theme(axis.title.x=element_blank())
gp <- gp + theme(axis.title.y=element_blank())
# Remove legends.
gp <- gp + theme(legend.position="none")
} else if(d == length(dyes)){ # If last dye channel.
# No title but x and y labels.
# Y label should ideally be centered vertically in final plot.
gp <- gp + labs(title = element_blank())
gp <- gp + labs(xlab = "Allele")
gp <- gp + labs(xlab = "Ratio")
# Not removing legend works but makes the last plot more compact (horizontally).
# Can the panel height or width be fixed for all subplots?
# 'bottom' is nicer (assuming I can't center it vertically in the final plot)
# but makes the last dye channel very compact (vertically).
# gp <- gp + theme(legend.position="bottom")
} else { # No titles, labels or legends.
gp <- gp + labs(title = element_blank())
gp <- gp + theme(axis.title.x = element_blank())
gp <- gp + theme(axis.title.y = element_blank())
gp <- gp + theme(legend.position="none")
}
# Print the ggplot graphics here
print(gp, newpage = FALSE)
# Done with this viewport
popViewport(1)
}
# Prepare data.
df$Marker <- factor(df$Marker, levels = c("DYS576", "DYS389 I", "DYS448", "DYS389 II", "DYS19",
"DYS391", "DYS481", "DYS549", "DYS533", "DYS438", "DYS437",
"DYS570", "DYS635", "DYS390", "DYS439", "DYS392",
"DYS643", "DYS393", "DYS458", "DYS385", "DYS456", "YGATAH4" ))
df$Type <- factor(round(df$Type, 2))
df$Dye <- factor(df$Dye, levels = c("B", "G", "Y", "R"))
df$Allele <- factor(df$Allele)
# Get y max to use same scale.
yMax <- max(df$Ratio)
# Get dyes.
dyes <- levels(df$Dye)
# Number of dyes.
noDyes <- length(dyes)
# Number of rows in table object.
noRows <- length(dyes) + 2
# Create table object.
g <- gtable(widths=unit(c(1,4,1),c("lines","null","null")),
heights = unit(c(1,rep(1,noDyes),1), c("line",rep("null",noDyes), "line")))
# Add titles.
g <- gtable_add_grob(g, textGrob("Stutter ratios"), t=1,b=1,l=2,r=2)
g <- gtable_add_grob(g, textGrob("Allele"), t=noRows ,b=noRows ,l=2,r=2)
g <- gtable_add_grob(g, textGrob("Ratio", rot=90), t=1,b=noRows ,l=1,r=1)
# Create a plot for the entire dataset to extract the legend.
gp <- ggplot(df, aes_string(x = "Allele", y = "Ratio"))
gp <- gp + geom_point(aes_string(colour = "Type"))
# Extract the legend.
guide <- gtable_filter(ggplotGrob(gp), pattern="guide")
# Add the legend to the table object.
g <- gtable_add_grob(g,guide , t=1,b=noRows,l=3,r=3)
# Loop over all dyes.
for(d in seq(along=dyes)){
# Create a plot for the current subset.
gp <- ggplot(subset(df, Dye==dyes[d]), aes_string(x = "Allele", y = "Ratio"))
gp <- gp + geom_point(aes_string(colour = "Type"), alpha = 0.8, position = position_jitter(width = 0.1))
gp <- gp + scale_colour_discrete(drop = FALSE)
gp <- gp + facet_grid(Dye ~ Marker, scales="free_x")
gp <- gp + ylim(0, yMax)
# Remove titles, axis labels and legend.
gp <- gp + labs(title = element_blank())
gp <- gp + theme(axis.title.x = element_blank())
gp <- gp + theme(axis.title.y = element_blank())
gp <- gp + theme(legend.position="none")
# Add plot panel to table object.
g <- gtable_add_grob(g,ggplotGrob(gp), t=(d+1),b=(d+1),l=2,r=2)
}
# Plot.
grid.newpage()
grid.draw(g)
最佳答案
放置 grobs 的一种简单方法是使用 gtable 包,
library(gtable)
gtable_add_grobs <- gtable_add_grob #misleading name
g <- gtable(widths=unit(c(1,4,1),c("lines","null","null")),
heights = unit(c(1,1,1,1), c("line","null","null", "line")))
lg <- list(textGrob("title"),
textGrob("xlab"),
textGrob("ylab", rot=90),
rectGrob(),
rectGrob(),
rectGrob())
pos <- data.frame(t=c(1, 4, 1, 2, 3, 2),
b=c(1, 4, 4, 2, 3, 3),
l=c(2, 2, 1, 2, 2, 3),
r=c(3, 2, 1, 2, 2, 3))
g <- with(pos, gtable_add_grobs(g, lg, t=t, l=l, b=b, r=r))
grid.newpage()
grid.draw(g)
gtable_filter(ggplotGrob(p), pattern="guide")
提取 ggplot 的图例.
关于r - ggplot2在使用facet时控制每行面板的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17314058/
大家好,我完成了这个基本的 C 程序,它向输入任何给定数字集的用户显示有序集、最小值、最大值、平均值和中值。我遇到的问题是,当我打印数字时,我必须使用诸如“3.2%f”之类的东西来设置标准的精度,我怎
我有这个基于 Python 的服务守护进程,它正在执行大量多路复用 IO(选择)。 从另一个脚本(也是 Python)我想查询这个服务守护进程的状态/信息和/或控制处理(例如暂停它、关闭它、更改一些参
我读到 Fortran 对表达式求值的顺序有严格的规则。对于某些数值算法来说,这一点非常重要。 数值 C 程序如何控制浮点运算的顺序并防止编译器“优化”到不需要的运算顺序,例如将 (a*b)*c 更改
上下文: 整个问题可以概括为我正在尝试复制调用system(或fork)的行为,但在 mpi 环境中。 (事实证明,你不能并行调用system。)这意味着我有一个程序在许多节点上运行,每个节点上有一个
我考虑过控制scanf来接受c中的任何输入。我的概念是等待10秒(或任何其他时间)来接受任何输入。10秒后它将退出并且不再接收任何输入。 int main(){ int a,b,c,d; sca
我正在尝试使用生成器停止 setTimeOut 上的执行流程。我究竟做错了什么?我无法让 console.log 每 1500 毫秒退出一次。我是 node 的新手,如果我在做一件非常愚蠢的事情,请不
我希望我的应用程序的 Activity 堆栈包含同一 Activity 的多个实例,每个实例处理不同的数据。因此,我将让 Activity A 在我的 Activity 堆栈中处理数据 a、b、c 和
我有这个 bash 文件,它向设备询问 OpenSSH 的 IP、密码等。 现在,如果我使用 ssh root@ip,我必须输入密码。这真的很烦人。第二;我不能让我的脚本向它发送命令。 这就是我想要的
我正在尝试测试我有权访问的机器的缓存属性。为此,我正在尝试读取内存并对其计时。我改变工作集大小和步幅访问模式以获得不同的测量值。 代码如下所示: clock1 = get_ticks() for (i
我正在尝试编写一个 makefile 来替换用于构建相当大的应用程序的脚本之一。 当前脚本一次编译一个文件,使用 make 的主要原因是并行化构建过程。使用 make -j 16 我目前在办公室服务器
我正在制作一个小的测试程序,它演示了一个粗糙的控制台界面。 该程序是一个低于标准的典型获取行、响应程序,它甚至不识别“退出”,并希望您通过按 control-c 强制退出。在 Mingw32 上完成。
好的,我有一个 VOIP 电话。我知道电话的 IP 地址和端口,并且可以完全访问电话,我正在使用它通过 SIP 中继调用 SIP 电话。 我基本上想随时查看手机上发生的事情,但我不知道从哪里开始。 如
是否可以指定 CWinApp::WriteProfileString() 使用的应用程序名称? 如果我使用 CWinApp::SetRegistryKey 将我的公司名称设置为“MyCompany”,
我正在尝试用 Python 控制 Tor。我在 stackoverflow 上阅读了其他几个关于这个主题的问题,但没有一个能回答这个问题。 我正在寻找一种方法,以便在命令运行时为您提供“新身份”、新
最近在做一个项目,涉及到iPhone设备和手表传输数据、控制彼此界面跳转,在网上找了很多资料,发现国内的网站这方面介绍的不多,而国外的网站写的也不是很全,所以在这写这篇文章,给大家参考一下,望大神指
我想增加图中值的范围。在示例中,值的范围从 50 到 200。但是,我需要按如下方式分配值:50 75 100 125 150 175 200 并且最好使用 scale_fill_gradientn
我有一个IconButton,当按下时波纹效果是圆形的并且比按钮的面积大,我怎样才能减少点击按钮时波纹效果的大小? IconButton( constraints
我正在使用代码契约(Contract)为我的项目生成附属程序集。基本上它为项目的 MyAssembly.dll 创建一个 MyAssembly.Contracts.dll。这应该放在你的程序集旁边,但
我想使用分面绘制图形,其中面板之间的边缘不同。面板按字母顺序自动排序(按照 ggplot 中的惯例)。一个简单的例子: library(igraph) library(ggraph) g <- mak
我想为我的 Android 应用程序创建一个小部件,以显示有关位置的一些实时详细信息,例如天气。但我想在任何时候允许最多 3 个小部件实例,每个实例都有不同的位置。我不确定该怎么做,也找不到任何信息。
我是一名优秀的程序员,十分优秀!