Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中的plotly折线图中的线的中间部分添加注释_R_Plotly_R Plotly - Fatal编程技术网

在R中的plotly折线图中的线的中间部分添加注释

在R中的plotly折线图中的线的中间部分添加注释,r,plotly,r-plotly,R,Plotly,R Plotly,我试图在r-绘图折线图中的每条线的中心部分添加文本注释,如下面的图1所示。但是,文本注释显示在行的两端,如图2所示。有人能帮忙解决这个问题吗 library(plotly) 图书馆(RColorBrewer) df library(plotly) library(RColorBrewer) df <- read.csv("https://cdn.rawgit.com/plotly/datasets/master/GanttChart-updated.csv", st

我试图在r-绘图折线图中的每条线的中心部分添加文本注释,如下面的图1所示。但是,文本注释显示在行的两端,如图2所示。有人能帮忙解决这个问题吗

library(plotly)
图书馆(RColorBrewer)
df
library(plotly)
library(RColorBrewer)

df <- read.csv("https://cdn.rawgit.com/plotly/datasets/master/GanttChart-updated.csv", stringsAsFactors = F)
df$Start <- as.Date(df$Start, format = "%m/%d/%Y")
client = "Sample Client"
cols <- brewer.pal(length(unique(df$Resource)), name = "Set3")
df$color <- factor(df$Resource, labels = cols)

p <- plot_ly()

for(i in 1:(nrow(df) - 1)){
    p <- add_trace(p,
             x = c(df$Start[i], df$Start[i] + df$Duration[i]),
             y = c(i, i), 
             mode = "lines",
             line = list(color = df$color[i], width = 30),
             showlegend = F,
             hoverinfo = "text",                
             text = paste("Task: ", df$Task[i], "<br>",
                          "Duration: ", df$Duration[i], "days<br>",
                          "Resource: ", df$Resource[i]),
             evaluate = T  
   )
 p <- add_annotations(p,
                      x = c(df$Start[i], df$Start[i] + df$Duration[i]),  
                      y = c(i, i),  
                      xref = "x",
                      yref = "y",
                      mode = "text",
                      text = df$Task[i],
                      textposition = 'middle',
                      textfont = list(color = '#000000', size = 16)
   )
 }

p