- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
想象一下我们有这样的数据:
dat <- structure(list(variable = c("a1", "a1", "a1", "a1", "a1", "a1",
"a2", "a2", "a2", "a2", "a2", "a2", "a3", "a3", "a3", "a3", "a3",
"a3", "a4", "a4", "a4", "a4", "a4", "a4"), value = c(9.17804065427195,
-0.477515191225569, 0.189943035684685, -6.06095979017212, -10.4173631972868,
-6.119330192816, -14.3820530117637, 13.9823789620469, 15.6437973890843,
0.754856919261315, -0.887052526388938, 7.4096244573169, 0.61043977214679,
28.4639357142541, 15.4511442682744, 15.8118136384483, 6.65940292893,
0.467862281678766, 482.791905769932, 493.606761379037, 491.254828253119,
504.323684433231, 499.323576709646, 492.625278087471)), .Names = c("variable",
"value"), row.names = c(NA, -24L), class = "data.frame")
value
对比
value
每个
variable
,这样我就有了以下格式的 6 个面板,其中字母代表轴所在的位置和一个
p
显示面板所在的位置。
a2 p
a3 p p
a4 p p p
a1 a2 a3
par(.....)
plot(a1 ~ a2, data=longdat)
plot(a1 ~ a3, data=longdat)
plot(a1 ~ a4, data=longdat)
......
facet_wrap
或
facet_grid
在
ggplot2
Lattice
似乎有我想要的那种形状的图(见下文),但我只能看到如何为
using two axes split per factor 做到这一点.这里的直方图不是必需的,这只是一个例子。
xyplot(value~value|variable,
data = a,
scales=list(alternating=FALSE,relation="same"),
layout=c(2,2))
relation
至
"free"
所以你在轴上为每个变量获得不同的比例,然后将面板分解成单独的面板。
最佳答案
编辑:使用 GGally (v1.0.1)
使用 ggpairs()
更容易来自 GGally
的函数包裹。让 ggpairs()
绘制和定位散点图,然后从结果图中删除不需要的元素。首先,以宽格式转换数据。
# Packages
library(GGally)
library(ggplot2)
library(tidyr)
# Data
dat <- structure(list(variable = c("a1", "a1", "a1", "a1", "a1", "a1",
"a2", "a2", "a2", "a2", "a2", "a2", "a3", "a3", "a3", "a3", "a3",
"a3", "a4", "a4", "a4", "a4", "a4", "a4"),
value = c(9.17804065427195,
-0.477515191225569, 0.189943035684685, -6.06095979017212, -10.4173631972868,
-6.119330192816, -14.3820530117637, 13.9823789620469, 15.6437973890843,
0.754856919261315, -0.887052526388938, 7.4096244573169, 0.61043977214679,
28.4639357142541, 15.4511442682744, 15.8118136384483, 6.65940292893,
0.467862281678766, 482.791905769932, 493.606761379037, 491.254828253119,
504.323684433231, 499.323576709646, 492.625278087471)), .Names = c("variable",
"value"), row.names = c(NA, -24L), class = "data.frame")
# Get the data in its wide format
dat$id <- sequence(rle(as.character(dat$variable))$lengths)
dat2 = spread(data = dat, key = variable, value = value)
# Base plot
gg = ggpairs(dat2,
columns = 2:5,
lower = list(continuous = "points"),
diag = list(continuous = "blankDiag"),
upper = list(continuous = "blank"))
# Trim off the diagonal spaces
n <- gg$nrow
gg$nrow <- gg$ncol <- n-1
v <- 1:n^2
gg$plots <- gg$plots[v > n & v%%n != 0]
# Trim off the last x axis label
# and the first y axis label
gg$xAxisLabels <- gg$xAxisLabels[-n]
gg$yAxisLabels <- gg$yAxisLabels[-1]
# Draw the plot
gg = gg +
theme_bw() +
theme(panel.grid = element_blank())
gg
pairs()
函数让您接近,但如果您只需要布局矩阵中显示的六个面板,那么您可能必须手动构建它。您可以使用
grid
构建图表, 或
ggplot
和
gtable
.这是一个
ggplot / gtable
版本。
dat
数据文件(即长格式)。它构造了六个
ggplot
的列表散点图。 ggplots 转换为 grobs,并提取相关轴 - 将成为新图表中左侧和底部轴的轴。构建了 gtable 布局,并将散点图 grobs(仅限绘图面板)添加到布局中。修改布局以采用轴,然后再次修改布局以采用可变标签。最后,还有一点整理。
dat <- structure(list(variable = c("a1", "a1", "a1", "a1", "a1", "a1",
"a2", "a2", "a2", "a2", "a2", "a2", "a3", "a3", "a3", "a3", "a3",
"a3", "a4", "a4", "a4", "a4", "a4", "a4"),
value = c(9.17804065427195,
-0.477515191225569, 0.189943035684685, -6.06095979017212, -10.4173631972868,
-6.119330192816, -14.3820530117637, 13.9823789620469, 15.6437973890843,
0.754856919261315, -0.887052526388938, 7.4096244573169, 0.61043977214679,
28.4639357142541, 15.4511442682744, 15.8118136384483, 6.65940292893,
0.467862281678766, 482.791905769932, 493.606761379037, 491.254828253119,
504.323684433231, 499.323576709646, 492.625278087471)), .Names = c("variable",
"value"), row.names = c(NA, -24L), class = "data.frame")
# Load packages
library("ggplot2")
library("plyr")
library("gtable")
library(grid)
# Number of items and item labels
item = unique(dat$variable)
n = length(item)
## List of scatterplots
scatter <- list()
for (i in 1:(n-1)) {
for (j in (i+1):n) {
# Data frame
df.point <- na.omit(data.frame(cbind(x = dat[dat$variable == item[i], 2], y = dat[dat$variable == item[j], 2])))
# Plot
p <- ggplot(df.point, aes(x, y)) +
geom_point(size = 1) +
theme_bw() +
theme(panel.grid = element_blank(),
axis.text = element_text(size = 6))
name <- paste0("Item", i, j)
scatter[[name]] <- p
} }
# Convert ggplots to grobs
scatterGrob <- llply(scatter, ggplotGrob)
# Extract the axes as grobs
# x axis
xaxes = subset(scatterGrob, grepl(paste0("^Item.", n), names(scatterGrob)))
xaxes = llply(xaxes, gtable_filter, "axis-b")
# y axis
yaxes = subset(scatterGrob, grepl("^Item1.*", names(scatterGrob)))
yaxes = llply(yaxes, gtable_filter, "axis-l")
# Tick marks and tick mark labels are easier to position if they are separated.
labelsb = list(); ticksb = list(); labelsl = list(); ticksl = list()
for(i in 1:(n-1)) {
x = xaxes[[i]][[1]][[1]]$children[[2]]
labelsb[[i]] = x$grobs[[2]]
ticksb[[i]] = x$grobs[[1]]
y = yaxes[[i]][[1]][[1]]$children[[2]]
labelsl[[i]] = y$grobs[[1]]
ticksl[[i]] = y$grobs[[2]]
}
## Extract the plot panels
scatterGrob <- llply(scatterGrob, gtable_filter, "panel")
## Set up initial gtable layout
gt <- gtable(unit(rep(1, n-1), "null"), unit(rep(1, n-1), "null"))
# Add scatterplots in the lower half of the matrix
k <- 1
for (i in 1:(n-1)) {
for (j in i:(n-1)) {
gt <- gtable_add_grob(gt, scatterGrob[[k]], t=j, l=i)
k <- k+1
} }
# Add rows and columns for axes
gt <- gtable_add_cols(gt, unit(0.25, "lines"), 0)
gt <- gtable_add_cols(gt, unit(1, "lines"), 0)
gt <- gtable_add_rows(gt, unit(0.25, "lines"), 2*(n-1))
gt <- gtable_add_rows(gt, unit(0.5, "lines"), 2*(n-1))
for (i in 1:(n-1)) {
gt <- gtable_add_grob(gt, ticksb[[i]], t=(n-1)+1, l=i+2)
gt <- gtable_add_grob(gt, labelsb[[i]], t=(n-1)+2, l=i+2)
gt <- gtable_add_grob(gt, ticksl[[i]], t=i, l=2)
gt <- gtable_add_grob(gt, labelsl[[i]], t=i, l=1)
}
# Add rows and columns for variable names
gt <- gtable_add_cols(gt, unit(1, "lines"), 0)
gt <- gtable_add_rows(gt, unit(1, "lines"), n+1)
for(i in 1:(n-1)) gt <- gtable_add_grob(gt,
textGrob(item[i], gp = gpar(fontsize = 8)), t=n+2, l=i+3)
for(i in 2:n) gt <- gtable_add_grob(gt,
textGrob(item[i], rot = 90, gp = gpar(fontsize = 8)), t=i-1, l=1)
# Add small gaps between the panels
for(i in (n-1):2) {
gt <- gtable_add_cols(gt, unit(0.4, "lines"), i+2)
gt <- gtable_add_rows(gt, unit(0.4, "lines"), i-1)
}
# Add margins to the whole plot
for(i in c(2*(n-1)+2, 0)) {
gt <- gtable_add_cols(gt, unit(.75, "lines"), i)
gt <- gtable_add_rows(gt, unit(.75, "lines"), i)
}
# Turn clipping off
gt$layout$clip = "off"
# Draw it
grid.newpage()
grid.draw(gt)
关于r - 生成具有交替轴和不同比例的堆叠多面板图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28943553/
我想交织相同模式和相等长度的两个向量。说: a <- rpois(lambda=3,n=5e5) b <- rpois(lambda=4,n=5e5) 我想交织或交织这两个向量,以创建一个等效于c(a
我有两个矩阵,我想以交替方式交织/交错/堆叠在彼此之上/rbind。 ranks=1:3 names=c("Karl", "Klaus", "Mary") x <- cbind(ranks, name
我在 JavaScript 中有一个字符串,如下所示: "This {{@is}} a $|test$| string with $|@string$| delimiters {{as}} follo
那么,我正在尝试“合并”一个字符串 (a) 和一个字符串列表 (b): a = '1234' b = ['+', '-', ''] 获得所需的输出(c): c = '1+2-34' 所需输出字符串中的
我有几个可变长度的数组,其中填充了表示好数据 block 和坏数据 block 的元组。 input = [ [(True, 0, 400), (False, 400, 500), (True
我有一个包含 3 个单元格的表,我想知道是否可以在每个单元格之间“旋转”数据? 基本上,在设定的时间后,我希望第一个单元格中的数据移动到第二个单元格,第二个单元格数据移动到第三个单元格,第三个单元格数
使用 RichFaces dataTable 组件交替行颜色的最简单方法是什么? 最佳答案 查找组件的 rowClasses 属性。 抱歉.. 阅读 richfaces 文档两秒钟后就明白了。 :)
我想创建一个有 4 列的表格,但只显示 3 列。所以我理想情况下有一个跨越 3 列的 div 并应用溢出:隐藏。单击按钮时,我希望第 3 列(公司 2)向左滑动,并替换为第 4 列(公司 3),以便将
有没有办法在 AngularJS 中替代 ng-repeats ? 例如,假设我有两个数组。 数组1 [a1, a2, a3, ...] 数组2 [b1, b2, b3, ...] 我想针对中型和大屏
我有三个包含元素的列表: a = [[0,1],[2,3],...] b = [[5,6],[7,8],...] c = [] 我想将 a 和 b 中的元素 append 到 c 中以获得: c =
我喜欢在 MATLAB 中绘制经过傅立叶变换的信号。通过 set(gca,'xtick',peaks,'FontSize',12);我可以在 x 轴上显示峰值。但有时,峰值靠得太近,显示峰值的文本与其
我正在使用 CSS Grid 为我的网站构建服务列表。整个网站网格中的一行被分成两个 CSS 网格列。 在第一行的第一列中,有一项服务的描述。在第二列中,有一个代表服务的图像。 每一行,描述和图像交替
我有以下 html 代码: 1 2 3 4 5 6 7 8 我想做的是使用奇数/偶数 nth-child 选择器对它们进行一些交替
在下面的代码示例中,我将 window.status 从“a”替换为“b” function alternateViaIntrvl() { setInterval('alterStatus()
下面的CSS和HTML代码生成 News Interviews ---------------------- Djing Break dance ---------------------- 为什么横
我曾经在 tableView willDisplay cell 方法中使用这段代码,但它没有准确地交替颜色 - 它几乎做到了,但有时仍然搞砸了 1 或 2 个相同的颜色和我不确定。 我发现一些建议在我
我的问题的本质是解决方案太多,我想在围绕它构建基础设施之前找出哪一个在优缺点中胜出。 (为了本论坛的目的进行了简化)这是一个拍卖网站,其中五个拍卖按排名#1-5 存储,#1 是当前特色拍卖。其他四个人
如果可能的话,我正在尝试找出是否有一种方法可以替换内容行分隔符的颜色。 例如: 问题是它必须是自动的,所以我假设可能需要 javascript,但我找不到这样的东西。我知道有些事情表明如果你有类似 t
这个看似简单的问题困扰了我整整 10 年。 (好的,不是每天!) 它在 IE 中运行良好,但在 FF 和 Chrome 中运行不正常,这通常表示代码有问题...我想在两侧都有 DIV 框(它们实际上是
我想找到交替 [0, 1, 0, 1] 所需的最少翻转次数,例如给定 [1, 1, 0, 1]。所以在这种情况下,它是一次翻转。 def solution(A): count = 0
我是一名优秀的程序员,十分优秀!