【问题标题】:add vertical and horizontal lines in hplot (rcharts)在 hplot (rcharts) 中添加垂直和水平线
【发布时间】:2016-02-23 16:17:47
【问题描述】:

我正在尝试在revealjs 演示文稿中的highchart (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...

【问题讨论】:

标签: r highcharts rcharts


【解决方案1】:

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

【讨论】:

  • 精彩的 jbkunst!有用。也很有趣 highcharter...我要试试这个新的包装器。谢谢
  • 它如何解释日期?垂直红线的值为 5.5,但这不是日期?有没有一种方法可以指定为特定日期,例如2020-01-01?
  • 我的错,我们只需要指定正确的格式:datetime_to_timestamp(as.Date("2017-01-10", tz = "UTC"))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-31
  • 2012-11-19
  • 1970-01-01
  • 2019-09-07
  • 2011-02-23
  • 2020-08-23
  • 1970-01-01
相关资源
最近更新 更多