gpt4 book ai didi

r - 在 Shiny 中叠加两个 ggplot

转载 作者:行者123 更新时间:2023-12-04 10:09:10 26 4
gpt4 key购买 nike

我有一个非常大的数据集,我正在使用 ggplot 在 Shiny 上绘制。我有一个与 x 轴上的值相关联的 slider ,我想用它为选定的数据子集重新着色,并让其余数据保持原样。

最简单的选择是重新创建整个绘图,但由于它是一个大型数据集,所以这是一个非常缓慢的过程。作为替代方案,我正在考虑让原始图保持原样,并在其上面放置另一个图,其中包含 plot.background = 'none' 和仅彩色点。我无法通过 server.r 中的 renderPlotui.r 中的 plotOutput 实现此目的-

服务器.r:

output$Plot1= renderPlot({
ggplot(mtcars) + geom_point(aes(x = mpg, y = wt))
})

output$Plot2= renderPlot({
ggplot(mtcars[mtcars$mpg > 15,]) + geom_point(aes(x = mpg, y = wt), color = 'Efficient')
})

ui.r(我不知道如何将 Plot2 放入其中):

fluidRow(
class = 'Plot1Row',
column(
plotOutput(
"Plot1",
width = '100%'
),
width = 12
)
)

有什么建议吗?

最佳答案

这是一个解释我的评论的例子:

library(ggplot2)
p <- ggplot(mtcars[mtcars$mpg > 15,]) + geom_point(aes(x = mpg, y = wt), color = 'green')
p

#produce an otherwise identical gtable with a subset of points with different color
p1 <- ggplot(mtcars[mtcars$mpg > 15,]) + geom_blank(aes(x = mpg, y = wt), color = 'green') +
geom_point(data = mtcars[mtcars$mpg > 25,], aes(x = mpg, y = wt), color = 'blue')
g1 <- ggplot_gtable(ggplot_build(p1))

library(gtable)
#find the correct viewport
seekViewport(grid.grep("panel", grep = TRUE)$name)
#draw just the points
grid.draw(gtable_filter(g1, "panel")$grobs[[1]]$children$geom_point.points)

resulting plot

这不会重新绘制完整的图。由于我不是 shiny 用户,您必须自己测试如何在 shiny 中执行此操作。

关于r - 在 Shiny 中叠加两个 ggplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31746962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com