【问题标题】:Plot categorical variable绘制分类变量
【发布时间】:2020-05-08 02:57:20
【问题描述】:

我是 R 新手,我按照说明幻灯片进行绘图:

survey[["Program"]] 是数据框中的分类数据列。

> survey[["Program"]]  # returns the Program column as a vector
 [1] "PPM"   "PPM"   "PPM"   "Other" "PPM"   "PPM"   "PPM"   "Other" "PPM"   "Other" "MISM"  "PPM"   "MISM" 
[14] "Other" "PPM"   "PPM"   "PPM"   "PPM"   "PPM"   "Other" "PPM"   "MISM"  "PPM"   "PPM"   "PPM"   "MISM" 
[27] "PPM"   "Other" "Other" "PPM"   "Other"

但是,当我实现 plot(survey[["Program"]]) 时,我得到了错误:

Error in plot.window(...) : need finite 'ylim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

我不知道为什么我不能得到如图所示的结果。

【问题讨论】:

    标签: r plot


    【解决方案1】:

    该绘图命令仅适用于 factor 列。 (嗯,它适用于很多事情,但这里的混乱是因为变量不是一个因素。)比较例如

    plot(c('a', 'a', 'b', 'b', 'c'))
    
    Error in plot.window(...) : need finite 'ylim' values
    In addition: Warning messages:
    1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
    2: In min(x) : no non-missing arguments to min; returning Inf
    3: In max(x) : no non-missing arguments to max; returning -Inf
    

    plot(factor(c('a', 'a', 'b', 'b', 'c')))
    

    您可以通过调用str(survey) 来检查您的列的类别。您读取数据的方式可能与那些幻灯片所假设的不同。

    你可以使用

    plot(factor(survey[["Program"]]))
    

    或许

    barplot(table(survey[["Program"]]))
    

    得到相同的结果。

    【讨论】:

      猜你喜欢
      • 2015-03-18
      • 2023-03-31
      • 2019-08-09
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2012-04-15
      • 1970-01-01
      相关资源
      最近更新 更多