【问题标题】:How to debug the R error "<var> must be either length 1 or the same as the data"?如何调试 R 错误“<var> 必须是长度 1 或与数据相同”?
【发布时间】:2018-05-12 16:43:36
【问题描述】:

我有这些数据:

# R> smokeyes_male

    LungCap.cc. Age..years. Height.inches. Smoke Gender Caesarean
211       6.575          10           63.2   yes   male       yes
220       7.000          10           62.8   yes   male       yes
252       9.350          11           71.2   yes   male        no
258       7.925          11           67.1   yes   male        no
280      10.275          11           72.2   yes   male        no
285       8.925          11           65.6   yes   male       yes
305       6.450          12           61.0   yes   male       yes
345       8.000          12           64.5   yes   male        no
370       9.750          13           72.8   yes   male        no
371       8.025          13           66.2   yes   male        no

我需要根据人的身高或是否剖腹产绘制肺活量和年龄之间的关系。

我收到以下错误:

library(ggplot2)
smoker_male_graph=ggplot(smokeyes_male,aes(x=smokeyes_male$age,y=smokeyes_male$LungCap.cc.)) + geom_point()
smoker_male_graph + facet_wrap(~smokeyes_male$Height.inches.)

错误:美学必须是长度1或与数据相同(33):x,y

代码如下:

smokeyes_male <- structure(list(LungCap.cc. = c(6.575, 7, 9.35, 7.925, 10.275, 
                                                8.925, 6.45, 8, 9.75, 8.025), Age..years. = c(10L, 10L, 11L, 
                                                                                              11L, 11L, 11L, 12L, 12L, 13L, 13L), Height.inches. = c(63.2, 
                                                                                                                                                     62.8, 71.2, 67.1, 72.2, 65.6, 61, 64.5, 72.8, 66.2), Smoke = structure(c(1L, 
                                                                                                                                                                                                                              1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "yes", class = "factor"), 
                                Gender = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                     1L), .Label = "male", class = "factor"), Caesarean = structure(c(2L, 
                                                                                                                      2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L), .Label = c("no", "yes"
                                                                                                                      ), class = "factor")), .Names = c("LungCap.cc.", "Age..years.", 
                                                                                                                                                        "Height.inches.", "Smoke", "Gender", "Caesarean"), class = "data.frame", row.names = c("211", 
                                                                                                                                                                                                                                               "220", "252", "258", "280", "285", "305", "345", "370", "371"
                                                                                                                                                        ))

【问题讨论】:

  • 根据你的例子,它应该是smokeyes_male$Age..years. 而不是smokeyes_male$age,第一个对我来说很好。并且不需要每次都使用smokeyes_male$,只需使用变量名

标签: r ggplot2 facet-grid


【解决方案1】:

您的代码基本上是正确的,只是需要进行一些清理。您可以将Caesarean 包含为color 美学。

ggplot(smokeyes_male, aes(x = Age..years., y = LungCap.cc., color = Caesarean)) + 
  geom_point() + 
  facet_wrap(~Height.inches.)

但是,从数据可视化的角度来看,我不确定该图的信息量有多大,因为您的刻面捕获了各个高度。这意味着,除非两个人的身高相同,否则您在每个子图上只会得到一分。 (也许这就是你的意图,但它似乎是一种从视觉上解释这些数据的奇怪方式。)

【讨论】:

    猜你喜欢
    • 2016-10-13
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多