这有可能用ggplot2重现这个格子图吗?
library(latticeExtra)
data(mtcars)
x <- t(as.matrix(scale(mtcars)))
dd.row <- as.dendrogram(hclust(dist(x)))
row.ord <- order.dendrogram(dd.row)
dd.col <- as.dendrogram(hclust(dist(t(x))))
col.ord <- order.dendrogram(dd.col)
library(lattice)
levelplot(x[row.ord, col.ord],
aspect = "fill",
scales = list(x = list(rot = 90)),
colorkey = list(space = "left"),
legend =
list(right =
list(fun = dendrogramGrob,
args =
list(x = dd.col, ord = col.ord,
side = "right",
size = 10)),
top =
list(fun = dendrogramGrob,
args =
list(x = dd.row,
side = "top",
size = 10))))
Run Code Online (Sandbox Code Playgroud)

我想在主题之间插入两个没有任何空间的图(因此它们共用一个轴).
鉴于:
p1 <- qplot(1,1,xlab="")
p1 <- p1 +
theme(legend.position="none",
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
plot.margin=unit(c(1,1,0,1), "cm"),
panel.margin=unit(c(1,1,0,1), "cm"))
p2 <- qplot(1,2)
grid.arrange(p1,p2)
Run Code Online (Sandbox Code Playgroud)
哪个产生:

