2014-06-20 54 views
3

我试图增加使用 NVD3和rCharts创建的图中x和y轴的字体大小。这是我的剧情代码。任何帮助表示赞赏。nvd3 scatterPlot与rCharts在R:增加标签的字体大小?

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') 
+0

现在最简单的方法是添加CSS在http://stackoverflow.com/questions/17883017/adjusting- axis-labels-nvd3-graph-in-rcharts,但我知道这可能不是一个可行的解决方案。我会尝试以另一种方式工作,并提供一个从R直接起作用的答案。 – timelyportfolio

回答

6

其实,我想我发现了一个解决方案,感谢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 
+0

感谢您的回答,它适用于我,但现在我有另一个问题。 **轴标签超出了绘图区**,所以我现在必须定义绘图边距。你可以帮我吗 ??我擅长R,但我不知道任何JavaScript或HTML。 – Koundy

+0

请参阅http://stackoverflow.com/questions/20334863/rcharts-how-to-add-axis-lables-and-headings-to-nvd3-chart。对于'y',解决方案相当简单''n1 $ chart(margin = list(left = 100))''所以将100改为适用的大小。因为显而易见的方法不起作用,所以我仍在使用'x'',例如'n1 $ chart(margin = list(left = 100,bottom = 100))''。 – timelyportfolio

+0

我实际上找不到处理'x'的好方法,因为nvd3不允许这样设置。另外,我可以在脚本中进行手动更改,但在任何重新调整大小事件时都会被更改。只是为了展示一些有用的东西,但我认为不可接受,我在之前的答案中添加了更多的代码,用空格旋转x刻度标签,但我并不认为它是可行的。 – timelyportfolio