# Plotting friend's heights names<-c("Adam", "Alice", "Brian", "Barbara", "Charles", "Carla", "Christina", "David") sex<-c("M","F","M","F","M","F","F","M") ages<-c(18, 19, 22, 21, 20, 29, 20, 19) heights<-c(167.5, 160, 170.5, 154, 179, 166, 158, 160.5) # plot(NULL,axes=FALSE,xlim=c(0,2),ylim=c(min(heights),max(heights)), xlab="",ylab="",main="My friends' heights") plot(NULL, axes=F, xlim=c(0,2), ylim=c(min(heights),max(heights)), xlab="", ylab="Height (cm)", main="My friends’ heights") axis(2, c(150, 155, 160,165, 170, 175, 180),at=c(150, 155, 160,165, 170, 175, 180), xpd=NA) ### sex.as.number<-sex sex.as.number<-gsub("M",1,sex.as.number) sex.as.number<-gsub("F",2,sex.as.number) #sex.as.number sex.as.number<-as.integer(sex.as.number) sex.as.number ### par(mfrow=c(1,2)) plot(NULL, axes=F, xlim=c(0,2), ylim=c(min(heights),max(heights)), xlab="", ylab="Height (cm)", main="My friends’ heights") axis(2, c(150, 155, 160,165, 170, 175, 180),at=c(150, 155, 160,165, 170, 175, 180), xpd=NA) colours1<-c("blue","deeppink") text(c(rep(1,8)),heights,names,col=colours1[sex.as.number]) for(i in 1:8){ arrows(0.65,heights[i],0,heights[i],code= 2, col=colours1[sex.as.number[i]],lwd=2,length=0.07)} # } to end the for loop # pdf("rplot.pdf") # graphics.off() ###### An alternative way avoiding coercing characters to numeric ###### using ifelse to change "M" and "F" to numbers 1 and 2 in one go sex.as.number <-NULL for (i in 1:8) ifelse(sex[i] == "M", sex.as.number[i]<-1, sex.as.number[i]<-2) colours1<-c("blue","deeppink") par(mfrow=c(1,2)) plot(NULL, axes=F, xlim=c(0,2), ylim=c(min(heights),max(heights)), xlab="", ylab="Height (cm)", main="My friends’ heights") axis(2, c(150, 155, 160,165, 170, 175, 180),at=c(150, 155, 160,165, 170, 175, 180), xpd=NA) text (c(rep(1,8)), heights, names, col=colours1[sex.as.number]) for (i in 1:8){ arrows(0.65, heights[i], 0, heights[i], code = 2, col=colours1[sex.as.number[i]], lwd=2, length=0.07) }