shosaco / vistime

Pretty timelines in R.
https://shosaco.github.io/vistime
GNU General Public License v3.0
168 stars 11 forks source link

Changing text size #4

Closed okeyes-r7 closed 5 years ago

okeyes-r7 commented 6 years ago

How does one modify the actual size of the text or similar attributes?

thiagoveloso commented 6 years ago

I am also interested on this feature - particularly how to increase the font size on the axis.

dellis83 commented 6 years ago

Related to this - being able to wrap the text would be useful as well.

shosaco commented 6 years ago

@dellis83 : Use can use the HTML <br> tag for this. Minimal example:

pres <- data.frame(Position = rep(c("President<br>big time", "Vice<br>so it is"), each = 3),
                   Name = c("Washington", rep(c("Adams", "Jefferson"), 2), "Burr"),
                   start = c("1789-03-29", "1797-02-03", "1801-02-03"),
                   end = c("1797-02-03", "1801-02-03", "1809-02-03"),
                   color = c('#cbb69d', '#603913', '#c69c6e'),
                   fontcolor = c("black", "white", "black"))

vistime(pres, events="Position", groups="Name", title="Presidents of the USA")

image

shosaco commented 6 years ago

@okeyes-r7 This can be achieved using the function plotly_build, which returns your plot as a list. You can manipulate all of the elements there. Explore the resulting object using str and then lapply over the elements. An Example where I manipulate the text size and change it to 28:

pres <- data.frame(Position = rep(c("President", "Vice"), each = 3),
                    Name = c("Washington", rep(c("Adams", "Jefferson"), 2), "Burr"),
                    start = c("1789-03-29", "1797-02-03", "1801-02-03"),
                    end = c("1797-02-03", "1801-02-03", "1809-02-03"),
                    color = c('#cbb69d', '#603913', '#c69c6e'),
                    fontcolor = c("black", "white", "black"))

p <- vistime(pres, events="Position", groups="Name", title="Presidents of the USA")
pp <- plotly_build(p)
pp$x$data <- lapply(pp$x$data, function(x){
 if(x$mode == "text"){
       x$textfont$size <- 28
       return(x)
  }else{
       return(x)
  }})
pp

image

shosaco commented 6 years ago

@thiagoveloso This is achievable by the same trick. I used the plotly help pages as an input to create a simple plot with custom font size, then plotly_build it and used str to find the place where the tick font size is stored. Solution:

pres <- data.frame(Position = rep(c("President", "Vice"), each = 3),
                    Name = c("Washington", rep(c("Adams", "Jefferson"), 2), "Burr"),
                    start = c("1789-03-29", "1797-02-03", "1801-02-03"),
                    end = c("1797-02-03", "1801-02-03", "1809-02-03"),
                    color = c('#cbb69d', '#603913', '#c69c6e'),
                    fontcolor = c("black", "white", "black"))

p <- vistime(pres, events="Position", groups="Name", title="Presidents of the USA")
pp <- plotly_build(p)
pp$x$layout$xaxis$tickfont <- list(size = 28)

image

thiagoveloso commented 5 years ago

@shosaco thank you so much for the hint, your example works for me!

However, I am also trying to change the font size for the y axis, and adding the following code did not produce the expected outcome:

pp$y$layout$yaxis$tickfont <- list(size = 28)

Any ideas?

shosaco commented 5 years ago

However, I am also trying to change the font size for the y axis, and adding the following code did not produce the expected outcome:

pp$y$layout$yaxis$tickfont <- list(size = 28)

Inspecting the layout element, we see that there are multiple yaxes (one for each subplot):

[1] "xaxis"      "yaxis4"     "yaxis3"     "yaxis2"     "yaxis"      "margin"     "hovermode" 
[8] "showlegend" "title"   

So we need to change the font size of all of them:

pres <- data.frame(Position = rep(c("President", "Vice"), each = 3),
                    Name = c("Washington", rep(c("Adams", "Jefferson"), 2), "Burr"),
                    start = c("1789-03-29", "1797-02-03", "1801-02-03"),
                    end = c("1797-02-03", "1801-02-03", "1809-02-03"),
                    color = c('#cbb69d', '#603913', '#c69c6e'),
                    fontcolor = c("black", "white", "black"))

p <- vistime(pres, events="Position", groups="Name", title="Presidents of the USA")
pp <- plotly_build(p)

# loop through the yaxes and change the font size for each element:
for(i in grep(pattern = "yaxis*", names(pp$x$layout))){
     yax <- pp$x$layout[[i]]
     yax$tickfont <- list(size = 28)
     pp$x$layout[[i]] <- yax
}

pp

2018-11-17 11_57_48- _privat_vistime - master - rstudio

Hope this helps :) sandro

thiagoveloso commented 5 years ago

Inspecting the layout element, we see that there are multiple yaxes (one for each subplot):

[1] "xaxis"      "yaxis4"     "yaxis3"     "yaxis2"     "yaxis"      "margin"     "hovermode" 
[8] "showlegend" "title"   

So we need to change the font size of all of them:

pres <- data.frame(Position = rep(c("President", "Vice"), each = 3),
                    Name = c("Washington", rep(c("Adams", "Jefferson"), 2), "Burr"),
                    start = c("1789-03-29", "1797-02-03", "1801-02-03"),
                    end = c("1797-02-03", "1801-02-03", "1809-02-03"),
                    color = c('#cbb69d', '#603913', '#c69c6e'),
                    fontcolor = c("black", "white", "black"))

p <- vistime(pres, events="Position", groups="Name", title="Presidents of the USA")
pp <- plotly_build(p)

# loop through the yaxes and change the font size for each element:
for(i in grep(pattern = "yaxis*", names(pp$x$layout))){
     yax <- pp$x$layout[[i]]
     yax$tickfont <- list(size = 28)
     pp$x$layout[[i]] <- yax
}

pp

Hope this helps :) sandro

Awesome, it definitely works! Many thanks!

Maybe these tricks can be added to the documentation and this issue can be closed?

shosaco commented 5 years ago

Added it to the readme.rd and the official documentation (?vistime), in version 0.6.9000.