2

I want to change the font on the inside of a vistime object, but it happens to only change the axis font.

library(vistime)

dat <- data.frame(event = 1:4, start = c("2019-01-01", "2019-01-10"))

p <- gg_vistime(dat) +   
  theme(text = element_text(family = "serif"))
p

vistime with wrong font

Karl A
  • 165
  • 11

2 Answers2

2

there is a hard way to do it illustrated in the official documentation here

dat <- data.frame(event = c("Ab","cD","Ef","gH"), start = c("2019-01-01", "2019-01-10"))
p=vistime(dat)    
pp <- plotly::plotly_build(p)

for(i in seq_along(pp$x$data)){

  if(pp$x$data[[i]]$mode == "text") {
    pp$x$data[[i]]$textfont$family="Comic Sans MS"
    pp$x$data[[i]]$textfont$size=12
  }
}
pp

plot

Wael
  • 1,640
  • 1
  • 9
  • 20
  • Thanks a lot. Two little issues: 1) I can't find it in the documentation (I only see an option to change the color). 2) This only works for plotly? how can I do it in the ggplot case? – Karl A Mar 15 '23 at 02:21
  • 1
    Hi Karl, 1) in the documentation : section 9, Changing events font size the toy example was about changing the font size and the ``pp`` R object contains a family instance. – Wael Mar 15 '23 at 08:25
0

I found a solution for ggplot.

There seems to be no way but to access the aes_params. Depending on the number of layers/data in the plot, the part that needs to be changed is different (a good way to find the correct one is to change the size of all layers (start with one 1 as in the example and go up to N) and look at the plot every time p$layers[[1]]$aes_params$size <- 20). In the example, the text is in layer 5. The family aes is not present but can easily be added.

# Load the vistime library
library(vistime)

# Create a data frame with event numbers and start dates
dat <- data.frame(event = 1:4, start = c("2019-01-01", "2019-01-10"))

# Create a vistime plot using gg_vistime() and assign it to p
p <- gg_vistime(dat) +   
  theme(text = element_text(family = "serif")) # Change the font family for all other text elements (e.g., axes, title)

# Increase the size and change the font family for event labels
p$layers[[5]]$aes_params$size <- 20
p$layers[[5]]$aes_params$family <- "serif" 

# Print the vistime plot object to the console
p

Inspired by this post: Is there a way to change marker size and label font size in an R gg_vistime timeline?

Karl A
  • 165
  • 11