3

I am trying to display anything other than straight zeros on my secondary Y-axis using plotly with the data given in the R Markdown code below:

---
title: "Test Stack Overflow Trending"
author: ""
date: ""
output:
   html_document:
      self_contained: no
---

```{r plot1, results='asis', tidy=FALSE, echo=FALSE, fig.retina=NULL, message=FALSE, out.width = "92%", out.height="450px"}

require(plotly)
require(data.table)

mydt <- data.table(NAME = "Overall", Val = c(391234, 518834, 489567),perc = c(0.24,0.25,0.24), mytdate=c("2015-09-01","2015-10-01","2015-11-01"))

mydt$mydate <- as.Date(mydt$mytdate,"%Y-%m-%d")

p1 <- mydt %>%
  plot_ly(x = mydate, y = Val,type = "scatter", mode = "markers") %>%
add_trace(x = mydate, y = perc, yaxis = "y2", mode="lines+markers") %>%
  layout(showlegend = F, xaxis=list(title=""),yaxis=list(title=""),yaxis2 = list(side="right",overlay="y", ticks="inside",tickformat = ":04,2f"))

p1

```

I am using R 3.1.3 64-bit on a Windows 7 64-bit operating system. I am using the plotly package version: 2.0.16 (downloaded from Github in mid December of 2015).

Here is the image I am getting using Google Chrome, version: 47.0.2526.111 attached below. Notice that my right hand axis shows constant zeros. I want it to display something like 0.21, 0.22, 0.23, 0.24. I know there has to be a way to change this with the tickformat option, but I still have yet to figure it out. Output seen in Google Chrome

4

2 回答 2

4

我知道如何解决您的问题,但我需要在文档中找到它。所以你只需要玩弄你的情节的边距和大小。此外,它会给你足够的余量来添加标签。

m = list(
    l = 50,
    r = 50,
    b = 100,
    t = 100,
    pad = 4
)

并添加一个layout()和只是玩heightwidth

p1 <- mydt %>%
    plot_ly(x = mydate, y = Val,type = "scatter", mode = "markers") %>%
    add_trace(x = mydate, y = perc, yaxis = "y2", mode="lines+markers") %>%
    layout(showlegend = F, xaxis=list(title=""),yaxis=list(title=""),yaxis2 = list(side="right",overlay="y", ticks="inside",tickformat = ":04,2f")) %>% layout(autosize = F, width = 750, height = 500, margin = m)

p1

在此处输入图像描述

于 2016-01-22T22:54:41.087 回答
0

好吧,我找到了解决我自己问题的方法。如果我删除布局中的 showlegend=F 选项,y2 轴将正确显示。如果我不想显示图例,我仍然无法弄清楚如何让它工作。另外,我在布局中有错误的选项,它应该是“覆盖”,而不是“覆盖”。如果可能的话,仍然希望使用 showlegend=F 获得更好的答案。

于 2016-01-22T22:35:43.137 回答