3

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]
Mamoun Benghezal
  • 5,264
  • 7
  • 28
  • 33

3 Answers3

4

Using

txt <- "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"
dat <- read.table(text = txt, header = TRUE)

One option is to index into a vector of colours of length nlevels(ACTIVITY) using the ACTIVITY variable as the index.

cols <- c("red","green","blue","orange")
plot(LAT ~ LONG, data = dat, col = cols[dat$ACTIVITY], pch = 19)
legend("topleft", legend = levels(dat$ACTIVITY), col = cols, pch = 19, bty = "n")

This produces

enter image description here

To see why this works, cols is expanded to

> cols[dat$ACTIVITY]
[2] "green"  "red"    "green"  "orange" "orange" "blue"

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

library("ggplot2")
plt <- ggplot(dat, aes(x = LONG, y = LAT, colour = ACTIVITY)) +
  geom_point()
plt

which produces

enter image description here

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • If you are familiar with the base plotting system, and want some of the power of ggplot2, the `qplot` function gives you the basic functionality with a syntax closer to what is comfortable from base plot. – tegancp Jun 17 '15 at 16:24
  • @tegancp I've never been fond of `qplot()` and I think in the 2nd edition of the GGplot book, Hadley is downplaying the role of `qplot()`. – Gavin Simpson Jun 17 '15 at 16:36
  • I would probably agree that `qplot()` could generally be avoided -- once you understand how ggplot2 operates. However, I think it does have value as a stepping stone to using `ggplot()` for those who have used R's `plot()` (or plotting software which works similarly), but may be intimidated by the unfamiliar terminology and syntax. – tegancp Jun 17 '15 at 17:11
  • I really think it gets in the way when trying to learn how `ggplot()` plot works. – Gavin Simpson Jun 17 '15 at 17:12
2

Use the ggplot2 package it's faster and more beautiful.

library(ggplot2)
ggplot("your dataframe") + geom_point(aes(x = Latitude, y = Longitude, colour = factor(ACTIVITY)))
Mostafa90
  • 1,674
  • 1
  • 21
  • 39
1

Here's how I would do this, using a named vector to define the colors:

set.seed(1);
N <- 30;
df <- data.frame(activity=sample(c('Resting','Feeding','Walking','Sleeping'),N,replace=T),lat=runif(N,0,100),long=runif(N,0,100));
cols <- c(Resting='red',Feeding='blue',Walking='green',Sleeping='yellow');
par(mar=c(5,4,4,6)+0.1,xaxs='i',yaxs='i');
plot(df$lat,df$long,xlim=c(0,100),ylim=c(0,100),col=cols[as.character(df$activity)],main='Activity Locations',xlab='Latitude',ylab='Longitude');
legend(103,80,names(cols),col=cols,pch=1,xpd=T);

plot

bgoldst
  • 34,190
  • 6
  • 38
  • 64