2014-07-03 116 views
0

我是rCharts的新手,其實這是我第一次嘗試。所以請原諒一個天真的問題。鼠標懸停線有點

我想創建一個簡單的rCharts視覺,它只有一條水平線(X軸),沒有Y軸。我希望能夠選擇長度,並且該行中的每個點都具有表示某些數據的鼠標懸停。另外我想爲某些特殊點添加顏色。 這看起來很簡單,但我在這方面遇到很大困難。

library(rCharts) 
age <- c(1:2000) 
dot <- rep(1,2000) 
name <- paste(letters[0], 1:2000, sep="") 
df <- data.frame(age=age,dot=dot,name=name) 
n1 <- nPlot(dot~age, data=df, type="scatterChart") 
n1$chart(tooltipContent = "#! function(key,x,y,e){var d = e.series.values[e.pointIndex];return 'x:'+ x + 'y:' + y + 'name:' + d.name }!#") 
n1 

現在,這將創建一個鼠標懸停的行,但在y = 1的行,並且還有x和y軸。我只需要一條線,就像有特殊事件標記的時間線。

非常感謝。

+0

是否有參考靜態圖表或草圖的圖表,可以幫助我們理解和可視化目標 – timelyportfolio

+0

@timelyportfolio我無法找到我想要的確切副本。但是,通過運行上述代碼獲得的圖表非常接近,它給出了一行y = 1,我們可以將鼠標懸停在每個點上。我想要的只是x軸上的那條線而沒有y軸。此外,我想標記(顏色不同)線上的一些特殊點。這裏是我從上面的腳本得到的圖表的鏈接。 https://www.dropbox.com/s/q43jxpoxn7vtu5p/n1.html –

+0

另一個想到的例子是一維散點圖。 –

回答

2

那麼,關閉y-axis是相當簡單的。我在代碼中添加了其他一些想法。

library(rCharts) 
age <- c(1:2000) 
dot <- c(
    rep(1,1000), 
    rep(2,1000) 
) 
name <- c(
    rep(letters[1], 1000), 
    rep(letters[2], 1000) 
) 
df <- data.frame(age=age,dot=dot,name=name) 
n1 <- nPlot(dot~age, data=df, group = "name", type="scatterChart") 
n1$chart(
    tooltipContent = "#! function(key,x,y,e){ 
    var d = e.series.values[e.pointIndex] 
    return 'x:'+ x + 'y:' + y + 'name:' + d.name 
    }!#", 
    showYAxis = FALSE, #turns off y axis 
    forceY = c(0,4)  #forces y axis to 0 min and 4 max 
) 
n1 

雖然我認爲這解決了這個問題,但我期待着一些事情。一種是如果你定義每個點,那麼數據會變大。我們可以更改爲lineChart以最小化發送的數據,但只有在定義的點上才顯示工具提示。我確信有一種方法可以將事件綁定到顯示工具提示的路徑上,但這超出了我的能力。我猜你可能會喜歡x是日期格式。如果您願意,我會很樂意演示一個例子。

n2 <- nPlot(
    dot~age 
    , data=data.frame(
    name = c(rep("A",2),rep("B",2)), 
    dot = c(1,1,2,2), 
    age = c(1,1000,1000,2000) 
) 
    , group = "name" 
    , type="lineChart" 
) 
n2$chart(
    tooltipContent = "#! function(key,x,y,e){ 
    var d = e.series.values[e.pointIndex] 
    return 'x:'+ x + 'y:' + y + 'name:' + d.name 
    }!#", 
    showYAxis = FALSE, #turns off y axis 
    forceY = c(0,4)  #forces y axis to 0 min and 4 max 
) 
n2 

這裏是一個基於評論

require(dplyr) 
require(magrittr) 
require(rCharts) 

data <- jsonlite::fromJSON('[ 
    [5, 
    0, "a1"], [480, 0, "a2"], [250, 0, "a3"], [100, 0, "a4"], [330, 0, "a5"], 
    [410, 0, "a6"], [475, 
        0, "a7"], [25, 0, "a8"], [85, 0, "a9"], [220, 0, "a10"], 
    [600, 0, "a11"] 

    ]') %>% as.data.frame(stringsAsFactors = F) %>% 
    set_colnames(c("x","y","name")) %>% 
    mutate(x = as.numeric(x)) %>% 
    mutate(y = as.numeric(y)) 

data$grp <- c(rep("A",3),rep("B",5),rep("Z",3)) 

n1 <- nPlot(
    y~x 
    ,group = "grp" 
    ,data = data 
    ,type="scatterChart" 
    ,height=200 
) 
n1$chart(
    tooltipContent = "#! function(key,x,y,e){ 
    var d = e.series.values[e.pointIndex] 
    var mytip = []; 
    mytip.push('<h1>name:'+ d.name + '</h1>'); 
    mytip.push('<p>x:' + x + '</p>'); 
    mytip.push('<p>y:' + y + '</p>'); 
    return mytip.join(''); 
    }!#", 
    showYAxis = FALSE, #turns off y axis 
    forceY = c(-1,1)  #forces y axis to 0 min and 4 max 
    ,showDistX = TRUE  #turn on markers on the x axis 
    ,showDistY = FALSE 
) 
n1$yAxis(
    showMaxMin = FALSE 
    ,axisLabel = NULL 
) 
n1 

注意額外的代碼:有在與干擾提示魚眼的錯誤;我們可以刪除以獲得工具提示立即出現

讓我知道這是如何工作的。

+0

感謝您的詳細解答。這與我想要的非常接近。我希望數據不在等級上,並且有一些標記點。這是我在d3中創建的https://www.dropbox.com/s/iqkgrn9rb630cix/tt.html。我正在嘗試將散點圖與x軸合併,散點圖上的點與x軸的其餘部分顏色不同。謝謝! –

+0

最終,建立一個像http://mostlyconjecture.com/blog/這樣的自定義圖表是有意義的,但我在答案中增加了一些代碼,希望顯示一些其他功能。讓我知道你的想法。 – timelyportfolio

+0

@ timeyportfolio,是否可以使用nplot修復圖形每個點上方的標籤?例如,如果我想打印出圖表,這將很方便。謝謝! –