Olaf Renk
Olaf Renk

Reputation: 23

R plot_ly Error: Column `x` must be length 1 or 4, not 2

for this i Error: Column x must be length 1 or 4, not 2

library(plotly)
library(RColorBrewer)
coul = brewer.pal(4, "BuPu") 
Vertrag <- read.table(text =
'"Vert","Start","Ende"
"1","01.07.2017","31.03.2018"
"1","31.03.2018","30.06.2018"
"1","01.07.2018","31.03.2019"
"1","31.03.2019","30.06.2019"',
sep=',',header =T )
Vertrag$Start <- as.Date(Vertrag$Start, format ="%d.%m.%Y")
Vertrag$Ende <- as.Date(Vertrag$Ende, format ="%d.%m.%Y")
p <- plot_ly()
for(i in 1:(nrow(Vertrag))){
  p <- add_trace(p,
                 x = c(Vertrag$Start[i],Vertrag$Start[i] + difftime(Vertrag$Ende[i],Vertrag$Start[i], units = "days")),
             y = c(Vertrag$Vert),
             mode = "lines",
             line = list(color = colorRampPalette(coul)(10),width = 20))
}
p

But when "Vertrag" have only two rows it works.

Any idea why and whats wrong?

Thanks

Olaf

Upvotes: 2

Views: 3706

Answers (1)

what_alnk
what_alnk

Reputation: 81

Changing y axis in add_trace() function from y = c(Vertrag$Vert) to y = c(Vertrag$Vert[i]) worked for me.

But I am not sure this is the plot you want to.

Upvotes: 1

Related Questions