I would like to create an interactive histogram which gives information about the bins by hover. This thread gives answer to how to add tooltip with count numbers.
library("ggvis")
cocaine %>%
ggvis(x = ~weight) %>%
layer_histograms() %>%
add_tooltip(function(df) (paste("count:", df$stack_upr_ - df$stack_lwr_)))
How can I add the share of each bin as well? I should somehow add nrow(cocaine) to ggvis and create the shares from count but I did not succeed on how to achieve that (tried to take the suggestions of this post but apparently that solves a different problem).
You could do something like this I imagine:
cocaine %>%
ggvis(x = ~weight) %>%
layer_histograms() %>%
add_tooltip(function(df) paste("count:", df$stack_upr_, 'share:',
format(df$stack_upr_/nrow(cocaine), digits=2)))
This will show both the share and the bin number.
Also, as a side note you do not need df$stack_upr_ - df$stack_lwr_ because df$stack_lwr_ will be zero. Just df$stack_upr_ will do.
Related
I am trying to interactively show both points and boxplots of the same data in a ggplotly situation.
"dodged" positioning does the job in ggplot, but when passing to plotly positioning goes off--how do I get boxes and points to line up? (Essentially throwing points on top of this question. I also realize that an answer to this question would likely also be an answer to my question, though there may be more answers for my issue.)
What I want is for both layers to show up together, even when a group is missing at a location (either centered or in the group location), for examply like so:
What I get with interactivity so far is this:
library(plotly)
mtcars_boxplot <- mtcars %>%
mutate(cyl=as.factor(cyl)) %>%
mutate(vs=as.factor(vs)) %>%
ggplot(aes(y=mpg, x=cyl)) +
geom_boxplot(aes(color=vs), position=position_dodge())+
geom_point(aes(color=vs), position=position_jitterdodge(), size = 0.5)
mtcars_boxplot %>%
ggplotly() %>%
layout(boxmode='group')
You can see that for cyl=8, the points are centered, but the box shows up in its group's location.
My question is: how do I get an interactive version of the first image, or something similar (preferably using ggplotly)?
I found a way to do this--not with ggplot, but pure plotly:
mtcars_boxplot <- mtcars %>%
mutate(cyl=as.factor(cyl)) %>%
mutate(vs=as.factor(vs)) %>%
plot_ly(type="box",
x = ~cyl,
y = ~mpg,
color = ~vs,
alignmentgroup = ~MOTART,
boxpoints = "all",
pointpos = 0,
jitter = 1) %>%
layout(boxmode='group')
If there is a ggplotly-answer, I would still love to know that one. (This actually ends up aligning more nicely, but is also more work when working in ggplot otherwise.)
I'm working with reefs across a large area and trying to visualize them quickly such that I can pick a smaller subset to work on initially. To do this, I have taken my spatial points, transformed them to LineString to make the mapping quicker and then used mapview to display them. The code I have is below:
data_as_sf %>%
filter(reef_name %in% subgroup) %>%
group_by(.dots=c("reef_name", "reef_section")) %>%
summarize() %>%
st_cast("LINESTRING") %>%
mapview()
A zoomed in sample of the mapview generated is here:
If anyone can provide advice such that I can
make the lines bigger/thicker so they are more easily seen when
zoomed out, and
have the lines colored by the factor "reef_name"
it would really help.
As answered in the comments by TimSalabim, have now updated the code to be
data_as_sf %>%
filter(reef_name %in% subgroup) %>%
group_by(.dots=c("reef_name", "reef_section")) %>%
summarize() %>%
st_cast("LINESTRING") %>%
mapview(lwd = 3, zcol = "reef_name")
which works just right.
I have a bar chart like this
iris %>% ggvis(~Species, ~Petal.Width)
and I would like to use handle_click() to change the fill of a bar to red when one is clicked. For example, if I clicked on versicolor, it should look like this
iris %>% ggvis(~Species, ~Petal.Width) %>% layer_bars(fill=~Species) %>% scale_nominal("fill", range = c("#333333","red", "#333333"))
I know I need to handle the click event, but I wasn't able to understand how by looking at the documentation. I have started with this, but I have no clue how to set up the barRed():
barRed <- function(data, location, session, ...) {
}
iris %>% ggvis(~Species, ~Petal.Width) %>%
handle_click(on_click=barRed)
I want to use ggvis for data exploration (because I am familiar with ggplot2), and it would be of great help, due to many groups in my data sets, to be able to select and unselect groups to make different specific (data) comparisons.
set.seed(10)
dat <- data.frame(x=c(1:3,1:3),y=rnorm(6),groups=factor(rep(1:2,each=3)))
library(ggvis)
dat %>% ggvis(~x, ~y) %>% layer_points(fill=~groups)
# i know this example does not work - but is that possible somehow?
dat %>% ggvis(~x, ~y) %>% layer_points(fill=input_checkbox(~groups))
What I want at the end, is a graphic with which I can select a subset of groups by using radio buttons (for example).
Is there any way to do that?
Thank you!
From the documentation
Limitations
Currently, interactive inputs can only be used in two places:
as arguments to transforms: layer_smooths(span = input_slider(0, 1))
as properties: props(size = input_slider(10, 1000))
This means that interactive inputs can only modify the data, not the
underlying plot specification. In other words, with only basic
interactivity there’s no way to add or remove layers, or switch
between different datasets. This is a reasonable limitation because if
you’re doing exploration you can always create a new ggvis with R
code, or if you’re polishing a plot for presentation, you can embed it
in a Shiny app and gain full control over the plot.
For simple data exploration, one idea could be to use filter():
set.seed(10)
dat <- data.frame(x = c(1:5,1:5,1:5),
y = rnorm(15), groups = factor(rep(1:5,each=3)))
Note: I edited your initial dataset to illustrate the concept with more groups
library(dplyr)
library(ggvis)
dat %>% ggvis(~x, ~y) %>% layer_points(fill = ~groups)
You could pass the arguments (either manually directly in your R code or with your radio buttons in a Shiny app) to filter() to isolate specific groups:
dat %>%
filter(groups == 1 | groups == 3) %>%
ggvis(~x, ~y) %>% layer_points(fill = ~groups)
Hi I'm a little confused with the scales in ggvis.
I'm trying to do two things: one is have a log scale (the equivalent of log="x" in plot()). I'm also looking for the equivalent of xlim=c(). In both cases, the code below is not giving the expected results.
# install.packages("ggvis", dependencies = TRUE)
library(ggvis)
df <- data.frame(a=c(1, 2, 3, 1000, 10000), b=c(0.1069, 0.0278, 0.0860, 15.5640, 30.1745))
df %>% ggvis(~a, ~b)
df %>% ggvis(~a, ~b) %>% scale_numeric("x", trans="log")
Notice that with trans="log", all dots are on the left of the plot and the scale disappears.
Next, I want to restrict the plot to certain values. I could subset the data frame but I'm looking to have the equivalent of xlim from plot().
df %>% ggvis(~a, ~b) %>% scale_numeric("x", trans="linear", domain=c(10, 40))
This is giving even weirder results, so I'm guessing I might be misinterpreting what domain does.
Thanks for your help!
I've encountered the same problem that you've mentioned.
Apparently, the developer of ggvis noticed this bug as well. This is currently marked as an issue. You can find the issue here: https://github.com/rstudio/ggvis/issues/230
In the context of your question:
# install.packages("ggvis", dependencies = TRUE)
library(ggvis)
df <- data.frame(a=c(1, 2, 3, 1000, 10000), b=c(0.1069, 0.0278, 0.0860, 15.5640, 30.1745))
df %>% ggvis(~a, ~b)
# Points will disapper
df %>% ggvis(~a, ~b) %>% scale_numeric("x", trans="log")
# Should work
df %>% ggvis(~a, ~b) %>% scale_numeric("x", trans="log", expand=0)
However, you may notice that after the transformation, the spacing between ticks don't appear to be uniform. But at least the dots are rendered correctly.
I ran into the same issue and noticed that as soon as I removed the 0 data points from my data (of which log() cannot be computed) everything started to work fine.
This is strange, I've now tried a lot of things with your data, but can't find the problem.
The I tested it on the Violent Crime Rates by US State (see help(USArrests)) and it worked like a charm.
data(USArrests)
# str(USArrests)
p <- USArrests %>% ggvis(~ Murder, ~ Rape) %>% layer_points()
p %>% scale_numeric("y", trans = "log")
This is not an answer, simply to share this with you.