Вопрос

I'm trying to create the following bubble chart with Highcharts in rCharts package. The following code works fine.

Rep <-  c(rep('John',5), rep('Bob',5), rep('Jen', 5))
Comp <- c('Goog', 'Yahoo', 'eBay', 'Oracle', 'Cisco', 'SAP', 'Oracle', 'MSFT', 'IBM', 'GE', 'JPMC' ,'BoA', 'Amazon', 'AE', 'Netflix')
Score <- round(runif(15,1,100))
repidx <- c(rep(2,5), rep(0,5), rep(1,5))
id <- 1:15
mv2 <- data.frame(Rep, Comp, Score, repidx, id)

a <- rCharts::Highcharts$new()
a$chart(type = 'bubble' , plotBorderWidth=0, zoomType='xy')
a$title(text='SPI')
a$xAxis(categories = attributes(mv2$Rep)$levels)
a$yAxis(min = 0, max = 100, startOnTick = FALSE, endOnTick = FALSE, 
        title=list(enabled = TRUE, text='Custom Made <i>SPI</i>'))
a$legend(enabled = FALSE)
a$plotOptions(bubble = list(dataLabels = list(enabled = TRUE, x = 0, 
                                              formatter="#! function() {
                                                return this.point.name;
                                              } !#", style=list(color= 'black'))))
a$series(data = list(
  list(x = 1, y = 20, z = 20, name = 'SAP') 
  , list(x = 1, y = 80, z = 80, name = 'YAHOO') 
  , list(x = 0, y = 67, z = 67, name = 'IBM') 
))
a

Here is how the bubble chart looks like: [1]: http://i.imgur.com/Z7K1tfi.jpg

But as you can see the data was entered manually. So I made the following list because a$series(data) take a list. But that does not working. Can someone help tell me what's wrong with the following code?

new_dat <- dlply(mv2, .(id), function(dat){ list(x=as.numeric(dat$repidx), 
 y=as.numeric(dat$Score), z=as.numeric(dat$Score), name=as.character(dat$Company))   } )

a <- rCharts::Highcharts$new()
a$chart(type = 'bubble' , plotBorderWidth=0, zoomType='xy')
a$title(text='SPI')
a$xAxis(categories = attributes(mv2$Rep)$levels)
a$yAxis(min = 0, max = 100, startOnTick = FALSE, endOnTick = FALSE, 
        title=list(enabled = TRUE, text='Custom Made <i>SPI</i>'))
a$legend(enabled = FALSE)
a$plotOptions(bubble = list(dataLabels = list(enabled = TRUE, x = 0, 
                                              formatter="#! function() {
                                                return this.point.name;
                                              } !#", style=list(color= 'black'))))
a$series(data = new_dat)
a
Это было полезно?

Решение

You wrote as.character(dat$Company) instead of as.character(dat$Comp). Make sure to eyeball new_dat (or anything you use derived from a complex calculation) to catch stuff like this in the console. If that still doesn't work, try to wrap date = new_dat with data = unname(new_dat).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top