将 ggplot 对象转换为 plotly 对象会创建与刻度值重叠的轴标题 [英] Converting ggplot object to plotly object creates axis title that overlaps tick values

查看:14
本文介绍了将 ggplot 对象转换为 plotly 对象会创建与刻度值重叠的轴标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个问题中描述的相同问题:

I had the same issue described in this question:

R:ggplot 和 plotly 轴边距不会改变

但是当我实施解决方案时,出现以下错误:

but when I implemented the solution, I got the following error:

警告:忽略未知的美学:文本我们建议您使用带有 ggplotly() 的开发版 ggplot2 安装它:devtools::install_github('hadley/ggplot2') tmp中的错误 [[2]] : 下标越界

Warning: Ignoring unknown aesthetics: text We recommend that you use the dev version of ggplot2 with ggplotly() Install it with: devtools::install_github('hadley/ggplot2') Error in tmp[[2]] : subscript out of bounds

这段代码会在我的机器上产生这个错误:

This code will produce this error on my machine:

library(gapminder)
library(plotly)
library(ggplot2)

lead <- rep("Fred Smith", 30)
lead <- append(lead, rep("Terry Jones", 30))
lead <- append(lead, rep("Henry Sarduci", 30))
proj_date <- seq(as.Date('2017-11-01'), as.Date('2017-11-30'), by = 'day')
proj_date <- append(proj_date, rep(proj_date, 2))
set.seed(1237)
actHrs <- runif(90, 1, 100)
cummActHrs <- cumsum(actHrs)
forHrs <- runif(90, 1, 100)
cummForHrs <- cumsum(forHrs)
df <- data.frame(Lead = lead, date_seq = proj_date,
                 cActHrs = cummActHrs,
                 cForHrs = cummForHrs)

makePlot <- function(dat=df, man_level = 'Lead') {
    p <- ggplot(dat, aes_string(x='date_seq', y='cActHrs',
                               group = man_level,
                               color = man_level),
                linetype = 1) +
         geom_line() +
         geom_line(data=df,
                   aes_string(x='date_seq', y = 'cForHrs',
                              group = man_level,
                              color = man_level),
                   linetype = 2)

    p <- p + geom_point(aes(text=sprintf('%s
Manager: %s
 MTD Actual Hrs: %s
MTD Forecasted Hrs: %s',
                                         date_seq, Lead, round(cActHrs, 2), round(cForHrs, 2))))

    p <- p + theme_classic() + ylab('Hours') + xlab('Date')

    gp <- ggplotly(p, tooltip = "text") %>% layout(hovermode = "compare")
    ### FIX IMPLEMENTED HERE ###
    gp[['x']][['layout']][['annotations']][[2]][['x']] <- -0.1
    gp %>% layout(margin = list(l = 75))

    return(gp)
}

## run the example
p1 <- makePlot()

推荐答案

试试这个:

makePlot <- function(dat=df, man_level = "Lead") {
  dat$var <- dat[,man_level]
  dat$grp <- ""
  p <- ggplot(dat, aes(x=date_seq, y=cActHrs,
              group = var, color = var,
              text=paste0("Manager:", date_seq,"<br>MTD Actual Hrs:", round(cActHrs, 2),
                          "<br>MTD Forecasted Hrs:", round(cForHrs, 2))),
              linetype = 1) +
       geom_line() +
       geom_line(data=dat,
                 aes(x=date_seq, y = cForHrs,
                     group = var, color = var),
                 linetype = 2) +
        geom_point() +
        theme_classic() + ylab("Hours") + xlab("Date") +
        scale_color_discrete(name=man_level) +
        facet_wrap(~grp)

    gp <- ggplotly(p, tooltip = "text") 
    # Set y-axis label position
    gp[["x"]][["layout"]][["annotations"]][[2]][["x"]] <- -0.06
    # Set legend label position    
    gp[["x"]][["layout"]][["annotations"]][[3]][["y"]] <- 0.93
    gp <- gp %>% layout(margin = list(l = 120, b=70), hovermode = "compare")

    return(gp)
}

这篇关于将 ggplot 对象转换为 plotly 对象会创建与刻度值重叠的轴标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