gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:13:03 28 4
gpt4 key购买 nike

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

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

    我已经搜索了这些错误,但解释完全让我感到困惑。 “需要 TRUE/FALSE 的缺失值。”如果您在流程中使用逻辑术语如 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

    当您有不同长度的对象时,应该弹出“较长的对象长度不是较短对象长度的倍数”,但我只使用 1 个具有 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()可以,但如果我不使用 na.omit()zmeltdata , 我的 pZ plotly 如下所示:
    enter image description here

    Eww,“Week.10”在“Week.1”之后而不是在“Week.9”之后......所以你无论如何都不想把它发送给 plotly!所以我不能完全重现你的 ggplot 例子。但我想知道你是否真的想保留这些 NA(CSV 本身读取“NA”,我期待空白的“单元格”)。你不想对这些进行预处理吗?

    请注意,当我不使用 na.omit() 时会收到以下警告消息在 zmeltdata :
    Warning messages:
    1: Removed 20 rows containing missing values (geom_path).
    2: Removed 47 rows containing missing values (geom_point).

    同样,除了纯粹的显示/绘图考虑之外,由于这看起来像科学数据,您是否不想用实际数字对周数进行编号,或者如果您真的想要一个字符串则填充数字? (“Week.01”、“Week.02”等)
    看起来丢失的数据都在尾随...... 10+周没有数据(还),对吧?

    感谢举报,

    玛丽安

    关于r - 将绘图导出到 Plot.ly 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27861827/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com