代码之家  ›  专栏  ›  技术社区  ›  geo_dd

R中plotly图表的轴

  •  0
  • geo_dd  · 技术社区  · 7 年前

    我试图用 plotly 在R中( the chart I want ). 我有一个6列的数据框,我想在y轴上有文本,在x轴上有年份的月份。基于我的 dataframe 我有以下几点:

    one=c('bla','bla','bla',
            'bla','bla','bla','bla','bla','bla','bla',
            'bla','bla')
    two=c('09/25/2017','10/02/2017','11/15/2017','11/29/2017','01/01/2018','01/01/2018','04/01/2018','07/01/2018','09/01/2018','09/01/2018',
                    '08/01/2020','09/01/2020')
    three=c(1102,55,46,214,181,181,122,62,700,700,31,30)
    four=c('bla','bla','bla',
              'bla','bla','bla','bla',
              'bla','bla','bla'
              ,'bla','bla')
    five=c('A','B','C','D','E','F','G','H','E','I','J','E')
    
    df=data.frame(one,two,three,four,five)
    df$two =as.Date(df$two,"%m/%d/%Y")
    
    client = "my example"
    
    # Choose colors based on number of resources
    cols <- RColorBrewer::brewer.pal(length(unique(df$five)), name = "Set3")
    df$color <- factor(df$five, labels = cols)
    
    # Initialize empty plot
    p <- plot_ly()
    
    # Each task is a separate trace
    # Each trace is essentially a thick line plot
    # x-axis ticks are dates and handled automatically
    
    for(i in 1:(nrow(df))){
      p <- add_trace(p,
                     x = c(df$two[i], df$two[i] + df$three[i]),  # x0, x1
                     y = c(i, i),  # y0, y1
                     mode = "lines",
                     line = list(color = df$color[i], width = 20),
                     showlegend = F,
                     hoverinfo = "text",
    
                     # Create custom hover text
    
                     text = paste("Task: ", df$one[i], "<br>",
                                  "Duration: ", df$three[i], "days<br>",
                                  "Resource: ", df$five[i]),
    
                     evaluate = T  # needed to avoid lazy loading
      )
    }
    
    
    # Add information to plot and make the chart more presentable
    
    p <- layout(p,
    
                # Axis options:
                # 1. Remove gridlines
                # 2. Customize y-axis tick labels and show task names instead of numbers
    
                xaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6")),
    
                yaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6"),
                             tickmode = "array", tickvals = 1:nrow(df), ticktext = unique(df$one),
                             domain = c(0, 0.9)),
    
                # Annotations
    
                annotations = list(
                  # Add total duration and total resources used
                  # x and y coordinates are based on a domain of [0,1] and not
                  # actual x-axis and y-axis values
    
                  list(xref = "paper", yref = "paper",
                       x = 0.80, y = 0.1,
                       text = paste0("Total Duration: ", sum(df$three), " days<br>",
                                     "Total Resources: ", length(unique(df$five)), "<br>"),
                       font = list(color = "#ffff66", size = 12),
                       ax = 0, ay = 0,
                       align = "left"),
    
                  # Add client name and title on top
    
                  list(xref = "paper", yref = "paper",
                       x = 0.1, y = 1, xanchor = "left",
                       text = paste0("Gantt Chart: ", client),
                       font = list(color = "#f2f2f2", size = 20, family = "Times New Roman"),
                       ax = 0, ay = 0,
                       align = "left")
                ),
    
                plot_bgcolor = "#333333",  # Chart area color
                paper_bgcolor = "#333333")  # Axis area color
    
    p
    

    第一列(一)是文本

    所以我的问题是:

    1. 如何从y轴(而不是数字)上的任务(第一列)中获取文本?
    2. 我怎样才能得到x轴上的所有月份?

    非常感谢。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Bart VdW    7 年前

    问题1的答案:

    您当前的代码不能实现您想要的功能的原因是:

    ticktext = unique(df$one)
    

    自从 df$one 包含12个相同的值,只有1个唯一值,因此不是您需要的12个值。要解决这个问题,你可以使用 ticktext = df$one 或者确保你的标签 df$1 是唯一的(如您链接到的示例中所示)。例如,更改 df$1

    要指定x轴上的刻度间隔,可以使用 dtick 论点在您的情况下,这将导致在x轴的代码行中增加以下内容:

    xaxis = list(showgrid = F, tickfont = list(color = "#e6e6e6"),
                 dtick = "M1")
    

    其中 M 是指定以月为单位的间隔,1指定间隔为1个月(令人震惊!)。仅供参考,这将自动将勾号标签的方向更改为垂直,如果您想对此进行调整,可以使用 tickangle 论点