3

I have a graph which i create with nPlot, i have two variable for X and Y axis and i want to add a third variable which i could see when I point my dots on the graph. For example, if a have X = age, Y = tall and Z = name, i want to be able to see changes of the tall depending on the age with the the name of the different person above dots.

Here is an example :

library(rCharts)
age <- c(1:20)
tall <- seq(0.5, 1.90, length = 20)
name <- paste(letters[1:20], 1:20, sep = "")
df <- data.frame(age, tall, name)
plot <- nPlot(x = "age", y = "tall", data = df, type = "scatterChart")
plot$xAxis(axisLabel = "the age")
plot$yAxis(axisLabel = "the tall")
plot  
Mostafa90
  • 1,674
  • 1
  • 21
  • 39

2 Answers2

3

You can use the tooltipContent option:

library(rCharts)
age <- c(1:20)
tall <- seq(0.5, 1.90, length = 20)
name <- paste(letters[1:20], 1:20, sep = "")
df <- data.frame(age = age, tall = tall, name = name)
n1 <- nPlot(age ~ tall ,data = df, type = "scatterChart")
n1$xAxis(axisLabel = "the age")
n1$yAxis(axisLabel = "the tall", width = 50)
n1$chart(tooltipContent = "#! function(key, x, y, e ){ 
  var d = e.series.values[e.pointIndex];
  return 'x: ' + x + '  y: ' + y + ' name: ' + d.name
} !#")
n1

enter image description here

e.series is the particular series the mouse is hovering, e.pointIndex is the index on the values of the series. So d = e.series.values[e.pointIndex] will give the data point for that series which is being hovered over. d.name will then give the name attribute.

Community
  • 1
  • 1
jdharrison
  • 30,085
  • 4
  • 77
  • 89
  • Thank you very much it's exactly what i want! – Mostafa90 Jun 20 '14 at 08:37
  • Just one more question, this graph will be in a shiny application and I would like to know if it's possible to send the user in another webpage when he click on a point of the graph. For example, in my case i will send the user in a xhtml page where he could see the complete identity of a1 or b2 ? Thanks in advance. – Mostafa90 Jun 20 '14 at 08:47
  • is it possible to assign `e.series.values[e.pointIndex]` to an R object? – Andriy T. Jul 10 '15 at 12:52
1

My version of R (3.0.2) cannot download the rChart package, but I can answer your question with base coding.

> age <- c(1:20)
> tall <- seq(0.5, 1.90, length = 20)
> name <- paste(letters[1:20], 1:20, sep = "")
> df <- data.frame(age, tall, name)
> plot(x = df$age, y = df$tall, xlab = "the age", ylab = "the tall")  
> text(df$age, df$tall, labels = df$name, adj = c(1,-0.3))  #Applies text based on x, y inputs (e.g. age and tall)

enter image description here

ccapizzano
  • 1,556
  • 13
  • 20
  • thank you for your help! but i always have the same problem because the option for nPlot are like "plot$..."! how could I add the text argument in my case ? thank you in advance. – Mostafa90 Jun 19 '14 at 14:23