Reputation: 1131
I'm trying to draw a graph which has three lines using ggplot. But then I want to be able to toggle the lines on or off so that when I', looking at an overcrowded plot I can just hide some of the data series. I can't seem to figure out how to do this (or even if its possible) with rstudio's manipulate package which includes checkboxes. Can someone please show me how to do this with this package or any other? Thank you
Code:
manipulate((ggplot(MeanFrameMelt, aes(x=variable, y=value,
color=id))+ geom_point()),
id = checkbox(FALSE, "File1"))
MeanFrameMelt (data):
id variable value
1 file1 V1 0.04114207
2 file2 V1 0.31830645
3 file3 V1 0.05797068
4 file1 V2 0.04138554
5 file2 V2 0.31510753
6 file3 V2 0.05830449
7 file1 V3 0.04157882
8 file2 V3 0.31220430
9 file3 V3 0.05865419
10 file1 V4 0.04177334
11 file2 V4 0.31117608
12 file3 V4 0.05900918
Upvotes: 0
Views: 1138
Reputation: 49650
Here is one approach that uses the tkexamp
function from the TeachingDemos package rather than manipulate
:
library(TeachingDemos)
tklist <- rep( list(list('checkbox',init="T")), 3 )
names(tklist) <- levels( MeanFrameMelt$id )
tkfun <- function(...) {
w <- c(...)
w2 <- names(w)[w]
df <- MeanFrameMelt[ MeanFrameMelt$id %in% w2, ]
print(ggplot(df, aes(x=variable, y=value,
color=id)) + geom_point() )
}
tkexamp( tkfun, tklist )
Or it looks a little nicer if the last line is:
tkexamp( tkfun, list(id=tklist), plotloc='left' )
Someone else with more familiarity with ggplot2 will need to chime in on how to modify this so that the colors stay the same and optionally the y limits stay the same.
Edit
Here is a version of the function to keep the colors and the y limits the same even when not displaying some of the values:
tkfun <- function(...) {
w <- c(...)
w2 <- names(w)[w]
df <- MeanFrameMelt[ MeanFrameMelt$id %in% w2, ]
print(ggplot(df, aes(x=variable, y=value, colour=id)) + geom_point() +
scale_colour_discrete(drop=FALSE) +
ylim(range(MeanFrameMelt$value))
)
}
Upvotes: 1
Reputation: 5700
This is not really a manipulate question, but rather a question of you have 3 Boolean values to use when creating your plot expression. How to do it? The checkbox
call creates these. The following doesn't exactly answer your specific question, but shows how to make this less confusing -- just add the lines one by one at the expense of being elegant. This could be done with RStudio, but I'm using a version of manipulate that works without RStudio:
require(gWidgets2) ## from github
options(guiToolkit="Qt") ## other choices too
source(system.file("examples", "manipulate.R", package="gWidgets2"))
manipulate({
plot(mpg ~ wt, mtcars)
if(do_lm)
abline(lm(mpg ~ wt, mtcars))
if(do_loess)
with(mtcars, lines(lowess(wt, mpg)))
## ...
},
do_lm=checkbox("Add regression line"),
do_loess = checkbox("Add lowess fit")
)
Upvotes: 2
Reputation: 21532
Here's one thing I figured out, based on Rstudio's example bar plot:
manipulate(
barplot(as.matrix(longley[,factor]),
beside = TRUE, main = factor),
factor = picker(1,2,2:3))
If you select the third option, 2:3
, it will plot both sets of data. Presumably the same sort of trickery can be used in other plot types. I'd be interested in seeing what others can come up with.
Update: the following works, albeit a bit clunky:
manipulate(matplot(foo[,1],foo[,c(which(c(fp2,fp3)==1))],t='l'),
fp2 = checkbox(TRUE,'col2') ,
fp3 = checkbox(TRUE,'col3'))
You'll need an fp4
line if you want 3 checkboxes per your comment.
Note -- I have a question posted in Rstudio's forum because I got error messages when trying to assign checkbox outputs to a vector, i.e. fpick[4]=checkbox(TRUE,'col4')
Upvotes: 0