我在連續和整數變數的散點圖上包含邊際分布圖。但是,在整數變數邊際分布圖(y 軸)中,由于 y 值都是整數,所以會出現這種鋸齒形圖案。有沒有辦法增加函式計算分布密度的箱/值的“寬度”(不確定這是正確的術語)?
目標是擺脫由于 y 值是整數而形成的鋸齒形模式。
library(GlmSimulatoR)
library(ggplot2)
library(patchwork)
### Create right-skewed dataset that has one continous variable and one integer variable
set.seed(123)
df1 <- data.frame(matrix(ncol = 2, nrow = 1000))
x <- c("int","cont")
colnames(df1) <- x
df1$int <- round(rgamma(1000, shape = 1, scale = 1),0)
df1$cont <- round(rgamma(1000, shape = 1, scale = 1),1)
p1 <- ggplot(data = df1, aes(x = cont, y = int))
geom_point(shape = 21, size = 2, color = "black", fill = "black", stroke = 1, alpha = 0.4)
xlab("Continuous Value")
ylab("Integer Value")
theme_bw()
theme(panel.grid = element_blank(),
text = element_text(size = 16),
axis.text.x = element_text(size = 16, color = "black"),
axis.text.y = element_text(size = 16, color = "black"))
dens1 <- ggplot(df1, aes(x = cont))
geom_density(alpha = 0.4)
theme_void()
theme(legend.position = "none")
dens2 <- ggplot(df1, aes(x = int))
geom_density(alpha = 0.4)
theme_void()
theme(legend.position = "none")
coord_flip()
dens1 plot_spacer() p1 dens2
plot_layout(ncol = 2, nrow = 2, widths = c(6,1), heights = c(1,6))
uj5u.com熱心網友回復:
來自?geom_density:
調整:乘法[原文如此]帶寬調整。這使得在仍然使用帶寬估計器的同時調整帶寬成為可能。例如,“adjust = 1/2”表示使用默認帶寬的一半。
因此,首先嘗試例如geom_density(..., adjust = 2)(帶寬是默認值的兩倍)然后從那里開始。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/430477.html
上一篇:R:使用rnorm()忽略NA
