【问题标题】:nvd3 scatterPlot with rCharts in R: Increase Font size of labels?R中带有rCharts的nvd3 scatterPlot:增加标签的字体大小?
【发布时间】:2014-06-20 19:26:08
【问题描述】:

我正在尝试增加使用创建的绘图中 x 和 y 轴的字体大小 NVD3 和 rCharts。这是我的情节代码。任何帮助表示赞赏。

n1 <- nPlot(pValues~Chr,data=dat,type="scatterChart",height=400,width=750)
n1$chart(tooltipContent= "#! function(key, x, y, e){
         return '<b>ID:</b> ' + e.point.ID
         } !#")
n1$chart(forceY = c(0,8))
n1$chart(forceX = c(0,10))
#n1$chart(color = '#! function(d){return d.pValues} !#')
n1$xAxis(axisLabel = 'Chromosome')
n1$yAxis(axisLabel = '-log P value')

【问题讨论】:

  • 现在最简单的方法是在 stackoverflow.com/questions/17883017/… 中添加 css,但我知道这可能不是一个可行的解决方案。我将尝试以另一种方式工作,并提供直接来自 R 的答案。

标签: r rcharts nvd3.js


【解决方案1】:

实际上,感谢this stack overflow discussion,我想我找到了一个解决方案。请让我知道这对你有没有用。将font-size 更改为您想要的任何内容。您还可以提供一整套 CSS 来更改样式、位置、颜色等。

dat <- data.frame(
  pValues = runif(20,0,5),
  Chr = 1:20,
  ID = sample(LETTERS[1:20])
)

n1 <- nPlot(pValues~Chr,data=dat,type="scatterChart",height=400,width=750)
n1$chart(tooltipContent= "#! function(key, x, y, e){
     return '<b>ID:</b> ' + e.point.ID
     } !#")
n1$chart(forceY = c(0,8))
n1$chart(forceX = c(0,10))
#n1$chart(color = '#! function(d){return d.pValues} !#')
n1$xAxis(axisLabel = 'Chromosome')
n1$yAxis(axisLabel = '-log P value')
n1

n1$setTemplate(afterScript = '<script>
  var css = document.createElement("style");
  css.type = "text/css";
  css.innerHTML = ".nv-axislabel { font-size: 15px; }";
  document.body.appendChild(css);
</script>'
)
n1

n1$chart(margin = list(left=100)) 
n1

### as stated in comments, x is basically unworkable but this kind of works

n1$xAxis(
  axisLabel = 'Chromosome'
  ,tickFormat = "#!function(d){return d + "        " }!#"  #add space to the number
  ,rotateLabels=90                                         #rotate tick labels
)
n1$setTemplate(afterScript = '<script>
  var css = document.createElement("style");
  css.type = "text/css";
  css.innerHTML = ".nv-x .nv-axislabel { font-size: 50px; }";
  document.body.appendChild(css);
  css = document.createElement("style");
  css.type = "text/css";
  css.innerHTML = ".nv-y .nv-axislabel { font-size: 50px; }";
  document.body.appendChild(css);
 </script>'
)
n1$chart(margin=list(left=100,bottom=100))
n1

【讨论】:

  • 感谢您的回答,它对我有用,但现在我遇到了另一个问题。 轴标签超出了绘图区域所以,我现在必须定义绘图边距。你能帮助我吗 ??我擅长 R,但我不知道任何 javascript 或 html。
  • stackoverflow.com/questions/20334863/…。对于y,解决方案相当简单n1$chart(margin=list(left=100)),因此将 100 更改为有效的大小。我仍在研究x,因为显而易见的方法不起作用,例如n1$chart(margin=list(left=100,bottom=100))
  • 我实际上找不到处理x 的好方法,因为nvd3 不允许设置它。此外,我可以在脚本中手动更改,但它会在任何调整大小事件时更改回来。只是为了展示一些有效的东西,但我认为不可接受,我在之前的答案中添加了更多代码,用空格旋转 x 刻度标签,但我并不认为它是可行的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
  • 2017-10-04
  • 2019-08-05
  • 2021-03-11
  • 1970-01-01
相关资源
最近更新 更多