在闪亮中叠加两个ggplot [英] Overlay Two ggplot in Shiny

查看:53
本文介绍了在闪亮中叠加两个ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的数据集,正在使用ggplot在Shiny上进行绘制.我有一个与x轴上的值绑定的滑块,我希望通过该滑块为数据的选定子集重新着色,并让其余数据保持原样.

I have a very large dataset that I'm plotting on Shiny using ggplot. I have a a slider tied to the values on x axis with which I would like to recolor the selected subset of the data and let the rest of the data remain as is.

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

The simplest option is to recreate the entire plot but since it's a large dataset it's a very slow process. As an alternative, I was considering letting the original plot stay as is, and putting another plot on top of it with the plot.background = 'none' and just the coloured points. I'm unable to achieve this with the renderPlot in the server.r and plotOutput in the ui.r -

server.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):

ui.r (I don't know how to fit Plot2 in this):

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

有什么提示吗?

推荐答案

以下是解释我的评论的示例:

Here is an example explaining my comment:

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)

这不会重绘完整的图.由于我不是一个闪亮的用户,因此您必须测试一下如何做到闪亮.

This doesn't redraw the complete plot. Since I'm not a shiny user, you'll have to test yourself how to do this in shiny.

这篇关于在闪亮中叠加两个ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