0

I want to plot a graph of this (see below), but after subsetting, R is plotting all variables, but only the data of the selected ones.

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

So is there a way to plot only the variable that I want without deleting it in my original data frame?

I don't want this! enter image description here

M. Beausoleil
  • 3,141
  • 6
  • 29
  • 61
  • `treatment` is a factor variable. If you make it a `character`, you'll have no further trouble, I reckon. – Frank Jun 26 '15 at 20:04

2 Answers2

2

You probably want droplevels:

test <- subset(OrchardSprays, treatment == "A")
test <- droplevels(test)
plot(test$treatment, test$decrease)
jeremycg
  • 24,657
  • 5
  • 63
  • 74
0

Try this:

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

I think the issue is that test$treatment is a factor with a bunch of levels, and that plot picks up on all of the levels when you plot the subset. By making test$treatment a string, you should avoid this issue.

ila
  • 709
  • 4
  • 15
  • You got a really good point here! But it says that there is a problem with 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` This works `test<-subset(OrchardSprays,treatment == "A") test$treatment <- as.factor(test$treatment) plot(test$treatment, test$decrease)` – M. Beausoleil Jun 26 '15 at 20:16