There are examples available of changing marker size in plotly objects created with vistime() (e.g. as below), but I am having trouble figuring out how to do the same thing for a ggplot2 object made with gg_vistime(). Ideally I would like to be able to specify both the marker size and the label font size. Is there a way to do this?
library(vistime)
dat <- data.frame(event = 1:4, start = c("2019-01-01", "2019-01-10"))
p <- vistime(dat)
# step 1: transform into a list
pp <- plotly::plotly_build(p)
# step 2: loop over pp$x$data, and change the marker size of all text elements to 50px
for(i in seq_along(pp$x$data)){
if(pp$x$data[[i]]$mode == "markers") pp$x$data[[i]]$marker$size <- 20
}
# or, using purrr:
# marker_idx <- which(purrr::map_chr(pp$x$data, "mode") == "markers")
# for(i in marker_idx) pp$x$data[[i]]$marker$size <- 20
# pp
pp
CodePudding user response:
You can use the following code to specify the textfont size too, like this:
library(vistime)
#> Warning: package 'vistime' was built under R version 4.1.2
dat <- data.frame(event = 1:4, start = c("2019-01-01", "2019-01-10"))
p <- vistime(dat)
# step 1: transform into a list
pp <- plotly::plotly_build(p)
# step 2: loop over pp$x$data, and change the marker size of all text elements to 50px
pp$x$data <- lapply(pp$x$data, function(x){
if(x$mode == "text"){
x$textfont$size <- 28
return(x)
}else if(x$mode == "markers"){
x$size <- 20
return(x)
}else{
return(x)
}})
pp
#> Warning: 'scatter' objects don't have these attributes: 'size'
#> Valid attributes include:
#> 'cliponaxis', 'connectgaps', 'customdata', 'customdatasrc', 'dx', 'dy', 'error_x', 'error_y', 'fill', 'fillcolor', 'groupnorm', 'hoverinfo', 'hoverinfosrc', 'hoverlabel', 'hoveron', 'hovertemplate', 'hovertemplatesrc', 'hovertext', 'hovertextsrc', 'ids', 'idssrc', 'legendgroup', 'legendgrouptitle', 'legendrank', 'line', 'marker', 'meta', 'metasrc', 'mode', 'name', 'opacity', 'orientation', 'selected', 'selectedpoints', 'showlegend', 'stackgaps', 'stackgroup', 'stream', 'text', 'textfont', 'textposition', 'textpositionsrc', 'textsrc', 'texttemplate', 'texttemplatesrc', 'transforms', 'type', 'uid', 'uirevision', 'unselected', 'visible', 'x', 'x0', 'xaxis', 'xcalendar', 'xhoverformat', 'xperiod', 'xperiod0', 'xperiodalignment', 'xsrc', 'y', 'y0', 'yaxis', 'ycalendar', 'yhoverformat', 'yperiod', 'yperiod0', 'yperiodalignment', 'ysrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'
Created on 2022-07-27 by the reprex package (v2.0.1)
CodePudding user response:
I figured out separate solutions for marker size and label size since gg_vistime() utilizes geom_text_repel() for the labels which doesn't store a size parameter in aes.
library(vistime)
dat <- data.frame(event = 1:4, start = c("2019-01-01", "2019-01-10"),
show_labels = FALSE)
# do not include labels in the original plot, add them later to control appearance
p <- gg_vistime(dat,
show_labels = FALSE)
# adjust the marker size by directly accessing the aes_params
p$layers[[3]]$aes_params$size <- 20
# add a new geom_text_repel layer for the labels where you can specify appearance
p
ggrepel::geom_text_repel(data=p$layers[[3]]$data,
label=p$layers[[3]]$data$label,
size =10,
color="black")