有没有办法用边缘直方图创建散点图,就像下面的示例一样ggplot2?在Matlab中它是scatterhist()函数,并且R也存在等价物.但是,我还没有看到ggplot2.

我开始尝试创建单个图形,但不知道如何正确排列它们.
require(ggplot2)
x<-rnorm(300)
y<-rt(300,df=2)
xy<-data.frame(x,y)
xhist <- qplot(x, geom="histogram") + scale_x_continuous(limits=c(min(x),max(x))) + opts(axis.text.x = theme_blank(), axis.title.x=theme_blank(), axis.ticks = theme_blank(), aspect.ratio = 5/16, axis.text.y = theme_blank(), axis.title.y=theme_blank(), background.colour="white")
yhist <- qplot(y, geom="histogram") + coord_flip() + opts(background.fill = "white", background.color ="black")
yhist <- yhist + scale_x_continuous(limits=c(min(x),max(x))) + opts(axis.text.x = theme_blank(), axis.title.x=theme_blank(), axis.ticks = theme_blank(), aspect.ratio = 16/5, axis.text.y = theme_blank(), axis.title.y=theme_blank() )
scatter <- qplot(x,y, data=xy) + scale_x_continuous(limits=c(min(x),max(x))) + scale_y_continuous(limits=c(min(y),max(y)))
none <- qplot(x,y, data=xy) + geom_blank()
Run Code Online (Sandbox Code Playgroud)
并使用 …
使用https://gist.github.com/low-decarie/5886616上的代码 可以生成双树枝状瓷砖图:
dual_dendogram_tile_plot(as.matrix(USArrests),main ="USA Arrests")

问题:将垂直树形图与瓷砖绘图区域对齐.(和/或改善水平树状图的对齐)
这个问题涉及到:
在上一个问题中,我询问了如何在用户坐标中获取边距线位置.我以line2user功能的形式收到了很好的答案.但是,当x或y轴在对数刻度上时,我无法弄清楚如何修改函数.
我做了一些修改以适应对数刻度轴:
line2user <- function(line, side, log = "") {
lh <- par('cin')[2] * par('cex') * par('lheight')
x_off <- diff(grconvertX(0:1, 'inches', 'user'))
y_off <- diff(grconvertY(0:1, 'inches', 'user'))
usr <- par('usr') ## Added by me
if (grepl("x", log)) usr[1:2] <- 10^usr[1:2] ## Added by me
if (grepl("y", log)) usr[3:4] <- 10^usr[3:4] ## Added by me
switch(side,
`1` = usr[3] - line * y_off * lh,
`2` = usr[1] - line * x_off * lh,
`3` = usr[4] …Run Code Online (Sandbox Code Playgroud) 在df下面,我想在每个方面重新排序从最高到最低的条形
我试过了
df <- df %>% tidyr::gather("var", "value", 2:4)
ggplot(df, aes (x = reorder(id, -value), y = value, fill = id))+
geom_bar(stat="identity")+facet_wrap(~var, ncol =3)
Run Code Online (Sandbox Code Playgroud)
它没有在每个方面从最高到最低排序.
我想出了另一种获得我想要的方法.我不得不一次绘制每个变量,然后使用组合所有绘图grid.arrange()
#I got this function from @eipi10's answer
#http://stackoverflow.com/questions/38637261/perfectly-align-several-plots/38640937#38640937
#Function to extract legend
# https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs
g_legend<-function(a.gplot) {
tmp <- ggplot_gtable(ggplot_build(a.gplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)
}
p1 <- ggplot(df[df$var== "A", ], aes (x = reorder(id, -value), y = value, fill = id))+
geom_bar(stat="identity") …Run Code Online (Sandbox Code Playgroud)