R:使用 ggplot 和plotly 将子图组合成一个有机体

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

我有大量带有分类变量的数据,这些变量具有“很多”级别。这给我绘制它们带来了一些困难,特别是当我将它们放入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))

我不知道 
r ggplot2 plotly subplot
1个回答
0
投票
参数,这对我有用:

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))


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