簡體   English   中英

從R中的同一變量繪制邊際直方圖(作為因子)和散點圖(作為數字)

[英]Plotting marginal histograms (as factors) and scatterplot (as numeric) from the same variable in R

我正在嘗試用這個問題的邊緣直方圖創建散點圖。 我的數據是兩個(數字)變量,它們共享七個離散的(某種程度上)對數間隔的水平。

我已經成功地做到了這一點的幫助下ggMarginalggExtra包,但是我不是很滿意這個結果,繪制使用相同的數據作為散點圖的邊際直方圖時一樣,東西不用排隊。 如下所示,直方圖條在數據點本身的右側或左側略有偏移。

library(ggMarginal)
library(ggplot2)
x <- rep(log10(c(1,2,3,4,5,6,7)), times=c(3,7,12,18,12,7,3))
y <- rep(log10(c(1,2,3,4,5,6,7)), times=c(3,1,13,28,13,1,3))
d <- data.frame("x" = x,"y" = y)
p1 <- ggMarginal(ggplot(d, aes(x,y)) + geom_point() + theme_bw(), type = "histogram")

P1

一個可能的解決方案是將直方圖中使用的變量更改為因子,以便它們與散點圖軸很好地對齊。 當使用ggplot創建直方圖時,這很好用:

p2 <- ggplot(data.frame(lapply(d, as.factor)), aes(x = x)) + geom_histogram()

P2

但是,當我嘗試使用ggMarginal執行此操作ggMarginal ,沒有得到預期的結果-似乎ggMarginal直方圖仍將變量視為數字。

p3 <- ggMarginal(ggplot(d, aes(x,y)) + geom_point() + theme_bw(),
                 x = as.factor(x), y = as.factor(y), type = "histogram")

P3

如何確保直方圖條在數據點上居中?

我絕對願意接受不涉及使用ggMarginal

不知道在這里復制我對您提到的問題的回答是否是一個好主意,但我仍然無權發表評論,否則請通知我。

我發現該軟件包( ggpubr )似乎可以很好地解決此問題,並且考慮了顯示數據的幾種可能性。

該包的鏈接在這里 ,在此鏈接中,您將找到一個使用它的不錯的教程。 為了完整起見,我附上我復制的示例之一。

我首先安裝了軟件包(需要devtools

if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/ggpubr")

對於不同的組顯示不同直方圖的具體實例中,它提到關於與ggExtra :“的一個限制ggExtra之處在於它不能與在散點圖中的多個組和邊際曲線應付在下面將R代碼,我們使用cowplot軟件包提供解決方案。” 就我而言,我必須安裝后一個軟件包:

install.packages("cowplot")

我遵循了這段代碼:

# Scatter plot colored by groups ("Species")
sp <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width",
            color = "Species", palette = "jco",
            size = 3, alpha = 0.6)+
border()                                         
# Marginal density plot of x (top panel) and y (right panel)
xplot <- ggdensity(iris, "Sepal.Length", fill = "Species",
               palette = "jco")
yplot <- ggdensity(iris, "Sepal.Width", fill = "Species", 
               palette = "jco")+
rotate()
# Cleaning the plots
sp <- sp + rremove("legend")
yplot <- yplot + clean_theme() + rremove("legend") 
xplot <- xplot + clean_theme() + rremove("legend")
# Arranging the plot using cowplot
library(cowplot)
plot_grid(xplot, NULL, sp, yplot, ncol = 2, align = "hv", 
      rel_widths = c(2, 1), rel_heights = c(1, 2))

對我來說效果很好:

虹膜設置邊際直方圖散點圖

如果您願意嘗試進行底圖繪制,則可以使用以下功能:

plots$scatterWithHists <- function(x, y, histCols=c("lightblue","lightblue"), lhist=20, xlim=range(x), ylim=range(y), ...){
  ## set up layout and graphical parameters
  layMat <- matrix(c(1,4,3,2), ncol=2)
  layout(layMat, widths=c(5/7, 2/7), heights=c(2/7, 5/7))
  ospc <- 0.5                                                  # outer space
  pext <- 4                                                    # par extension down and to the left
  bspc <- 1                                                    # space between scatter plot and bar plots
  par. <- par(mar=c(pext, pext, bspc, bspc), oma=rep(ospc, 4)) # plot parameters

  ## barplot and line for x (top)
  xhist <- hist(x, breaks=seq(xlim[1], xlim[2], length.out=lhist), plot=FALSE)
  par(mar=c(0, pext, 0, 0))
  barplot(xhist$density, axes=FALSE, ylim=c(0, max(xhist$density)), space=0, col=histCols[1])

  ## barplot and line for y (right)
  yhist <- hist(y, breaks=seq(ylim[1], ylim[2], length.out=lhist), plot=FALSE)
  par(mar=c(pext, 0, 0, 0))
  barplot(yhist$density, axes=FALSE, xlim=c(0, max(yhist$density)), space=0, col=histCols[2], horiz=TRUE)

  ## overlap
  dx <- density(x)
  dy <- density(y)
  par(mar=c(0, 0, 0, 0))
  plot(dx, col=histCols[1], xlim=range(c(dx$x, dy$x)), ylim=range(c(dx$y, dy$y)),
       lwd=4, type="l", main="", xlab="", ylab="", yaxt="n", xaxt="n", bty="n"
       )
  points(dy, col=histCols[2], type="l", lwd=3)

  ## scatter plot
  par(mar=c(pext, pext, 0, 0))
  plot(x, y, xlim=xlim, ylim=ylim, ...)
}

做就是了:

scatterWithHists(x,y, histCols=c("lightblue","orange"))

你會得到:

marginalHists

如果您絕對要使用ggMargins查找xparamsyparams 它說您可以使用這些參數將附加參數發送到x-margin和y-margin。 我只是成功發送了一些瑣碎的東西,例如顏色。 但是也許發送類似xlim東西會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM