我尝试使用高清系统在闪亮的应用中渲染图形,该高清系统共享x轴(天),但有多个y轴(百分比和计数)。经过一些研究,似乎我应该使用' 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
然而,这会产生something that looks like this。
为了更好地了解我使用的数据,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轴,第二个列表给出右侧。我能找到的最接近我的问题的答案是this stackoverflow response
如果有人有任何见解,我们将非常感激!
答案 0 :(得分:0)
您在yAxis
中对hc_add_series
语句的使用似乎已被取消。首先,它不应该在hcaes
内,第二,它是一个数字,指定该系列属于哪个轴(按hy_yAxis_multiple
调用中出现的顺序)。因此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
也许以此为例说明问题中的代码应该是什么样的。 Copy-Paste-Runnable,没有外部变量,减去所有不重要的事情(例如主题和图例)。