Reputation: 12905
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.
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 (I don't know how to fit Plot2 in this):
fluidRow(
class = 'Plot1Row',
column(
plotOutput(
"Plot1",
width = '100%'
),
width = 12
)
)
Any tips?
Upvotes: 3
Views: 1410
Reputation: 132969
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.
Upvotes: 1