如何在plotly R中修改标签大小?

5

我需要修改Plotly图表中标签和标题的大小。

我找到了一种只能修改坐标轴标签和标题大小的方法。

此外,当我已经指定了x轴和y轴标题时,为什么只显示了x轴标题而没有显示y轴标题,我也不太明白。

这是我的代码:

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)))

我已经尝试过这种方法,但它没有起作用:
  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)))

这是我的图表:

enter image description here

这是我的数据:

> 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"

我该如何解决这个问题?

我不知道为什么我无法看到我添加的图形的图像... - ArTu
图片链接在代码块中(需要在代码两端加上```)- 我已经修复了,但需要批准。 - BHudson
1个回答

7
原始代码存在一些问题。其中一个括号放错位置导致y轴标签无法显示。
为了调整标题大小,我将该规范分解成自己的布局函数。标题大小被调整得更大,还需要调整绘图的边距。
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)) )

我觉得这是使用Plotly和R的非常好的参考文献:https://plot.ly/r/plotly-fundamentals/.

enter image description here


谢谢!我正在尝试在其他情况下使用这段代码...但我真的不明白哪部分是要修改图例大小的代码部分...你能帮我吗?谢谢! - ArTu
1
@ArTu,要编辑图例文本属性,请将此行添加到函数layout(legend = list(font = list(size = 18,color =“#000”),bgcolor =“#E2E2E2”))中。请参见答案中的链接以获取详细说明。 - Dave2e

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接