繁体   English   中英

带有高字符的闪亮应用程序中的多个Y轴

[英]Multiple y-axes in shiny app w/ highcharter

我正在尝试使用共享一个x轴(天)但具有多个y轴(百分比和计数)的highcharter在闪亮的应用程序中绘制图形。 经过一些研究,似乎我应该使用'hc_yAxis_multiples'方法。 在左侧的y轴上,我显示了%。 在右侧y轴上,我希望显示计数。 有一个基于左y轴(%)的折线图和一个基于右y轴显示的堆叠条形图。

我已经能够覆盖两个图形,但是基于右y轴的条形图部分未格式化为相应的y轴。 根据我一直在看的东西,似乎这样的事情会产生我想要的结果:

##This first block is to show what the data types of the variables I'm using are and what the structure of my df looks like
df$inbox_rate <- df$total_inbox / df$total_volume
df$inbox_rate <- round((df$inbox_rate*100),0)
df$received_dt <- as.character(df$received_dt)
df$received_dt <- as.Date(df$received_dt, "%Y%m%d")
df <- df[order(df$received_dt),]

## This second block here is where I'm trying to build the chart with two Y-axes
hc <- highchart()%>%
hc_title(text = paste(domain_name,sep=""),align = "center") %>%
hc_legend(align = "center") %>%
hc_xAxis(type = "datetime", labels = list(format = '{value:%m/%d}')) %>%
hc_yAxis_multiples(list(title = list(text = "IPR"),labels=list(format = '{value}%'),min=0,
    max=100,showFirstLabel = TRUE,showLastLabel=TRUE,opposite = FALSE),
    list(title = list(text = "Total Subscribers"),min=0,max = max(df$total_users),
    labels = list(format = "{value}"),showLastLabel = FALSE, opposite = TRUE)) %>%
hc_plotOptions(column = list(stacking = "normal")) %>%
hc_add_series(df,"column",hcaes(
  x=received_dt,y=total_users,group=isp,yAxis=total_users)) %>%
hc_add_series(df,type="line",hcaes(
  x=received_dt,y=inbox_rate,group=isp,yAxis=inbox_rate)) %>%   
hc_exporting(enabled = TRUE) %>%
hc_add_theme(thm)
hc

但是,这会产生如下所示的内容

为了提供有关我正在使用的数据的更多信息, domain_name是一个字符串变量,如下所示: example.com total_users变量是一个数字,从0变化到约50000。 received_dt变量是日期,使用格式化as.Date(df$received_dt, "%Y%m%d") inbox_rate变量是一个百分比,从0到100。

即使条形的值相差很大,条形数也会全部显示到图形的整个高度。 重申一下,我希望条形图高度所基于的正确y轴是df$total_users的计数。 在hc_yAxis_multiples函数中,给出了两个列表。 我认为第一个列表给出了左侧的y轴,第二个列表给出了右侧的y轴。 我可以找到的最接近我的问题的答案是此stackoverflow响应给出的

如果有人有任何见识,将不胜感激!

您使用的yAxis中陈述hc_add_series似乎是关闭。 首先,它不应位于hcaes ,其次,它是一个数字,指定系列所属的轴(按hy_yAxis_multiple调用中的出现顺序)。 因此hc_add_series(..., yAxis = 1)应使用hc_add_series(..., yAxis = 1)将序列分配给第二个(右)轴。

下面是一个(完全不言自明的,独立的,最小的)示例,显示了它应该如何工作。

library(highcharter)

df <- data.frame(
  total_inbox = c(2, 3, 4, 5, 6),
  total_volume = c(30, 30, 30, 30, 30),
  total_users = c(300, 400, 20, 340, 330),
  received_dt = c("20180202", "20180204", "20180206", "20180210", "20180212"),
  isp = "ProviderXY"
)

df$inbox_rate <- df$total_inbox / df$total_volume
df$inbox_rate <- round((df$inbox_rate*100),0)
df$received_dt <- as.character(df$received_dt)
df$received_dt <- as.Date(df$received_dt, "%Y%m%d")
df <- df[order(df$received_dt),]

hc <- highchart()%>%
  hc_xAxis(type = "datetime", labels = list(format = '{value:%m/%d}')) %>%
  hc_yAxis_multiples(list(title = list(text = "IPR"),labels=list(format = '{value}%'),min=0,
                          max=100,showFirstLabel = TRUE,showLastLabel=TRUE,opposite = FALSE),
                     list(title = list(text = "Total Subscribers"),min=0,max = max(df$total_users),
                          labels = list(format = "{value}"),showLastLabel = FALSE, opposite = TRUE)) %>%
  hc_plotOptions(column = list(stacking = "normal")) %>%
  hc_add_series(df,type="column",hcaes(x=received_dt,y=total_users,group=isp),yAxis=1) %>%
  hc_add_series(df,type="line",hcaes(x=received_dt,y=inbox_rate,group=isp))

hc

也许以此为例说明问题中的代码应如何。 复制粘贴可运行,没有外部变量,并且减去此处无关紧要的所有内容(例如主题和图例)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM