我有大量带有分类变量的数据,这些变量具有“很多”级别。这给我绘制它们带来了一些困难,特别是当我将它们放入Rmarkdown
时。我正在尝试找出最好的处理方法,我发现
ggplot2
和 plotly
的组合可能会对我有所帮助。在我提供的示例中,我能够放大 x 轴 - 并区分彼此的类别级别。我想要一些关于如何更好地将我的次要情节与我的主要情节结合起来的建议,请参阅下面的代码:
我想知道是否需要将这两个图合并为一个
,但不知道如何做得更好。
# source: https://devcodef1.com/news/1304946/r-markdown-slider-zoom-plots
library(plotly)
set.seed(123)
n_var=70
N=400
data<-data.table(var1=as.character(sample(1:n_var, replace=T,size=N)),
var2=sample(1:10, replace=T, size=N),
response=rnorm(N),
exposure=rgamma(N, 1))
dt1 <- data[, .(factor=mean(response),
exposure=sum(exposure)),
, by=var1][order(-var1)]
dt2 <- data[, .(factor=mean(response),
exposure=sum(exposure)),
, by=var2][order(-var2)]
# setup for categorical variable
p <- ggplot(dt, aes(x = var1, y = factor)) +
geom_point() +
labs(x = "var1", y = "response")
p <- ggplotly(p)
p <- plotly_build(p)
p$x$layout$hovermode <- "closest"
e <- ggplot(dt, aes_string(x = "var1", y = "exposure")) +
geom_bar(stat = "identity", fill = "steelblue2",
size = 0.8, alpha = .4) +
labs(x = "", y = "exposure") +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_line(color = NA))
subplot(p, e, nrows = 2, margin = 0.04, heights = c(0.8, 0.2))
# setup for continuous variable
p <- ggplot(dt2, aes(x = var2, y = factor)) +
geom_line() +
labs(x = "var2", y = "response")
p <- ggplotly(p)
p <- plotly_build(p)
e <- ggplot(data = data, aes(x = var2, weight = exposure)) +
geom_density(fill = "steelblue2", size = 0.8, alpha = .4) +
labs(x = "") +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_line(color = NA))
subplot(p, e, nrows = 2, margin = 0.04, heights = c(0.8, 0.2))
p <- ggplot(dt1, aes(x = var1, y = factor)) +
geom_point() +
labs(x = "var1", y = "response")
e <- ggplot(dt1, aes(x = var1, y = exposure)) +
geom_bar(stat = "identity", fill = "steelblue2", alpha = 0.4) +
labs(x = "", y = "exposure")
# Convert to plotly objects
p_plotly <- ggplotly(p)
e_plotly <- ggplotly(e)
# Combine plots and share x-axis
subplot(p_plotly, e_plotly, nrows = 2, shareX = TRUE, heights = c(0.8, 0.2))