使用rCharts包创建气泡图(Highcharts)

时间:2014-03-21 18:09:06

标签: r highcharts rcharts

我试图在rCharts包中使用Highcharts创建以下气泡图。以下代码工作正常。

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

以下是气泡图的外观:[1]:http://i.imgur.com/Z7K1tfi.jpg

但是你可以看到数据是手动输入的。所以我做了以下列表,因为$ series(数据)列出了一个列表。但这不起作用。有人可以帮我告诉我以下代码有什么问题吗?

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

1 个答案:

答案 0 :(得分:1)

你写了as.character(dat$Company)而不是as.character(dat$Comp)。确保将new_dat(或您使用从复杂计算中得到的任何内容)的眼球移到控制台中以捕获这样的内容。如果仍然无效,请尝试使用date = new_dat包裹data = unname(new_dat)