legends on ggvis graph are overlaping when using tooltip - r

I'm generating a graph with ggvis and the legends are in top of each-other.
library(ggvis)
df1 <- data.frame(x=c(0.6,1,1.4), y=c(-2, -.8, -0.2), number=c(10,8,6),
type=c('A', 'A', 'B'))
df1 %>% ggvis(x = ~x, y = ~y) %>%
layer_points(shape=~type, fill=~number)
How can I fix this?
Thanks!
Steven's solution works for the simple example but It does not work when you add a tooltip:
library(ggvis)
df1 <- data.frame(x=c(0.6,1,1.4), y=c(-2, -.8, -0.2), number=c(10,8,6),
type=c('A', 'A', 'B'), id=c(1:3))
tooltip <- function(x) {
if(is.null(x)) return(NULL)
row <- df1[df1$id == x$id, ]
paste0(names(row), ": ", format(row), collapse = "<br />")
}
df1 %>% ggvis(x = ~x, y = ~y) %>%
layer_points(shape=~type, fill=~number, key := ~id) %>%
add_tooltip(tooltip, "hover") %>%
add_legend("shape", properties = legend_props(legend = list(y = 50)))

Try:
df1 %>% ggvis(x = ~x, y = ~y) %>%
layer_points(shape=~type, fill=~number) %>%
add_legend("shape", properties = legend_props(legend = list(y = 50)))
Edit:
As mentionned by #aosmith, you could use the set_options() workaround:
df1 %>% ggvis(x = ~x, y = ~y) %>%
layer_points(shape=~type, fill=~number, key := ~id) %>%
add_tooltip(tooltip, "hover") %>%
add_legend("shape", properties = legend_props(legend = list(y = 50))) %>%
set_options(duration = 0)

Related

Interactively select a grouping variable in plotly

How can I create a grouped bar chart in plotly that has a dropdown (or something else), so a viewer can select the grouping variable?
Working example:
library(dplyr)
library(plotly)
library(reshape2)
iris$Sepal.L <- iris$Sepal.Length %>%
cut(breaks = c(4,5,7,8),
labels = c("Length.a","Length.b","Length.c"))
iris$Sepal.W <- iris$Sepal.Width %>%
cut(breaks = c(1,3,5),
labels = c("Width.a","Width.b"))
# Get percentages
data1 <- table(iris$Species, iris$Sepal.L) %>%
prop.table(margin = 1)
data2 <- table(iris$Species, iris$Sepal.W) %>%
prop.table(margin = 1)
# Convert to df
data1 <- data.frame(Var1=row.names(data1), cbind(data1))
row.names(data1) <- NULL
data2 <- data.frame(Var1=row.names(data2), cbind(data2))
row.names(data2) <- NULL
plot_ly(
data = data1,
name = "Length.a",
x = ~Var1, y = ~Length.a,
type = "bar") %>%
add_trace(y=~Length.b, name = "Length.b") %>%
add_trace(y=~Length.c, name = "Length.c")
plot_ly(
data = data2,
name = "Width.a",
x = ~Var1, y = ~Width.a,
type = "bar") %>%
add_trace(y=~Width.b, name = "Width.b")
For example if I would like to select between viewing a plot with table(iris$Species, iris$Sepal.Length) and a plot with table(iris$Species, iris$Sepal.Width)
Bonus:
If it's easy; being able to interactively select the x variable as well would be cool, but not necessary.
You can find a solution here.
The idea is to plot your bar charts (with data1 and data2) all together and to make visible only one at a time.
items <- list(
list(label="Var1",
args=list(list(visible=c(T,T,T,F,F)))),
list(label="Var2",
args=list(list(visible=c(F,F,F,T,T))))
)
plot_ly(data=data1) %>%
add_bars(name = "Length.a",
x = ~Var1, y = ~Length.a, visible=T) %>%
add_bars(name = "Length.b",
x = ~Var1, y = ~Length.b, visible=T) %>%
add_bars(name = "Length.c",
x = ~Var1, y = ~Length.c, visible=T) %>%
add_bars(name = "Width.a",
x = ~Var1, y = ~Width.a, visible=F, data=data2, marker=list(color="#377EB8")) %>%
add_bars(name = "Width.b",
x = ~Var1, y = ~Width.b, visible=F, data=data2, marker=list(color="#FF7F00")) %>%
layout(
title = "Bar chart with drop down menu",
xaxis = list(title="x"),
yaxis = list(title = "y"),
showlegend = T,
updatemenus = list(
list(y = 0.9,
buttons = items)
))

