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
  • 2,623
  • 4
  • 22
  • 55
  • `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
  • 23,792
  • 5
  • 62
  • 72
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
  • 674
  • 4
  • 14
  • 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 – M. Beausoleil Jun 26 '15 at 20:16