Skip to content
Prev 23186 / 29559 Next

plotKML - set color according to category

Dear Almut

You're no getting many responses to your question because you don't give information enough for us to see where the presumed plotting error is.

I think you're not telling the whole truth. You're saying

   "In my data table I have a column with classification category
   'phase' (e.g. migration, stopover, breeding, wintering etc.)."

If that is the case then all birds have the same number of factor levels or categories! You cannot have a column where subsets of the records have different numbers of levels. Let me demonstrate that by a small example.

## create tw data frames
dat.1 <- data.frame(x = 1:12, y = rnorm(12), A = gl(3, 4, labels = c("low", "middle", "high")))
dat.2 <- data.frame(x = 1:12, y = 2 + rnorm(12), A = gl(2, 6, labels = c("small", "big")))

## combine those row wise
alldat <- rbind(dat.1, dat.2)

## print number of levels and the labels
nlevels(alldat$A)
levels(alldat$A)

## plot data with a legend
plot(y ~ x, data = alldat, pch = 19, col = alldat$A, cex = 2, ylim = c(-2, 5))
legend("top", legend = levels(alldat$A), col = 1:nlevels(alldat$A), lty = 1, lwd = 10, ncol = 5)

## subset data according to a subset of the A factor
subdat <- subset(alldat, A %in% c("low", "big"))

## only "low" and "big" records
print(subdat)

## but still we have the same nummber and labels as in alldat
nlevels(subdat$A)
levels(subdat$A)

## and plotting will still have the same definition of factor A as in alldat
plot(y ~ x, data = subdat, pch = 19, col = subdat$A, cex = 2, ylim = c(-2, 5))
legend("top", legend = levels(subdat$A), col = 1:nlevels(subdat$A), lty = 1, lwd = 10, ncol = 5)

So why do you have different levels for different birds? Did you originally get your data seperated for each bird and then later combined those is some way that was impropriate when having some columns with factors?

Or did you not combine data frames but in fact you looped over a number of data frames with different definition of the factor 'phase'. That would certainly explain your problem with different number of categories.

Nobody can really help you before you provide an R coded small reproducible example that shows us the problem that you have (see e.g. http://www.r-bloggers.com/three-tips-for-posting-good-questions-to-r-help-and-stack-overflow).



Yours sincerely / Med venlig hilsen

Frede Aakmann T?gersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender. 



-----Original Message-----
From: R-sig-Geo [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of Almut Schlaich - Werkgroep Grauwe Kiekendief
Sent: 4. august 2015 10:37
To: Adam Sparks
Cc: r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] plotKML - set color according to category

Dear Adam,

Thank you for your response!
I run your script on the raster data and I think the requirements are 
the same. For my point data set I already have a colomn with a factorial 
variable (in this case the phases of the annual cylce) and the points 
are colored according to them.

But imagine you would make maps of your raster data of two different 
countries, where the second one has different categories from the other 
(e.g. the second country could have values [13,14], 
(14,15],(15,16],(16,17] instead of [14,15],(15,16],(16,17],(17,18) for 
the Philippines). Then the plotKML would use the same four colors for 
these two maps, even though the values are not the same.

Could you now also define that e.g. [13,14] is white, (14,15) light 
yellow and so on and use the same color for the same value in the 
different plots?

Thanks again and kind regards,
Almut

Am 8/4/2015 um 7:09 AM schrieb Adam Sparks: