R中的曲线图

时间:2017-11-28 10:55:26

标签: r plotly gantt-chart

我正在尝试使用R中的plotly重现此甘特图(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轴上获得所有月份?
  3. 谢谢。

1 个答案:

答案 0 :(得分:1)

回答问题1:

您当前代码没有按照您的意愿执行操作的原因是:

ticktext = unique(df$one)

由于df$one包含12个相同的值,因此只有1个唯一值,因此不需要12个。要解决此问题,您可以使用ticktext = df$one或确保df$one中的标签是唯一的(与您链接的示例中的情况一样)。例如,将df$one更改为bla1,bla2,...,bla12将适用于您当前的示例。

问题2:

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

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

其中M指定您想要以月为单位的间隔,而1指定您希望间隔为1个月(令人震惊!)。仅供参考,这会自动将刻度标签的方向更改为垂直,如果您想对其进行调整,可以使用tickangle参数。