R ggvis format tooltip using percentages

I'm using the ggvis package in R to build a build a horizontal stack bar. In the tooltip, I want to add both the absolute and relative values (in percentages).
The following code works, but the tooltip is not formatted correctly yet:
all_values <- function(x) {
if(is.null(x)) return(NULL)
#x[,sapply(x, is.double)] <- apply(x[,sapply(x, is.double)], 1, function(x) {paste(round(100*x, 2), "%", sep="")})
paste0(names(x), ": ",format(x), collapse = "<br />")
}
df <- data.frame(a = c('a','b','c'), v1 = c(7,2,1), v2 = c(0.7,0.2,0.1))
df %>% ggvis(x = ~v1, y = ~a, fill = ~v2) %>%
layer_rects(x2 = 0, height = band()) %>%
add_tooltip(all_values, "hover") %>%
add_tooltip(all_values, "click")
I want to format v2 in such a way that it shows percentages in the tooltip.
The values themself (0.7 for example) should still be used as fill.
Removing the commented part in all_values let's the visual crash when hovering for some reason, even though the result of the function would be perfect to me.
Any suggestions?
Something like this?
library(ggvis)
all_values <- function(x) {
if(is.null(x)) return(NULL)
x <- paste0(x[,3]*100,"%")
paste0(names(x), "",format(x), collapse = "<br />")
}
df <- data.frame(a = c('a','b','c'), v1 = c(7,2,1), v2 = c(0.7,0.2,0.1))
df %>% ggvis(x = ~v1, y = ~a, fill = ~v2) %>%
layer_rects(x2 = 0, height = band()) %>%
add_tooltip(all_values, "hover") %>%
add_tooltip(all_values, "click")

r ggvis axis format with percentage and decimal places

I want to ask, if someone knows answer regarding axis format in r package ggvis.
I am using % axis format, but I want to add som decimal places depending on spread of CVaR.
image here
Here is my code :
Datovka %>% ggvis(x = ~yShort,y = ~xShort,strokeWidth:=1, stroke:="blue", strokeWidth.hover := 3) %>% layer_paths() %>% layer_paths(x = ~yNoShort, y = ~xNoShort, data = DatovkaNoShort , stroke :="green",strokeWidth:=1, strokeWidth.hover := 3) %>%
layer_points(~vol,~r,data = vynosyR, fill := ~isinP, key := ~id , size := 60, size.hover :=120 ) %>%
# hide_legend("fill") %>%
add_tooltip(all_values, "hover") %>%
add_axis("x", title = "CVaR",format = "%" , title_offset = 50) %>%
add_axis("y", title = "Expected return", format = "%" , title_offset = 50) %>%
scale_numeric("y", nice = FALSE, clamp = TRUE) %>%
scale_numeric("x", nice = FALSE, clamp = TRUE) }
Thank you
You can read about formating here: https://github.com/d3/d3-3.x-api-reference/blob/master/Formatting.md
df <- data.frame(x = seq(0.1, 1, 0.1) / 100, y = runif(10))
df %>% ggvis(~x, ~y) %>% layer_paths() %>% add_axis("x", format = '.2%')

Add mouseover to outliers but not other points?

