2014-06-20 83 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