Multiple line graphs in ggvis using for loop - r

I am trying to create multiple line graphs using ggvis. I am able to plot multiple lines but I am unable to add tooltip for these lines. I need to show the x and y value when I hover the mouse on the lines. I also am unable to add points to the lines in the for loop.
Below is a simplified example I am working with. column "c1" is the x values and columns "c2", "c3" and "c4" are to be plotted(lines with points and tooltip)
Screenshot of the plot
mydf <- data.frame(c(1:10),c(11:20), c(21:30), c(31:40))
v <- c("c1","c2","c3", "c4")
names(mydf) <- v
myggv <- mydf %>% ggvis(x = ~c1, y = ~c2) %>% layer_lines() %>% layer_points() %>% add_tooltip( function(mydf){paste0("x:",mydf$c1,"<br>","y:",mydf$c2)}, "hover")
for(r in v[2:length(v)]){
myggv <- (myggv %>% layer_paths(x = ~c1, y = as.name(r)) %>% layer_points()
%>% add_tooltip( function(mydf){paste0("x:",mydf$c1,"<br>","y:",mydf[,r] )}, "hover"))
}
Thanks

The best approach here is to not use a for loop. I mean, you can, but it's not the way ggvis approaches things. Also I can't get the tooltip to work in the loop (it gives the only the correct result for the last added line. But here is how I would do it anway:
mydf <- data.frame(c1 = c(1:10),
c2 = c(11:20),
c3 = c(21:30),
c4 = c(31:40))
myggv <- ggvis(mydf)
for (r in names(mydf)[-1]) {
myggv <- (myggv %>%
layer_paths(x = ~c1, y = as.name(r)) %>%
layer_points(x = ~c1, y = as.name(r)) %>%
add_tooltip(function(mydf) {
paste0("x:", mydf[[1]], "<br>", "y:", mydf[[r]])}, "hover"))
}
The nicer way is to restructure your data, and then use group_by to create seperate lines. As an added benefit, this is perhaps nicer to read. This way your tooltips also work:
mydf2 <- tidyr::gather(mydf, 'var', 'val', -c1)
myggv2 <- mydf2 %>%
ggvis(x = ~c1, y = ~val) %>%
layer_points() %>%
add_tooltip(function(d) { paste0("x:", d$c1, "<br>", "y:", d$val) }, "hover") %>%
group_by(var) %>%
layer_paths()
You might want to use layer_lines() instead of layer_paths().

Related

ggplot plots within a table

Problem
I would like to produce a good looking table which has ggplots within the cells of one column. One key element is that I would like to create a pdf output of this table eventually.
What I have tried so far
Hopefully the example below is understandable. Essentially I found that I can achieve what I want using the gt package. The problem is this creates a html widget which you then have to use phantomJS and webshot to export as a pdf.
library(dplyr)
library(purrr)
library(gt)
library(ggplot2)
dat = tibble(
RowLabel = letters[1:5],
Numeric = seq(100,500,100)
) %>%
mutate(
plotData = RowLabel %>% map(function(pos){
tibble(y=runif(10)*100) %>%
arrange(desc(y)) %>%
mutate(x=row_number())
}),
plot_obj = plotData %>% map(function(df){
df %>%
ggplot(aes(x=x,y=y))+
geom_col()
}),
plot_grob = plot_obj %>% map(cowplot::as_grob)
)
tab = dat %>%
select(RowLabel, Numeric) %>%
mutate(
ggplot = NA
) %>%
gt() %>%
text_transform(
locations = cells_body(vars(ggplot)),
fn = function(x) {
dat$plot_obj %>%
map(ggplot_image, height = px(50))
}
)
tab
What do I want
I would like an output which is similar to the above example. However, I would like a solution which does not require me to use html widgets and can be saved directly as a pdf without the use of other programs. Is this possible to do using ggplot? I have started to learn more about grids/grobs/gtables etc but have not made any meaningful progress.
Thanks in advance!
Perhaps you could tweak the gtsave() function to suit? E.g.
library(dplyr)
library(purrr)
library(gt)
library(ggplot2)
dat = tibble(
RowLabel = letters[1:5],
Numeric = seq(100,500,100)
) %>%
mutate(
plotData = RowLabel %>% map(function(pos){
tibble(y=runif(10)*100) %>%
arrange(desc(y)) %>%
mutate(x=row_number())
}),
plot_obj = plotData %>% map(function(df){
df %>%
ggplot(aes(x=x,y=y))+
geom_col()
}),
plot_grob = plot_obj %>% map(cowplot::as_grob)
)
tab = dat %>%
select(RowLabel, Numeric) %>%
mutate(
ggplot = NA
) %>%
gt() %>%
text_transform(
locations = cells_body(vars(ggplot)),
fn = function(x) {
dat$plot_obj %>%
map(ggplot_image, height = px(50))
}
)
tab %>%
gt::gtsave(filename = "test.pdf", vwidth = 180, vheight = 250)
(R v4.0.3 / gt v0.2.2)

ggvis - using a variable to assign "x" property using props

According to the properties and scales reference and this SO post I should be able to use a props(prop()) call as shown below to create the graph. I'm getting an unintelligible error though.
test_df <- data.frame(cbind(x_vals = letters[1:5],
y_vals = 1:5))
Graphs correctly:
test_df %>% ggvis(x = ~x_vals, y = ~y_vals) %>%
layer_points()
Has error:
x_val_variable <- "x_vals"
test_df %>% ggvis(y = ~y_vals) %>%
props(prop("x", as.name(x_val_variable)) %>%
layer_points()
Can anyone help me by telling me what I'm doing wrong?
I figured out the correct usage of prop(), it doesn't belong in the %>% chain, it's an argument of ggvis or layer_points(). I still don't understand when you would use props() though.
test_df <- data.frame(cbind(x_vals = letters[1:5],
y_vals = 1:5))
x_val_variable <- "x_vals"
#you can use either one of these, they are identical
p1 <- prop("x", as.name(x_val_variable))
p2 <- prop("x", parse(text = x_val_variable)[[1]])
test_df %>% ggvis(p1, y = ~y_vals) %>% layer_points()

Even Axis in R Charts

I've searched a bit, but I'm not able to find a way to acheive my axis goals. There are really 2 questions here.
How can I guarantee the spacing between my major ticks is the same? If it means some dots overlap, then so be it.
Is there a way to add a zoom/date range control to this chart? Data goes from 2013 to current and is continually added.
2.b. If I do this, is there a way to have it as you zoom out it automatically start bucketing by week, then month, then year? and of course the inverse.
Here is where you can get the data: https://opendata.miamidade.gov/311/311-Service-Requests-Miami-Dade-County/dj6j-qg5t
Here is the image of the current chart:
Note that I am doing this to learn R, and hence any erraneous suggestions are also appreciated. Here is the code that generates this:
#Graphics Visualizations Package
library("ggvis")
#Adds %>% forward pipe operator
library("magrittr")
#adds grouping and manipulations
library("dplyr")
#adds data fiendlyness stuffs
library("tidyr")
library("shiny")
library("checkpoint")
checkpoint("2016-03-29")
rData <- read.csv("C:\\data\\Miami_311.csv",
header=TRUE,
sep=",")
rDSamp <- rData[sample(1:length(rData$Case.Owner), 1000),]
#Convert to known date time
rDSamp$Ticket.Created.Date...Time <-
rDSamp$Ticket.Created.Date...Time %>%
as.POSIXct(format="%m/%d/%Y") %>%
as.character()
FilterDateRange = function(data, feature, minDate, maxDate) {
minDate = minDate %>%
as.POSIXct(format="%m/%d/%Y") %>%
as.character()
maxDate = maxDate %>%
as.POSIXct(format="%m/%d/%Y") %>%
as.character()
result = subset(data, data[feature] <= maxDate)
subset(result, result[feature] >= minDate)
}
d <- rDSamp %>%
FilterDateRange("Ticket.Created.Date...Time", "1/1/2013", "12/31/2013") %>%
group_by(Ticket.Created.Date...Time, Case.Owner) %>%
summarise(
count = n()
) %>%
arrange(Ticket.Created.Date...Time)
xAxisValues = "1/1/2013" %>%
as.Date(format="%m/%d/%Y") %>%
as.character() %>%
as.Date() %>%
seq(by = "1 months", length.out = 12)
d %>%
ggvis(~Ticket.Created.Date...Time, ~count) %>%
layer_points(fill = ~Case.Owner) %>%
add_tooltip(function(data){
paste("Owner:", data$Case.Owner, "<br>","Date:", data$Ticket.Created.Date...Time)
}, "hover") %>%
add_axis("x",
title = "Date",
values = xAxisValues,
ticks = 365,
properties = axis_props(
majorTicks = list(strokeWidth = 2)))

Disappearing values in ggvis when using tooltip on grouped data

This works fine
library(dplyr)
library(ggvis)
years <- as.factor(c(2013,2013,2014,2014,2015,2015))
months <- c(1,2,1,2,1,2)
values <- c(3,2,4,6,5,1)
df <- data.frame(years,months,values)
df %>%
group_by(years) %>%
ggvis(~months, ~values) %>%
layer_points( fill = ~years)
However, when I add a tooltip the points all appear momentarily but only the 2015 values remain
df <- cbind(df, id = seq_len(nrow(df)))
all_values <- function(x) {
if(is.null(x)) return(NULL)
row <- df[df$id == x$id,]
paste0(names(row),": ",format(row), collapse = "<br />")
}
df %>%
group_by(years) %>%
ggvis(~months, ~values, key:= ~id) %>%
layer_points( fill = ~years) %>%
add_tooltip(all_values, "hover")
Probably some simple error, but any help appreciated
cheers
At the moment, trying to add a tooltip specific to a row when the data are grouped doesn't seem to work. This makes some sense, actually, as the grouping implies that you may want info by group.
You don't need grouping in your layer_points example at all, but you would need it if you wanted lines between the points for specific years as well as points. If this is what you wanted, you'd group the dataset after you add the points, and put key in layer_points instead of in the overall ggvis.
df %>%
ggvis(~months, ~values) %>%
layer_points( fill = ~years, key:= ~id) %>%
add_tooltip(all_values, "hover") %>%
group_by(years) %>%
layer_lines(stroke = ~years, strokeWidth := 2)
This isn't totally ideal, because the tooltip still shows up for the lines even though lines don't have unique id data associated with them. To change this, make a small change to your tooltip function to check just if the id variable is NULL rather than the whole dataset from ggvis.
all_values = function(x) {
if(is.null(x$id)) return(NULL)
row <- df[df$id == x$id,]
paste0(names(row),": ",format(row), collapse = "<br />")
}
You can add a tooltip at the group level, but you'll need to figure out what sort of summary info you want to display. In the function for the tooltip you will be working with the grouping variable instead of id.
For example, you could just show all values associated with the group. The way I've set this up you'll need to click on the line to see the group info:
group_values1 = function(x) {
if(is.null(x)) return(NULL)
group = df[df$years == unique(x$years),]
paste0(names(group), ": ", format(group), collapse = "<br />")
}
df %>%
ggvis(~months, ~values) %>%
layer_points( fill = ~years, key:= ~id) %>%
add_tooltip(all_values, "hover") %>%
group_by(years) %>%
layer_lines(stroke = ~years, strokeWidth := 2) %>%
add_tooltip(group_values1, "click")
You may want to show summary information, instead. In this example, I'll display the overall change in values and how many months passed in the tooltip. I make the summary dataset with functions from dplyr.
group_values2 = function(x) {
if(is.null(x)) return(NULL)
group = df[df$years == unique(x$years),]
groupval = group %>% group_by(years) %>%
summarise(`Change in value` = max(values) - min(values),
`Months Passed` = max(months) - min(months))
paste0(names(groupval), ": ", format(groupval), collapse = "<br />")
}
df %>%
ggvis(~months, ~values) %>%
layer_points( fill = ~years, key:= ~id) %>%
add_tooltip(all_values, "hover") %>%
group_by(years) %>%
layer_lines(stroke = ~years, strokeWidth := 2) %>%
add_tooltip(group_values2, "click")
The problem lies in you putting group_by before ggvis and calling add_tooltip afterwards. Just put group_by part after ggvis call
df %>%
ggvis(~months, ~values, key:= ~id) %>%
group_by(years) %>%
layer_points( fill = ~years) %>%
add_tooltip(all_values, "hover")
no idea why it happens though

Multiple plots from multiple data frames using ggvis in shiny in R

How do I plot multiple series from different data frames, with different numbers of rows on one ggvis plot?
shinyServer(function(input, output, session) {
vis1 <- reactive({
df2 <-data.frame(csv1)
df3 <- data.frame(csv2)
long2 <- array()
matrix2<-data.frame(Col1,Col2,Col3,Col4)
matrix2$long2 = as.character(paste0("Col3: ",Col3,", Col4: ",Col4))
matrix2 # What does this line do?
})
vis1 %>%
ggvis(x = ~Col1, y = ~Col2,fill = ~Col4, key:= ~long2) %>%
layer_points() %>%
add_axis("x", title = "Col1") %>%
scale_numeric("x", domain = c(0, 130), nice = FALSE, clamp = TRUE)%>%
add_axis("y", title = "Col2") %>%
scale_numeric("y", domain = c(0, 8000), nice = FALSE, clamp = TRUE)%>%
add_tooltip(function(matrix2){
paste0("Col2: ", matrix2$Col2, "<br>", "Col1: ", matrix2$Col1, "<br>", " ", as.character(matrix2$long2))
}, "hover") %>%
layer_paths(data = df2, x = ~Col1,y = ~Col2,fill:="green") %>%
bind_shiny("plot_col2")#,"plot_ui_col2")
output$matrix2_table <- renderTable({
vis1()[,c("Col1","Col2","Col3","Col4")]
})
This is most of the server.R code, with ui.R code that works fine with one series plotted on these axes.
But trying to put the data from two data frames on these axes just returns
"Error in add_data(vis, data, data_name) : object 'df2' not found"
Doesn't even get to df3.
How do I get it to recognise df2, then use that data to plot the second series?
I get that plotting two series from the same data frame is done like this:
df %>% ggvis(~x,~y) layer_points() %>%
df %>% layer_points(~x,~y1)
There abouts, but how is it done with different numbers of points and from different data frames?
Thank you.
I put everything in the same data frame, in the same columns, just added more rows, also added a colours column, so they would be plotted in different colours.
The first thing to do was to make a much larger data frame in the unnamed function section at the top (only executes once).

Resources