我想消除两个地块之间的空白区域.
我有调整高度的印象,就像宽度所做的那样:左对齐两个图形边缘(ggplot)是解决方案,但无法弄明白.
我使用此处指示的方法来对齐共享相同横坐标的图形.
但是当我的一些图表有一个传奇而其他图表没有传说时,我无法使它工作.
这是一个例子:
library(ggplot2)
library(reshape2)
library(gridExtra)
x = seq(0, 10, length.out = 200)
y1 = sin(x)
y2 = cos(x)
y3 = sin(x) * cos(x)
df1 <- data.frame(x, y1, y2)
df1 <- melt(df1, id.vars = "x")
g1 <- ggplot(df1, aes(x, value, color = variable)) + geom_line()
print(g1)
df2 <- data.frame(x, y3)
g2 <- ggplot(df2, aes(x, y3)) + geom_line()
print(g2)
gA <- ggplotGrob(g1)
gB <- ggplotGrob(g2)
maxWidth <- grid::unit.pmax(gA$widths[2:3], gB$widths[2:3])
gA$widths[2:3] <- maxWidth
gB$widths[2:3] <- maxWidth
g <- arrangeGrob(gA, gB, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用grid.arrange在ggplot生成的同一页面上显示多个图形.这些图使用相同的x数据但具有不同的y变量.由于y数据具有不同的比例,因此图表具有不同的尺寸.
我尝试在ggplot2中使用各种主题选项来更改绘图大小并移动y轴标签,但没有一个能够对齐绘图.我想将这些图排列成2 x 2的正方形,这样每个图都是相同的大小,x轴对齐.
这是一些测试数据:
A <- c(1,5,6,7,9)
B <- c(10,56,64,86,98)
C <- c(2001,3333,5678,4345,5345)
D <- c(13446,20336,24333,34345,42345)
L <- c(20,34,45,55,67)
M <- data.frame(L, A, B, C, D)
Run Code Online (Sandbox Code Playgroud)
我用来绘制的代码:
x1 <- ggplot(M, aes(L, A,xmin=10,ymin=0)) + geom_point() + stat_smooth(method='lm')
x2 <- ggplot(M, aes(L, B,xmin=10,ymin=0)) + geom_point() + stat_smooth(method='lm')
x3 <- ggplot(M, aes(L, C,xmin=10,ymin=0)) + geom_point() + stat_smooth(method='lm')
x4 <- ggplot(M, aes(L, D,xmin=10,ymin=0)) + geom_point() + stat_smooth(method='lm')
grid.arrange(x1,x2,x3,x4,nrow=2)
Run Code Online (Sandbox Code Playgroud)
如果运行此代码,您将看到由于y轴单位的长度较大,底部两个图的绘图区域较小.
如何使实际绘图窗口相同?
我有一些数据集/变量,我想绘制它们,但我想以紧凑的方式做到这一点.要做到这一点,我希望它们共享相同的y轴但不同的x轴,并且由于不同的分布,我希望其中一个x轴是对数缩放而另一个是线性缩放.
假设我有一个长尾变量(我想在绘制时对x轴进行对数缩放):
library(PtProcess)
library(ggplot2)
set.seed(1)
lambda <- 1.5
a <- 1
pareto <- rpareto(1000,lambda=lambda,a=a)
x_pareto <- seq(from=min(pareto),to=max(pareto),length=1000)
y_pareto <- 1-ppareto(x_pareto,lambda,a)
df1 <- data.frame(x=x_pareto,cdf=y_pareto)
ggplot(df1,aes(x=x,y=cdf)) + geom_line() + scale_x_log10()
Run Code Online (Sandbox Code Playgroud)

和一个正常的变量:
set.seed(1)
mean <- 3
norm <- rnorm(1000,mean=mean)
x_norm <- seq(from=min(norm),to=max(norm),length=1000)
y_norm <- pnorm(x_norm,mean=mean)
df2 <- data.frame(x=x_norm,cdf=y_norm)
ggplot(df2,aes(x=x,y=cdf)) + geom_line()
Run Code Online (Sandbox Code Playgroud)

我想使用相同的y轴并排绘制它们.
我可以使用facet看起来很棒,但是我不知道如何使每个x轴具有不同的比例(scale_x_log10()使它们都按比例缩放):
df1 <- cbind(df1,"pareto")
colnames(df1)[3] <- 'var'
df2 <- cbind(df2,"norm")
colnames(df2)[3] <- 'var'
df <- rbind(df1,df2)
ggplot(df,aes(x=x,y=cdf)) + geom_line() +
facet_wrap(~var,scales="free_x") + scale_x_log10()
Run Code Online (Sandbox Code Playgroud)

使用grid.arrange …
我正试图通过使用cowplot包来以出版物的方式安排绘图.
我只是希望面板的尺寸和尺寸相同.
library(ggplot2)
library(cowplot)
gg1 <- ggplot(mtcars)+
geom_point(aes(x=mpg,y=hp))+
theme_bw()+
theme(aspect.ratio=1)
gg2 <- ggplot(mtcars)+
geom_point(aes(x=mpg,y=hp,fill=cyl))+
facet_wrap(~cyl,ncol=2)+
theme_bw()+
theme(aspect.ratio=1,
legend.position='none')
output <- plot_grid(gg1,gg2, labels = c('A','B'),label_size = 20)
print(output)
Run Code Online (Sandbox Code Playgroud)
如您所见,水平轴既不匹配,也不匹配面板的上边缘.
这个论点align从cowplot不方位图工作.
有任何想法吗?
使用https://gist.github.com/low-decarie/5886616上的代码 可以生成双树枝状瓷砖图:
dual_dendogram_tile_plot(as.matrix(USArrests),main ="USA Arrests")

问题:将垂直树形图与瓷砖绘图区域对齐.(和/或改善水平树状图的对齐)
这个问题涉及到:
我想用相同的图例在同一页面上绘制两个ggplot2.http://code.google.com/p/gridextra/wiki/arrangeGrob描述了如何执行此操作.这看起来不错.但是......在我的例子中,我有两个具有相同x轴和不同y轴的图.当y轴的范围至少比另一个图的10倍(例如10000而不是1000)时,ggplot2(或网格?)不能使图正确对齐(参见下面的输出).
如何使用两个不同的y轴对齐绘图的左侧?
x = c(1, 2)
y = c(10, 1000)
data1 = data.frame(x,y)
p1 <- ggplot(data1) + aes(x=x, y=y, colour=x) + geom_line()
y = c(10, 10000)
data2 = data.frame(x,y)
p2 <- ggplot(data2) + aes(x=x, y=y, colour=x) + geom_line()
# Source: http://code.google.com/p/gridextra/wiki/arrangeGrob
leg <- ggplotGrob(p1 + opts(keep="legend_box"))
legend=gTree(children=gList(leg), cl="legendGrob")
widthDetails.legendGrob <- function(x) unit(3, "cm")
grid.arrange(
p1 + opts(legend.position="none"),
p2 + opts(legend.position="none"),
legend=legend, main ="", left = "")
Run Code Online (Sandbox Code Playgroud)

我在同一页面上有两个ggplots,我希望它们的面板宽度相同.
一些样本数据:
dfr1 <- data.frame(
time = 1:10,
value = runif(10)
)
dfr2 <- data.frame(
time = 1:10,
value = runif(10, 1000, 1001)
)
Run Code Online (Sandbox Code Playgroud)
一个低于另一个的情节:
p1 <- ggplot(dfr1, aes(time, value)) + geom_line()
p2 <- ggplot(dfr2, aes(time, value)) + geom_line()
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 1)))
print(p1, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
print(p2, vp = viewport(layout.pos.row = 2, layout.pos.col = 1))
Run Code Online (Sandbox Code Playgroud)
如何指定每个图中的面板宽度和位置,以使它们对齐?
(我不想将图表与刻面结合起来;这在我的实际例子中是不合适的.)
我在这里问了一个关于电网排列的问题并得到了很好的回应.我想现在减少绘图之间的空间但是会出错.首先,我介绍有效的代码,然后是错误代码(我尝试过的).我实际上找不到grid.arrange并且一直认为它来自gridExtra但我可能是不正确的.
所以2部分:
grid.arrange(Baptiste我知道你维护gridExtra所以如果我没有按照预期的方式使用它,请纠正我的想法或使用包.)好代码坏空间
require(ggplot2);require(gridExtra)
A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +
coord_flip() + ylab("")
B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip()
gA <- ggplot_gtable(ggplot_build(A))
gB <- ggplot_gtable(ggplot_build(B))
maxWidth = grid::unit.pmax(gA$widths[2:3], gB$widths[2:3])
gA$widths[2:3] <- as.list(maxWidth)
gB$widths[2:3] <- as.list(maxWidth)
grid.arrange(gA, gB, ncol=1)
Run Code Online (Sandbox Code Playgroud)
糟糕的代码(我的尝试)
require(ggplot2);require(gridExtra)
A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +
coord_flip() + ylab("") + theme(plot.margin= unit(1, "cm"))
B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip()
gA <- ggplot_gtable(ggplot_build(A))
gB <- ggplot_gtable(ggplot_build(B))
maxWidth …Run Code Online (Sandbox Code Playgroud)