I have my data stored in the csv format it . I want to plot this data colored as per the activity means 4 different activities should be of 4 different color.
ACTIVITY LAT LONG
Resting 21.14169444 70.79052778
Feeding 21.14158333 70.79313889
Resting 21.14158333 70.79313889
Walking 21.14163889 70.79266667
Walking 21.14180556 70.79222222
Sleeping 21.14180556 70.79222222
I have tried the following codes but it didn't work:
ACTIVITY.cols <- cut(ACTIVITY, 5, labels = c("pink", "green", "yellow","red","blue"))
plot(Data$Latitude,Data$Longitude, col = as.character(ACTIVITY.cols)
and
plot(Data$Latitude,Data$Longitude, col=c("red","blue","green","yellow")[Data$ACTIVITY]
Using
One option is to index into a vector of colours of length
nlevels(ACTIVITY)
using theACTIVITY
variable as the index.This produces
To see why this works,
cols
is expanded tobecause
ACTIVITY
is a factor but stored numerically as 1,2,...,n.Other higher-level solutions are available, so consider the ggplot2 package for simple creation of the same plot.
which produces