nik
nik

Reputation: 2584

how to plot specific rows based on a match of row name in another color

I have a data like this

data<- structure(list(CountM = c(45.64666293, 59.36843063, 60.03210967, 
46.82944874, 43.03029792, 61.84389291, 43.03029792, 52.67732934, 
46.64235489, 51.49190061, 44.43427349), CountB = c(45.40011971, 
58.8366324, 63.25364716, 47.25941075, 46.48880606, 61.84736213, 
46.48880606, 52.88026788, 51.98626832, 50.02266849, 51.5420982
)), .Names = c("CountM", "CountB"), class = "data.frame", row.names = c("India", 
"USA", "USA2", "USA3", "USA4", "Holand", "Germany", "Italy", 
"China", "Rusa", "India2"))

if I want to plot it , I can do it

plot(data$CountM,data$CountB)

I want to plot USA3, Italy and India2 with another color.

how can I do it?

Upvotes: 0

Views: 519

Answers (1)

Evan Friedland
Evan Friedland

Reputation: 3194

There are many ways to go about coloring points in a plot but in this case I believe the simple route of adding colored points works best.

plot(data$CountM,data$CountB) # Plot
interested_in <- c("USA3","Italy","India2")

# Adding colored points
these <- which(rownames(data) %in% interested_in)
points(data[these,"CountM"], data[these,"CountB"], 
       pch = 16, # for a filled circle
       col = c("red","blue","orange")) # for 3 colors given "these" has length 3

# Adding labels
text(data[these,"CountM"], data[these,"CountB"], # x and y points
     labels = interested_in, # the actual text
     pos = 3, # 1,2,3,4 = below,left,top,right
     cex = 1.1, # sizing
     font = 2) # bolding

Edit for non overlapping labels

#install.packages("maptools") # run once in your console, no "#"
library(maptools)
pointLabel(data[these,"CountM"], data[these,"CountB"], labels = interested_in, 
           pos = 3, # 1,2,3,4 = below,left,top,right
           cex = 1.1, # sizing
           font = 2)

Upvotes: 2

Related Questions