Lets say we have a shapefile of cities with a column called category



How can i plot the A category only etc?



but wasn't correct.

  • plot(subset(cities, category == "A"))? Cannot test it right now, though. – fdetsch Jul 8 '15 at 18:57
  • Looks terrific, but i would like to assign it first to a variable and then just plot it like my example that i tried. – gsa Jul 8 '15 at 19:13
  • @gsa, you are not creating a "variable" but rather a new object that is duplicating the associated elements in the source object. – Jeffrey Evans Jul 8 '15 at 21:54

It sounds like you want to subset your data and plotting is secondary. Please keep in mind that it is not always necessary to create a new object. If you are only wanting to plot subsets of the data it is far more efficient to take @fdetsch advice and subset in the call to plot. Here are some examples of subseting and plotting sp class data using the meuse dataset.

coordinates(meuse) <- ~x+y

# subset data and create new object based on column query
meuse.s1 <- meuse[meuse$soil == "1" ,]
plot(meuse.s1, pch=20)

# Plot above subset without creating new object
plot(meuse[meuse$soil == "1" ,], pch=20) 

Here is how you would iterate through each class and plot the subset

# set colors based on number of values
meuse.col = topo.colors(nlevels(meuse$soil))

# set plot so it is 2x2 in one plot canvas

He is where you define the for loop where "i" is the iterator value for 1 to nlevels in the variable (ie., 1,2,3). The plot function is subsetting each factor level (ie., levels(meuse$soil)[i]). The subset column does not need to be a factor. You could use unique() to return the number of unique values and iterate on that value rather than using levels() and nlevels().

The additional arguments in plot (ie.,g pch, col) are graphic parameters for the point symbol type and color. The box and title are functions for the plot that put a boundary and place a title on each plot. You can bring up help for all graphical parameters using ?par. I am using paste() in title() to create a string of "Soil class: i" for the plot title.

  for(i in 1:nlevels(meuse$soil)) {
    plot(meuse[meuse$soil == levels(meuse$soil)[i] ,], 
         pch=20, col=meuse.col[i])
    title(paste("Soil class: ", levels(meuse$soil)[i], sep=""))      

I would point out that one way to understand and digest code examples is to break them down and not necessary look at the code as a whole, as it can be confounding. One example is the way I am treating levels. If there is a variable set in a loop you can set it manually (ie., i=1). Then you can pull apart specific code in the loop. To understand specifics you can run just the commands and see what they return.

First, let's examine the data and classes.


You will see that meuse@data is a data.frame object with 8 numeric columns and 4 factorial columns including soil, which contains 3 levels (unique values). Now we can look at what the code is actually doing.

paste("Soil class: ", levels(meuse$soil)[i], sep="") 

This is generally a good practice because not only does it help you learn how the code works it also lets you test your own code to make sure it is doing what you think it is. A good example is making sure that coercion is resulting in the object type/class intended.

| improve this answer | |
  • Your assumption was right but the anaswer is way too complex.Could you use my own example as the base of your answer? – gsa Jul 8 '15 at 21:34
  • 2
    No, because your example is not reproducible. If you provide a reproducible example I will modify my answer to reflect your specific data. The meuse dataset is an internal data.frame object that is induced with the sp library so, you can work through my example and run the code directly. The only real difference in my example is that "meuse" is replacing "cities" and "soil" is replacing "category". – Jeffrey Evans Jul 8 '15 at 21:57
  • 1
    For topical reasons, check out this nice tutorial on writing reproducible examples. – fdetsch Jul 9 '15 at 8:31
  • i practised your answer and finally got it. Now if you can explain what you did in the " iterate through classes and plot" part and also add more comments in that specific segment would be great. – gsa Jul 9 '15 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.