【问题标题】:Main title and legend.position issue on ggplotlyggplotly 上的主标题和 legend.position 问题
【发布时间】:2017-05-08 13:16:53
【问题描述】:

我在如何在 ggplotly 中定位主标题和图例时遇到问题。我希望我的主标题位于图表顶部并左对齐。我还希望我的图例位于图表的底部中心

这是我的代码

library(ggplot2)
library(dplyr)
library(tidyr)
library(plotly)

Month_Names <- c("2010-11","2010-12",
             "2011-01","2011-02","2011-03","2011-04","2011-05","2011-06","2011-07","2011-08","2011-09","2011-10","2011-11","2011-12",
             "2012-01","2012-02","2012-03","2012-04","2012-05","2012-06","2012-07","2012-08","2012-09","2012-10","2012-11","2012-12",
             "2013-01","2013-02","2013-03","2013-04","2013-05","2013-06","2013-07","2013-08","2013-09","2013-10","2013-11","2013-12",
             "2014-01","2014-02","2014-03","2014-04","2014-05","2014-06","2014-07","2014-08","2014-09","2014-10","2014-11","2014-12",
             "2015-01","2015-02","2015-03","2015-04","2015-05","2015-06","2015-07","2015-08","2015-09","2015-10","2015-11","2015-12",
             "2016-01","2016-02","2016-03","2016-04","2016-05","2016-06","2016-07","2016-08","2016-09","2016-10","2016-11","2016-12",
             "2017-01")
Actual_volume <- c(54447,57156,
               52033,49547,58718,53109,56488,60095,54683,60863,56692,55283,55504,56633,
               53267,52587,54680,55569,60013,56985,59709,61281,54188,59832,56489,55819,
               59295,52692,56663,59698,61232,57694,63111,60473,58984,64050,54957,63238,
               59460,54430,58901,61088,60496,62984,66895,62720,65591,67815,58289,72002,
               61054,60329,69283,68002,63196,72267,71058,69539,71379,70925,68704,76956,
               65863,70494,77348,70214,74770,77480,69721,83034,76761,77927,79768,81836,
               75381)

df_data <- data.frame(Month_Names, Actual_volume) 

trendDateRange1 <- c("2010-11-01", "2017-01-31")
trendDateRange2 <- c("2012-01-01", "2012-12-31")
trendDateRange3 <- c("2013-01-01", "2013-12-31")
numoftrends <- 3

trends <- data_frame(Start = c("2010-11", "2012-01", "2013-01"),
                 End = c("2017-01", "2012-12", "2013-12"))
combined_data <- df_data %>%
                 crossing(trends) %>%
                 mutate(Month_Names = as.character(Month_Names),
TrendName = paste(Start, End, sep = "-")) %>%
filter(Month_Names >= Start,
     Month_Names <= End)


p <- ggplot(combined_data, aes(Month_Names, y = Actual_volume,
                      group = TrendName,
                      color = TrendName)) +
     geom_line() +
     labs(x=" ",y=" ") +
     labs(title = "New plot title") +
     theme(plot.title = element_text(family = "Trebuchet MS", color="blue", 
                      size=18, 
                              margin = margin(10, 0, 10, 0)),
    legend.position = "bottom",
    legend.title = element_blank(),
    axis.text.x = element_text(angle = 45, hjust = 1)
 )

print(ggplotly(p))

【问题讨论】:

    标签: r ggplot2 plotly


    【解决方案1】:

    我没有找到任何解决标题问题的方法。看看这是否有助于您了解传说。

    p <- ggplot(combined_data, aes(Month_Names, y = Actual_volume,
                               group = TrendName,
                               color = TrendName)) +
      geom_line() +
      labs(x=" ",y=" ") +
      labs(title = "New plot title") +
      theme(plot.title = element_text(family = "Trebuchet MS", color="blue", 
                                  size=18, 
                                  margin = margin(10, 0, 10, 0)),
        legend.position = "bottom",
        legend.title = element_blank(),
        #axis.text.x = element_text(angle = 45, hjust = 1)
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank()
      )
    
    ggplotly(p) %>% 
      layout(title = "New plot title",
         #xaxis = list(showticklabels = FALSE),
         legend = list(orientation = "h",
                       y = 0, x = 0))
    

    【讨论】:

    • 感谢 Rafael Cunha 抽出时间调查此事。但是,当我运行您的代码时,我收到一条错误消息:“长度错误(x$annotations):'x' 必须是一个列表。”我错过了什么吗?
    • 我不知道这个错误。尝试清除您的环境并运行代码的第一部分直到filter(Month_Names &gt;= Start, Month_Names &lt;= End),然后运行我发布的部分
    • 我多次清理了我的环境并按照您的建议运行它,但我仍然收到此错误。您是否尝试过运行它?我在 R Studio 1.0.136 上运行它。谢谢
    • 我跑了不止一次。我关闭了我的 RStudio,再次打开,再次运行。很奇怪。不知道是不是RStudio版本的问题(我的是0.99.903)
    • 嗨 Rafael - 我将 annonations = list() 添加到您的代码中。它运行了,但是我需要将我的axis.text.x 显示在图表上。这是我更改的代码。 ggplotly(p) %>% layout(title = "New plot title", #xaxis = list(showticklabels = FALSE), legend = list(orientation = "h", y = 0, x = 0), annotations = list( ))
    猜你喜欢
    • 1970-01-01
    • 2017-06-15
    • 2021-11-19
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 2013-11-01
    相关资源
    最近更新 更多