【问题标题】:Scatterplot with left and bottom histograms in ggplot2ggplot2中带有左侧和底部直方图的散点图
【发布时间】:2014-10-26 10:24:55
【问题描述】:

如何创建带有左侧和底部直方图的散点图,就像下面 ggplot2 中的示例一样?

library(ggplot2)
library(gridExtra)

data1<-diamonds
detrend<-lm(log(price)~log(carat),data=data1)
data1$lprice2<-resid(detrend)

empty <- ggplot()+geom_point(aes(1,1), colour="white")+
     opts(axis.ticks=theme_blank(), 
     panel.background=theme_blank(), 
     axis.text.x=theme_blank(), axis.text.y=theme_blank(),           
     axis.title.x=theme_blank(), axis.title.y=theme_blank())

scatter<-qplot(log(carat),lprice2,data=data1,xlab="Weight",ylab="Price Residuals",
     colour=factor(color),main="Diamonds - Weight to Price by Color")
scatter<-scatter+theme(legend.position="top")
scatter<-scatter+theme(plot.title=element_text(size=20,colour="blue"))


hist_left<-ggplot(data1,aes(x=price, fill=color))+geom_histogram(aes(y = ..density..))+
theme(legend.position = "none")+coord_flip()

hist_bottom<-ggplot(data1,aes(x=carat, fill=color))+geom_histogram()
 +theme(legend.position =     "none")

如何使用grid.arrange来排列这些情节以及如何像图片一样翻转hist_left?

【问题讨论】:

  • 谢谢!但我使用 grid.arrange(arrangeGrob(hist_left,scatter,ncol=2,widths=c(3,1)),arrangeGrob(hist_bottom,ncol=2,widths=c(3,1)), heights=c(1 ,3)) 并且不能绘制图片。

标签: r


【解决方案1】:

您还应该删除空白占位符图中的 panel.grid 并切换到 element_blanktheme_blank。此外,请删除您的 hist_left 上的标签。

library(ggplot2)
library(gridExtra)

data1 <- diamonds
detrend <- lm(log(price)~log(carat) ,data=data1)
data1$lprice2 <- resid(detrend)

empty <- ggplot()
empty <- empty + geom_point(aes(1,1), colour="white")
empty <- empty + theme(axis.ticks=element_blank(), 
                       panel.background=element_blank(), 
                       axis.text.x=element_blank(), 
                       axis.text.y=element_blank(),           
                       axis.title.x=element_blank(), 
                       axis.title.y=element_blank(),
                       panel.grid=element_blank())

scatter <- qplot(log(carat), lprice2, data=data1, 
                 xlab="Weight", ylab="Price Residuals",
                 colour=factor(color),
                 main="Diamonds - Weight to Price by Color")
scatter <- scatter + theme(legend.position="top")
scatter <- scatter + theme(plot.title=element_text(size=20, colour="blue"))

hist_left <- ggplot(data1,aes(x=price, fill=color))
hist_left <- hist_left + geom_histogram(aes(y = ..density..))
hist_left <- hist_left + labs(x=NULL, y=NULL, title=NULL)
hist_left <- hist_left + theme(legend.position = "none")

hist_bottom <- ggplot(data1, aes(x=carat, fill=color))
hist_bottom <- hist_bottom + geom_histogram()
hist_bottom <- hist_bottom + theme(legend.position = "none")

grid.arrange(arrangeGrob(hist_left + coord_flip(), scatter, ncol=2, widths=c(1,3)),
             arrangeGrob(empty, hist_bottom, ncol=2, widths=c(1,3)),
             heights=c(3,1))

您可以通过scale_x_reverse 接近您的目标:

grid.arrange(arrangeGrob(hist_left + scale_x_reverse(), scatter, ncol=2, widths=c(1,3)),
             arrangeGrob(empty, hist_bottom, ncol=2, widths=c(1,3)),
             heights=c(3,1))

而且,您可以尝试通过转换 hist_left' to a grob witheditGrob` 并使用视口参数(例如,但请注意这不是您想要的)来获取您的确切图像:

hlg <- ggplotGrob(hist_left)
hlg <- editGrob(hlg, vp=viewport(angle=90))

但您需要复习网格图形以弄清楚您想如何操作 grob 表组件。

【讨论】:

    【解决方案2】:

    试试这个作为起点:

    grid.arrange(hist_left, scatter, empty, hist_bottom, 
                 widths=c(1, 4), as.table=FALSE, nrow=2)
    

    【讨论】:

      猜你喜欢
      • 2019-10-23
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 2014-06-29
      相关资源
      最近更新 更多