在hplot中添加垂直和水平线(rcharts)

时间:2016-02-23 16:17:47

标签: r highcharts rcharts

我正在尝试在revealjs演示文稿中的高图(rcharts)中添加水平和垂直线条。 我试图以这种方式修改此post的代码:

require(xlsx) 
library(rCharts)

Perhplot.df <-read.xlsx("C:\\RDirectory\\AREALAVORO\\JOB\\RISULTATI2.xlsx", sheetName="completo2")

lDf <- split(Perhplot.df, Perhplot.df$variable)

h16 <- hPlot(protection ~ days, data = lDf$Exposure, 
type = "bubble", 
group = "label",
title = "By Days of Exposure", 
subtitle = "Move the mouse pointer on the bubbles to view the data",
size = "cluster_size", 
group = "label")
h16$set(width = 1000, height = 600)

ord <- c("Less than 1 week"=0,
"1-2 weeks"=1,
"3-4 weeks"=2,
"More than 4 weeks"=3,
"Mean"=4
)
h16$params$series <- lapply(h16$params$series, function(d){
temp = ord[d$name]
names(temp) = NULL
d$legendIndex = temp
return(d)
})
h16$yAxis(min = 35, max = 70, title = list(text = "Level of Protection"))
h16$xAxis(min = 0, max = 45, title = list(text = "Days of Exposure"))

dfy<-data.frame(y=c(35,58,70), x=c(18.8,18.8,18.8))
h16$layer(y~x,data=dfy,type="line",color=list(const = 'darkblue'))

h16$show('inline', include_assets = TRUE)

气泡图是好的,但后来我尝试添加垂直线我有这个错误:

Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : ‘layer’ is not a valid field or method name for reference class “Highcharts”

因此该解决方案适用于Dimple Charts,但不适用于Highcharts ......

1 个答案:

答案 0 :(得分:0)

rcharts highcharts plotLines相同。

您需要使用plotLines参数:

library("rCharts")
# Some data
x <- abs(rnorm(10))
# A graph with column
h <- Highcharts$new()
h$chart(type = "column")
h$series(data = x)
h$xAxis(categories = letters[1:10])
# the horizontal line
h$yAxis(title = list(text = "rnorm()"),
    plotLines = list(list(
      value = mean(x),
      color = '#ff0000',
      width = 3,
      zIndex = 4,
      label = list(text = "mean",
                   style = list( color = '#ff0000', fontWeight = 'bold' )
      ))))
h

哟添加垂直你可以通过xAxis更改yAxis。

或者如果你使用highcharter(这是R的一个新的highcharts包装器):

h2 <- highchart() %>% 
  hc_chart(type = "column") %>% 
  hc_add_serie(data = x) %>% 
  hc_xAxis(categories = letters[1:10]) %>% 
  hc_yAxis(title = list(text = "rnorm()"),
        plotLines = list(list(
          value = mean(x),
          color = '#ff0000',
          width = 3,
          zIndex = 4,
          label = list(text = "mean",
                       style = list( color = '#ff0000', fontWeight = 'bold'   )
          ))))
h2

来源:http://jkunst.com/highcharter/highcharts-api.html#hc_xaxis-and-hc_yaxis

enter image description here