繁体   English   中英

R中的绘图图的坐标轴

[英]axis from plotly chart in R

我试图用R中的plotly我想要的图表 )重现此甘特图。 我有一个包含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轴(而不是数字)的任务(第1列)中获取文本?
  2. 如何获得x轴上的所有月份?

谢谢。

回答问题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参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM