2015-01-15 16 views
5

Vorrei forzare tutti i segni di graduazione e le etichette di spunta a comparire lungo l'asse in un nPlot rCharts dalla libreria nvd3. Ho provato diversi approcci senza successo.rCharts nvd3 tick delle librerie forza

Questo è il comportamento di default:

df <- data.frame(x = 1:13, y = rnorm(13)) 
library(rCharts) 
n <- nPlot(data = df, y ~ x, type = 'lineChart') 
n$yAxis(showMaxMin = FALSE) 

enter image description here

Mi piacerebbe avere tutti i dati in 1:13 spettacolo lungo l'asse x.

In definitiva, ho personalizzato segni di graduazione voglio mostrare ugualmente distanziati con la seguente sostituzione:

n$xAxis(tickFormat = "#! function (x) { 
    ticklabels = ['0-1000', '1000-1500', '1500-1700', '1700-1820', '1820-1913', 
     '1913-1950', '1950-1970', '1970-1990', '1990-2012', '2012-2030', 
     '2030-2050', '2050-2070', '2070-2100'] 
     return ticklabels[x-1]; 
} !#") 

enter image description here

Spero che sia chiaro il motivo per cui voglio avere tutti i segni di spunta e etichette stampate (e equidistanti).

Per dare un'idea del prodotto finito, ecco un impressione ggplot2:

library(ggplot2) 
df <- data.frame(x = c('0-1000', '1000-1500', '1500-1700', '1700-1820', '1820-1913', 
    '1913-1950', '1950-1970', '1970-1990', '1990-2012', '2012-2030', '2030-2050', 
    '2050-2070', '2070-2100'), y = rnorm(13), z = "group1") 
ggplot(data = df, aes(x = x, y = y, group = z)) + geom_line() 

enter image description here

Qui ci sono diverse cose che ho provato, sulla base di diversi suggerimenti che ho trovato qua e là: nessuno lavoro.

In base alla mia lettura dei documenti, ho pensato che questo avrebbe funzionato:

n$xAxis(tickFormat = "#! function (x) { 
    return [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13][x-1]; 
} !#") 

ho anche provato questo, sulla remota possibilità:

n$xAxis(ticks = 13) 

Ho anche cercato di coniugare tickValues e tickFormat ma senza successo.

Ho anche pensato di scrivere una sceneggiatura, ma ancora una volta la mia comprensione della biblioteca nvd3 era insufficiente.

n$setTemplate(afterScript = 
    "<script> 
     var chart = nv.models.lineChart(); 
     var newAxisScale = d3.scale.linear(); 
      .range(d3.extent(chart.axes[0]._scale.range())) 
      .domain([1, d3.max(chart.axes[0]._scale.domain())]) 
     chart.axes[0].shapes.call(
      d3.svg.axis() 
      .orient('bottom') 
      .scale(newAxisScale) 
      .ticks(13) 
      //.tickValues() 
      //.tickFormat(d3.format()) 
     ).selectAll('text') 
      .attr('transform','') 
    </script>" 
) 

Nessuno di questi errori di report nella console, ma nessuno di essi modifica l'aspetto del primo grafico sopra.

+0

Qui è una questione connessa, ma non avere una risposta ei commenti lì non si applicano al mio problema (si applica a una data lungo l'asse x, ma ho una stringa arbitraria) . http://stackoverflow.com/questions/25120598/strings-as-tick-values-for-rcharts-nvd3-line-chart – PatrickT

+0

E anche questa domanda simile che sto osservando ora suggerisce che non c'è speranza ... http : //stackoverflow.com/questions/25481853/rcharts-nvd3-linechart-with-categorical-x-axis – PatrickT

risposta

3

Si scopre che non ho impostato correttamente tickValues come ho ottenuto la sintassi confusa con tickFormat. Ecco una soluzione rCharts. Il d3 o nvd3 soluzione corrispondente deve essere facile dedurre.

n <- nPlot(data = df, y ~ x, type = 'lineChart') 
n$yAxis(showMaxMin = FALSE) 
n$addParams(height = 500, width = 1000) 
n$xAxis(tickValues = "#! function (x) {  
    tickvalues = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; 
    return tickvalues; 
} !#") 
n$xAxis(tickFormat = "#! function (x) { 
    tickformat = ['0-1000', '1000-1500', '1500-1700', '1700-1820', '1820-1913', 
     '1913-1950', '1950-1970', '1970-1990', '1990-2012', '2012-2030', '2030-2050', 
     '2050-2070', '2070-2100']; 
    return tickformat[x-1]; 
} !#") 
n 

Notate come il codice ha tickvalues in tickValues ma tickformat[x-1] in tickFormat.

enter image description here

Problemi correlati