ArTu
ArTu

Reputation: 483

How can I modify label size in plotly R?

I have to modify the size of the label and of the title of a plotly graph.

I've found a way to modify only axis label and axis title size.

Furthermore I don't understand why the x axis title is shown and the y axis title not when I've specified both.

Here's my code:

cc <- count(mydata, year_dx1, Age)
cc2 <- left_join(cc, count(cc, year_dx1, wt = n, name = 'nn'))
cc2 %>%
mutate(Percentage = (n / nn)*100) %>%
plot_ly(x = ~year_dx1, y = ~Percentage, color = ~Age) %>%
add_bars() %>%
layout(barmode = "stack")%>%
layout(title = 'Age at diagnosis distribution by time', xaxis 
= list(title = 'Year of diagnosis',                                                  
 yaxis = list(title = 'Count')))%>%
 layout(
  xaxis = list(titlefont = list(size = 22), tickfont = 
list(size = 22)),
  yaxis = list(titlefont = list(size = 22), tickfont = 
 list(size = 22)))

I've tried in this way but it doesn't work:

  cc <- count(mydata, year_dx1, Age)
 cc2 <- left_join(cc, count(cc, year_dx1, wt = n, name ='nn'))
 cc2 %>% mutate(Percentage = (n / nn)*100) %>%
 plot_ly(x = ~year_dx1, y = ~Percentage, color = ~Age) %>%
 add_bars() %>% layout(barmode = "stack")%>% layout(title ='Age at diagnosis distribution by time', xaxis = list(title = 'Year of diagnosis',yaxis = list(title = 'Count')))%>%
  layout(xaxis = list(titlefont = list(size = 22), tickfont = 
 list(size = 22)),yaxis = list(titlefont = list(size = 22), tickfont =list(size = 22)),legend = list(tickfont = list(size = 22)))

Here's my graph:

enter image description here

Here are my data:

> mydata$Age
  [2] ">64,<=72" ">64,<=72" "<=53"     "<=53"     "<=53"     ">53,<=64" "<=53"    
  [8] ">72"      ">53,<=64" ">64,<=72" ">64,<=72" ">53,<=64" ">72"      ">64,<=72"
 [15] ">53,<=64" ">72"      "<=53"     "<=53"     "<=53"     ">53,<=64" ">72"     
 [22] ">72"      ">53,<=64" ">53,<=64" ">64,<=72" ">72"      "<=53"     ">72"     
 [29] ">72"      ">53,<=64" ">53,<=64" ">72"      ">53,<=64" "<=53"     ">53,<=64"
 [36] ">64,<=72" ">72"      ">64,<=72" ">64,<=72" ">72"      ">64,<=72" ">64,<=72"
 [43] ">53,<=64" ">72"      ">53,<=64" ">72"      ">64,<=72" ">64,<=72" ">72"     
 [50] "<=53"     ">53,<=64" ">53,<=64" ">72"      ">64,<=72" ">72"      ">72"     
 [57] ">72"      ">72"      ">72"      ">64,<=72" "<=53"     ">53,<=64" ">53,<=64"
 [64] ">64,<=72" ">72"      ">72"      ">53,<=64" ">72"      ">72"      ">72"     
 [71] "<=53"     "<=53"     "<=53"     ">64,<=72" "<=53"     ">53,<=64" ">53,<=64"
 [78] ">64,<=72" ">72"      ">53,<=64" "<=53"     "<=53"     "<=53"     ">72"     
 [85] ">72"      ">64,<=72" "<=53"     ">53,<=64" ">64,<=72" ">53,<=64" "<=53"    
 [92] "<=53"     ">72"      ">72"      ">53,<=64" ">53,<=64" "<=53"     ">72"     
 [99] ">53,<=64" ">64,<=72" ">72"      ">53,<=64" ">72"      ">64,<=72" ">64,<=72"

> mydata$year_dx1
  [2] "2015-2019" "2010-2014" "2005-2009" "2010-2014" "2005-2009" "2010-2014"
  [7] "2000-2004" "2010-2014" "2000-2004" "2005-2009" "1995-1999" "2010-2014"
 [13] "2005-2009" "2005-2009" "2005-2009" "2015-2019" "2015-2019" "2015-2019"
 [19] "2015-2019" "2010-2014" "2010-2014" "2010-2014" "2015-2019" "2010-2014"
 [25] "2010-2014" "2005-2009" "2015-2019" "2005-2009" "2015-2019" "2005-2009"
 [31] "2010-2014" "2015-2019" "2005-2009" "2010-2014" "2005-2009" "2010-2014"
 [37] "2015-2019" "2005-2009" "2010-2014" "2005-2009" "2005-2009" "2010-2014"
 [43] "2015-2019" "2010-2014" "2010-2014" "2015-2019" "2005-2009" "2015-2019"
 [49] "2010-2014" "2015-2019" "2010-2014" "2015-2019" "2015-2019" "2015-2019"
 [55] "2015-2019" "2015-2019" "2015-2019" "2015-2019" "2010-2014" "2015-2019"
 [61] "1995-1999" "2015-2019" "2010-2014" "2015-2019" "2005-2009" "2005-2009"
 [67] "2015-2019" "2010-2014" "2015-2019" "2015-2019" "2015-2019" "2015-2019"
 [73] "1995-1999" "2005-2009" "2015-2019" "2010-2014" "2000-2004" "2005-2009"
 [79] "2010-2014" "2005-2009" "2010-2014" "1995-1999" "2010-2014" "2015-2019"
 [85] "2015-2019" "2015-2019" "2010-2014" "2005-2009" "2015-2019" "2010-2014"
 [91] "2005-2009" "2010-2014" "2000-2004" "2015-2019" "2005-2009" "2000-2004"
 [97] "1995-1999" "2015-2019" "2015-2019" "2000-2004" "2010-2014" "2010-2014"
[103] "2010-2014" "2010-2014" "2010-2014" "2000-2004" "2010-2014" "2015-2019"

How can I fix this problem?

Upvotes: 6

Views: 7142

Answers (1)

Dave2e
Dave2e

Reputation: 24139

There was a couple of issues with the original code. There was a misplaced ")" which prevented the y-axis label from displaying.

In order to adjust the title size, I broke that specification into its own layout function. Sized the title is larger and I also needed to adjust the plot's margins.

cc2 %>%
  mutate(Percentage = (n / nn)*100) %>%
  plot_ly(x = ~year_dx1, y = ~Percentage, color = ~Age) %>%
  add_bars() %>%
  layout(barmode = "stack")%>%
  layout(autosize = T, margin=list( l = 50, r = 50, b = 100, t = 100,  pad = 4)) %>%
  layout(title = 'Age at diagnosis distribution by time', font=list(size = 20)) %>%
  layout(xaxis = list(title = 'Year of diagnosis'), yaxis = list(title = 'Count')) %>%
  layout( xaxis = list(titlefont = list(size = 22), tickfont = list(size = 22)),
          yaxis = list(titlefont = list(size = 22), tickfont = list(size = 22)) )

I find this a very good reference for using Plotly with R: https://plot.ly/r/plotly-fundamentals/.

enter image description here

Upvotes: 9

Related Questions