将绘图导出到 Plot.ly 时出错

Errors when exporting plots to Plot.ly

我有这个数据(前 20 行的样本):

我使用以下方法绘制它:

pZ <- ggplot(zmeltdata,aes(x=variable,y=value,color=Codering,group=Codering)) + 
  geom_line()+
  geom_point()+
  theme_few()+
  theme(legend.position="right")+
  scale_color_hue(name = "Treatment group:")+
  scale_y_continuous(labels = percent)+
  ylab("Germination percentage")+
  xlab("Week number")+
  labs(title = "Z. monophyllum germination data")
pZ

图表显示得很好:

然而,当我想将其导出到 Plot.ly 时,出现以下错误:

> py <- plotly()
> response<-py$ggplotly(pZ)
Error in if (all(xcomp) && all(ycomp)) { : 
  missing value where TRUE/FALSE needed
In addition: Warning message:
In trace.list[[lind[1]]]$y == trace.list[[lind[2]]]$y :
  longer object length is not a multiple of shorter object length

我已经搜索了这些错误,但是解释让我很困惑。 "The missing value where TRUE/FALSE needed." 如果您在过程中使用逻辑术语 IF/ELSE/TRUE/FALSE 等,则应该会发生,而我根本不会!即使在检查图表值中的任何 NA 时,我也会得到:

> is.na(pZ)
       data      layers      scales     mapping       theme coordinates       facet    plot_env      labels 
      FALSE       FALSE       FALSE       FALSE       FALSE       FALSE       FALSE       FALSE       FALSE 

和 'longer object length is not multiple of shorter object length' 应该在你有不同长度的 object 时弹出,但我只使用 1 object 和 3 行完全相同长度.. 当我要求这些行时,图表的值确实给了我一个 NULL,但这是应该发生的..

> nrow(zmeltdata)
[1] 143
> nrow(test)
NULL

总而言之,我很困惑,不知道如何正确处理这些错误,有人可以详细说明吗?

感谢您的宝贵时间。

编辑:我尝试使用 1:100 的随机样本将不同的图表导出到 Plot.ly 并且效果很好,我很确定错误在我的数据中,我只是不知道如何解决它。

EDIT2:回应@Gregor:

> dput(head(zmeltdata, 20))
structure(list(Codering = structure(c(16L, 19L, 20L, 21L, 22L, 
23L, 24L, 25L, 26L, 17L, 18L, 16L, 19L, 20L, 21L, 22L, 23L, 24L, 
25L, 26L), .Label = c("B1", "C2", "C3", "C8", "M1", "M101", "M102", 
"M2", "M3", "M4", "M5", "M6", "M7", "M8", "M9", "Z1", "Z101", 
"Z102", "Z2", "Z3", "Z4", "Z5", "Z6", "Z7", "Z8", "Z9"), class = "factor"), 
    variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Week.0", 
    "Week.1", "Week.2", "Week.3", "Week.4", "Week.5", "Week.6", 
    "Week.7", "Week.8", "Week.9", "Week.10", "Week.11", "Week.12"
    ), class = "factor"), value = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
    NA, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Codering", 
"variable", "value"), row.names = c(NA, 20L), class = "data.frame")

还有尾巴:

> dput(tail(zmeltdata, 43))
structure(list(Codering = structure(c(19L, 20L, 21L, 22L, 23L, 
24L, 25L, 26L, 17L, 18L, 16L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 
26L, 17L, 18L, 16L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 17L, 
18L, 16L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 17L, 18L), .Label = c("B1", 
"C2", "C3", "C8", "M1", "M101", "M102", "M2", "M3", "M4", "M5", 
"M6", "M7", "M8", "M9", "Z1", "Z101", "Z102", "Z2", "Z3", "Z4", 
"Z5", "Z6", "Z7", "Z8", "Z9"), class = "factor"), variable = structure(c(10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L), .Label = c("Week.0", "Week.1", "Week.2", "Week.3", 
"Week.4", "Week.5", "Week.6", "Week.7", "Week.8", "Week.9", "Week.10", 
"Week.11", "Week.12"), class = "factor"), value = c(0.1, 0.06, 
0.05, 0.09, 0.04, 0.08, 0.05, 0.08, 0, 0, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("Codering", 
"variable", "value"), row.names = 101:143, class = "data.frame")

我对这些并不感到惊讶,数据集中有很多 NA,但它们不应该被证明是一个问题,因为我之前使用过类似(更大)的数据集。

我还有 .csv 文件供您使用:https://www.mediafire.com/?jij1vlp14a29ntt

问题是关于处理 NA 的...我通过 运行 得到了 https://plot.ly/~marianne2/417/z-monophyllum-germination-data/ 以下代码:

pZ <- ggplot(na.omit(zmeltdata), aes(x=variable, y=value, color=Codering,
                                 group=Codering)) +
  geom_line() +
  geom_point() +
  # theme_few() +
  theme(legend.position="right") +
  scale_color_hue(name="Treatment group:") +
  # scale_y_continuous(labels = percent) +
  ylab("Germination percentage") +
  xlab("Week number") +
  labs(title="Z. monophyllum germination data")

py$ggplotly(pZ, kwargs=list(fileopt="overwrite", filename="test_zdata"))

请注意,我必须注释掉 theme_few()scale_y_continuous(labels = percent),因为如果只加载 "ggplot2",我会得到以下错误:

Error: could not find function "theme_few"

Error in structure(list(call = match.call(), aesthetics = aesthetics,  : 
object 'percent' not found

分别。我想这些是依赖性问题(也许您使用的是 "ggthemes" 的版本?)。

我不知道theme_few()有什么神奇的作用,但是如果我不在zmeltdata上使用na.omit(),我的pZ情节是这样的:

Eww,"Week.10" 出现在 "Week.1" 之后,而不是 "Week.9" 之后......所以你无论如何都不想将它发送给 plotly!所以我无法完全重现您的 ggplot 示例。但我想知道您是否真的想保留这些 NA(CSV 本身显示为 "NA",我原以为是空白 "cells")。反正你不想 pre-process 这些吗?

请注意,当我不在 zmeltdata 上使用 na.omit() 时收到以下警告消息:

Warning messages:
1: Removed 20 rows containing missing values (geom_path).
2: Removed 47 rows containing missing values (geom_point).

同样,除了纯粹的 displaying/plotting 考虑之外,由于这看起来像是科学数据,您是否不想用实际数字对周进行编号,或者如果您确实需要字符串则填充数字? ("Week.01"、"Week.02" 等) 看起来丢失的数据都是尾随的……(还)没有 10 周以上的数据,对吗?

感谢您的报告,

玛丽安