I'd like to plot a large scatterplot using the highcharter package, but only allow mouse over on a few outliers. Is there a way to enable mouseTracking on one series but not the other?
df <- data.frame( x = rnorm(1000), y = rnorm(1000) )
df$sig <- ifelse( abs(df$x) > 2, "signif", "not")
library(highcharter)
hc <- highchart() %>%
hc_add_series_df(df, type = "scatter", group=sig)
Right now I can only disable mouse over on all points, but the hc_plotOptions says something about using a series array?
hc_plotOptions(hc, scatter = list( enableMouseTracking= FALSE ))
There are a lot of way to do what you want.
I think the simplest is use:
hchart(df, "scatter", hcaes(x, y, group = sig), enableMouseTracking = c(FALSE, TRUE))
(Note this is the development version of highcharter.)
Which is same as:
highchart() %>%
hc_add_series(data = df %>% filter(sig == "not"), type = "scatter", enableMouseTracking = FALSE) %>%
hc_add_series(data = df %>% filter(sig == "signif"), type = "scatter", enableMouseTracking = TRUE)
Or
highchart() %>%
hc_add_series(data = list_parse(df %>% filter(sig == "not")), type = "scatter", enableMouseTracking = FALSE) %>%
hc_add_series(data = list_parse(df %>% filter(sig == "signif")), type = "scatter", enableMouseTracking = TRUE)

shiny+ggvis: how to add a line(median) to scatterplot?

I have an reactive ggvis scatterplot (layer_points) in shiny.
Now i want to add an horizontal line and vertical line in the plot to resemble the median of the x/y axis.
i know how to calculate it, but not how to display it in same plot.
my code so far:
vis <- reactive({
# Lables for axes
xvar_name <- names(axis_vars)[axis_vars == input$xvar]
yvar_name <- names(axis_vars)[axis_vars == input$yvar]
xvar <- prop("x", as.symbol(input$xvar))
yvar <- prop("y", as.symbol(input$yvar))
gegevens %>%
ggvis(x = xvar, y = yvar) %>%
layer_points(size := 50, size.hover := 200,
fillOpacity := 0.2, fillOpacity.hover := 0.5,
stroke = ~bron, key := ~Project.ID) %>%
add_tooltip(gegevens_tooltip, "hover") %>%
add_axis("x", title = xvar_name, format='d', grid = FALSE) %>%
add_axis("y", title = yvar_name, format='d', grid = FALSE) %>%
add_legend("stroke", title = "Gegevens van:", values = c("A", "B")) %>%
scale_numeric("x", trans = "log", expand=0) %>%
scale_numeric("y", trans = "log", expand=0) %>%
scale_nominal("stroke", domain = c("A", "B"),
range = c("blue", "#aaa")) %>%
set_options(width = 600, height = 600)
})
vis %>% bind_shiny("plot1")
to calculate the median i use:
output$defects <- renderText ({
d <- median(gegevens()$Total.Defects.Delivered)
paste("de mediaan voor totaal aantal Defects is:", d)
})
Lots of thanks for helping.
Seems i misunderstood your example, but i got it working, just after i posted i couldn't. Well here is the solution:
vis <- reactive({
# Lables for axes
xvar_name <- names(axis_vars)[axis_vars == input$xvar]
yvar_name <- names(axis_vars)[axis_vars == input$yvar]
xvar <- prop("x", as.symbol(input$xvar))
yvar <- prop("y", as.symbol(input$yvar))
gegevens %>%
ggvis(x = xvar, y = yvar) %>%
layer_points(size := 50, size.hover := 200,
fillOpacity := 0.2, fillOpacity.hover := 0.5,
stroke = ~bron, key := ~Project.ID) %>%
add_tooltip(gegevens_tooltip, "hover") %>%
add_axis("x", title = xvar_name, format='d', grid = FALSE, properties = axis_props(labels = list(angle = 90, align = "left"))) %>%
add_axis("y", title = yvar_name, format='d', grid = FALSE) %>%
add_legend("stroke", title = "Gegevens van:", values = c("A", "B")) %>%
scale_numeric("x", trans = "log", expand=0) %>%
scale_numeric("y", trans = "log", expand=0) %>%
scale_nominal("stroke", domain = c("A", "B"),
range = c("blue", "#aaa")) %>%
set_options(width = 600, height = 600) %>%
layer_paths(data = gegevens, x = median(gegevens()$kolomname.i.want.the.median.from)), y = yvar ) %>%
layer_paths(data = gegevens, x = xvar, y = median(gegevens()$kolomname.i.want.the.median.from))
})
this gives me an cross in my plot by calculating the median of x and y, even if the user changes the original input. of course i need to find out how to get "kolomname.i.want.the.median.from" to be the x-/ or y-value.
but i now know how to get the lines in, and that was the question.
So thank you aosmith for the right direction.

Resources