[R-sig-Geo] plotKML - set color according to category

Frede Aakmann Tøgersen frtog at vestas.com
Tue Aug 4 11:37:53 CEST 2015

```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.

-----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
Cc: r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] plotKML - set color according to category

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],
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:
> Dear Almut,
> I've done this with raster data, but not points as you are describing.
> If you convert to a factor and then cut the data, it is possible to
> create a KML as you are describing. Towards the end of a script that I
> put together for the Manila R User Group there's some code that may help.
>
>
>
>
> On Wed, Jul 29, 2015 at 11:12 PM, Almut Schlaich - Werkgroep Grauwe
> Kiekendief <almut.schlaich at grauwekiekendief.nl
> <mailto:almut.schlaich at grauwekiekendief.nl>> wrote:
>
>     Dear Dr. Hengl,
>
>     First of all, I would like to thank you and your colleagues for
>     the great package plotKML that I really enjoy using.
>     This was indeed a missing link to visualize data, in our case
>     tracking data, in Google Earth to easily explore them visually.
>
>     I would like to ask you a question about the possibilities to
>     color the points according to a variable.
>     In my data table I have a column with classification category
>     "phase" (e.g. migration, stopover, breeding, wintering etc.).
>     Using e.g.:
>     kml_layer(data,colour=phase,colour_scale=worldgrids_pal[[4]]), I
>     get tracks nicely colored depending on category.
>
>     But the difficulty arises when one bird has less levels of the
>     category than another (e.g. one bird making no stopover).
>     In this case the same colors are used, but for different values of
>     the category, just in the order of first appearance.
>     To easily compare tracks between individuals I would like to color
>     the same category always in the same color. I tried to
>     define the colors for each category and give the column with a
>     color value per point to the kml_layer function, but this
>     didn't work. Is it possible to determine the color of each point?
>
>     At the moment I just use the combination of kml_open, kml_layer
>     and kml_close to produce the output.
>     Unfortuntely, the much more beautiful and handy plotKML for
>     Space-time Trajectory Data Frames doesn't work for the
>     tracks since they have too many points. Could you tell me what is
>     the limit for this? And might it be possible for more
>     points in near future?
>
>
>     Kind regards,
>     Almut Schlaich
>
>     --
>     Almut Schlaich
>
>     Werkgroep Grauwe Kiekendief
>     telefoon: +31(0)650459766
>     e-mail: almut.schlaich at grauwekiekendief.nl
>     <mailto:almut.schlaich at grauwekiekendief.nl>
>     postadres: Postbus 46, 9679 ZG Scheemda
>     www.grauwekiekendief.nl <http://www.grauwekiekendief.nl>
>
>     _______________________________________________
>     R-sig-Geo mailing list
>     R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
>     https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>
>
> --
> --
> Plant Disease Management Specialist
> IRRI
> DAPO Box 7777, Metro Manila, Philippines
> ph: +63(2) 580-5600 ext 2753
>

--
Almut Schlaich

Werkgroep Grauwe Kiekendief
telefoon: +31(0)650459766
e-mail: almut.schlaich at grauwekiekendief.nl
postadres: Postbus 46, 9679 ZG Scheemda
www.grauwekiekendief.nl