【问题标题】:How to plot Subset data without the unselected data in R如何在 R 中绘制没有未选择数据的子集数据
【发布时间】:2015-06-26 20:00:39
【问题描述】:

我想为此绘制图表(见下文),但在子集之后,R 正在绘制所有变量,但只绘制所选变量的数据。

test<-subset(OrchardSprays,treatment == "A")
plot(test$treatment, test$decrease)

那么有没有办法只绘制我想要的变量而不在我的原始数据框中删除它?

我不要这个!

【问题讨论】:

  • treatment 是一个因子变量。如果你把它设为character,我想你就不会再有麻烦了。

标签: r plot subset


【解决方案1】:

你可能想要droplevels:

test <- subset(OrchardSprays, treatment == "A")
test <- droplevels(test)
plot(test$treatment, test$decrease)

【讨论】:

【解决方案2】:

试试这个:

test<-subset(OrchardSprays,treatment == "A")
test$treatment <- as.character(test$treatment)
plot(test$treatment, test$decrease)

我认为问题在于 test$treatment 是一个包含多个级别的因素,而当您绘制子集时,plot 会在所有级别上出现。通过将test$treatment 设为字符串,您应该避免此问题。

【讨论】:

  • 您的观点非常好!但它说 as.character 存在问题:Error in plot.window(...) : need finite 'xlim' 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 这有效test&lt;-subset(OrchardSprays,treatment == "A") test$treatment &lt;- as.factor(test$treatment) plot(test$treatment, test$decrease)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 2015-09-07
  • 2014-11-21
  • 2023-02-05
  • 2014-03-11
  • 1970-01-01
  • 2019-04-19
相关资源
最近更新 更多