【发布时间】:2018-02-14 08:31:51
【问题描述】:
我正在尝试使用共享 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
但是这会产生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
如果有人有任何见解,将不胜感激!
【问题讨论】:
-
请添加一些数据来填写您的变量,这样人们就不必猜测您的
df、domain_name、thm、received_dt是什么样的。任何有代表性的模拟数据就足够了。 -
我添加了一些关于我正在使用的数据的见解。我希望这能消除任何困惑。
-
没有。那是因为混乱不是主要问题。它与您需要帮助有关,因此您还提供了可以由查看此线程的任何人运行的代码 sn-ps。如果我必须花 15 分钟来找出“已经按照我想要的方式格式化”是什么意思,并用我自己的样本数据制作我自己的数据集,那么我只会生气并留下这个问题。如果你问自己为什么还没有人提供答案。
-
对此我深表歉意。我在发布的代码块中添加了更多信息,显示了我如何计算变量以及如何格式化
received_dt。希望这更符合对寻求帮助的人有用的内容。
标签: r highcharts shiny