如何制作主散点图和两个直方图组合的ggplot?

问题描述 投票:0回答:1

我有一个数据集

df
我想用散点图进行分析和可视化以与直方图相关。此外,一个的颜色数据点为蓝色,另一个的颜色数据点为黄色,但两者的非零对都为绿色。对于那些绿色的,我想建立关联。实际上,零是
NAs
,但我喜欢情节中的它们。下面是生成示例的脚本:

n <- 300; percent_zeros <- 0.2; n_zeros <- percent_zeros * n
set.seed(123)  
x <- runif(n, min = 0, max = 0.6)  
noise <- runif(n, min = -0.05, max = 0.05)  
y <- 0.8 * x + noise  
y <- pmax(0, pmin(0.6, y))
zero_indices_x <- sample(1:n, n_zeros)   
zero_indices_y <- sample(setdiff(1:n, zero_indices_x), n_zeros)  
x[zero_indices_x] <- 0
y[zero_indices_y] <- 0
non_zero_indices <- which(x != 0 & y != 0)
df <- data.frame(x,y,color = NA)
df$color <- ifelse(x != 0, "yellow", df$color)
df$color <- ifelse(y != 0, "blue", df$color)
df$color <- ifelse(x != 0 & y != 0, "green", df$color)
 
plot(df$x, df$y, main = NULL,
     xlab = NULL, ylab = NULL, pch = 19, col = df$color)
abline(lm(df$y ~ df$x), col = "red", lwd = 2)
abline(lm(y[x != 0 & y != 0] ~ x[x != 0 & y != 0]), col = "green", lwd = 2)

hist(x, breaks = 50, col = "yellow", border = "white", xlab = NULL, main = NULL)
hist(y, breaks = 50, col = "blue", border = "white", xlab = NULL, main = NULL)

但我希望它看起来像这样: enter image description here 绿线是非零对的相关性;如果能有相关系数和p值就完美了。 谁能帮我编写一个漂亮的 ggplot 代码吗?

r dataframe ggplot2 correlation
1个回答
0
投票

这是实现您想要的结果的一种选择,它使用

ggplot2
创建三个图,并使用
patchwork
将它们组合起来。另外,我使用
ggpubr::stat_cor
:

添加了相关系数和 p 值
library(ggplot2)
library(patchwork)
library(ggpubr)

p1 <- ggplot(df, aes(x, y, color = I(color))) +
  geom_point() +
  geom_smooth(se = FALSE, method = "lm", color = "red") +
  geom_smooth(
    data = ~ subset(., x > 0 & y > 0), se = FALSE,
    method = "lm", color = "green"
  ) +
  ggpubr::stat_cor(
    data = ~ subset(., x > 0 & y > 0), se = FALSE,
    color = "green",
    label.x.npc = .25,
    geom = "label"
  )
#> Warning in ggpubr::stat_cor(data = ~subset(., x > 0 & y > 0), se = FALSE, :
#> Ignoring unknown parameters: `se`

p2 <- ggplot(df, aes(y = y)) +
  geom_histogram(fill = "yellow") +
  scale_x_reverse()

p3 <- ggplot(df, aes(x)) +
  geom_histogram(fill = "blue") +
  scale_y_reverse()

design <-
"
BA
#C
"

list(p1, p2, p3) |>
  wrap_plots(design = design)
#> `geom_smooth()` using formula = 'y ~ x'
#> `geom_smooth()` using formula = 'y ~ x'
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

© www.soinside.com 2019 - 2024. All rights reserved.