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

时间:2015-01-09 13:37:38

标签: r plot error-handling export plotly

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

  • 编码变量值
  • 1 Z1 Week.0 0
  • 2 Z2 Week.0 0
  • 3 Z3 Week.0 0
  • 4 Z4 Week.0 0
  • 5 Z5 Week.0 0
  • 6 Z6 Week.0 0
  • 7 Z7 Week.0 0
  • 8 Z8 Week.0 0
  • 9 Z9 Week.0 0
  • 10 Z101 Week.0 NA
  • 11 Z102 Week.0 NA
  • 12 Z1 Week.1 0
  • 13 Z2 Week.1 0
  • 14 Z3 Week.1 0
  • 15 Z4 Week.1 0
  • 16 Z5 Week.1 0
  • 17 Z6 Week.1 0
  • 18 Z7 Week.1 0
  • 19 Z8 Week.1 0

我用它绘图:

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

图表显示正常:

enter image description here

然而,当我想将它导出到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

我已经搜索过这些错误,但解释却让我感到困惑。 &#34;需要TRUE / FALSE的缺失值。&#34;如果你在你的过程中使用逻辑术语作为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 

并且较长的对象长度不是较短对象长度的倍数&#39;当你有不同长度的对象时应该弹出,但我只使用1个具有3行长度完全相同的对象。当我使用时,图形的值确实给了NULL请求那些行,但这应该发生..

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

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

感谢您的时间。

编辑:我尝试使用1:100的随机样本将不同的图形导出到Plot.ly并且工作得很好,我很确定错误在我的数据中,我可以&#39 ;弄清楚如何解决它。

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,但它们不应该被证明是一个问题,因为我之前使用过类似(更大)的数据集。 / p>

如果您愿意,我还可以使用.csv文件:https://www.mediafire.com/?jij1vlp14a29ntt

1 个答案:

答案 0 :(得分:2)

问题在于处理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)发表评论,因为仅加载&#34; ggplot2&#34;,我会收到以下错误:

Error: could not find function "theme_few"

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

分别。我想这些都是依赖性问题(也许你正在使用&#34; ggthemes&#34;?)的版本。

我不知道theme_few()有什么样的魔法,但如果我不在na.omit()上使用zmeltdata,我的pZ情节看起来很像像这样: enter image description here

Eww,&#34;第10周和第34周;在&#34;第1周和第34周之后来到而不是在&#34;第9周和第34周之后......所以你无论如何都不想把它发送到剧情!所以我无法完全重现你的ggplot示例。但是我想知道你是否真的想保留这些NA(CSV本身读取&#34; NA&#34;,我期待空白&#34;细胞&#34;)。你不想要预先处理这些吗?

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

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

同样,除了纯粹的显示/绘图考虑因素之外,由于这看起来像科学数据,你不想用实际数字来表示周数,或者如果你真的想要一个字符串,请填上数字? (&#34;第01周&#34;,&#34;第02周和#34;等) 而且看起来缺失的数据全都落后...... 10周以上的数据(还没有),对吧?

感谢您举报,

玛丽安