user3639557
user3639557

Reputation: 5281

point labels in R scatter plot

I have the following toy data

Xeafield (1999) (PER) ,1,0.5745375408
Lancelot et al. (1989),0.9394939494,0.4733405876
LemLM Xeafield (1997) (TER) ,0.6265126513,0.2959738847
Almore and Flemin (2001) (KER),0.4218921892,0.5745375408
Malek et al. (2006) (HER) ,0.4125412541,1
Charles and Osborne (2003),0.0308030803,0.1414581066

And trying a simple 2D plot in R with points labeled using the 1st column.

pdf('data.pdf', width = 7, height = 8)

d1 <- read.csv("data.csv", header=F, dec=".",sep = ",")
plot(as.matrix(d1[,2]), as.matrix(d1[,3]), col= "blue", pch = 19, cex = 1, lty = "solid", lwd = 2, ylim=c(0,1), xaxt = "n",yaxt = "n")

text(as.matrix(d1[,2]), as.matrix(d1[,3]), labels=as.matrix(d1[,1]), cex= 0.7, pos=3)

x_axis_range <- c(0,1) 
x_axis_labels <- c("Small","Large")
axis(1,at = x_axis_range, labels = x_axis_labels)

y_axis_range <- c(0,1) 
y_axis_labels <- c("Slow","Fast")
axis(2,at = y_axis_range, labels = y_axis_labels)
title(xlab="Memory", ylab="Speed",cex.lab=1)
dev.off()

But the plot doesn't come out right. A few issues I have: the axis label are messed up (it shows as.matrix ..., instead of the label I specified), and the margin of the plot is to small that node labels are cutoff. I am new to using R and plot, appreciate your help.

enter image description here

Upvotes: 0

Views: 113

Answers (1)

Marco Sandri
Marco Sandri

Reputation: 24252

A simple solution for your problem is to define axis labels and axis ranges in the plot function.

d1 <- structure(list(V1 = structure(c(6L, 3L, 4L, 1L, 5L, 2L), .Label = c("Almore and Flemin (2001) (KER)", 
"Charles and Osborne (2003)", "Lancelot et al. (1989)", "LemLM Xeafield (1997) (TER) ", 
"Malek et al. (2006) (HER) ", "Xeafield (1999) (PER) "), class = "factor"), 
    V2 = c(1, 0.9394939494, 0.6265126513, 0.4218921892, 0.4125412541, 
    0.0308030803), V3 = c(0.5745375408, 0.4733405876, 0.2959738847, 
    0.5745375408, 1, 0.1414581066)), .Names = c("V1", "V2", "V3"
), class = "data.frame", row.names = c(NA, -6L))

# Use xlab and ylab for axis labels and
# and xlim and ylim for setting axis ranges
plot(as.matrix(d1[,2]), as.matrix(d1[,3]), col= "blue", pch = 19, 
    cex = 1, lty = "solid", lwd = 2, ylim=c(-0.1,1.1), xaxt = "n",yaxt = "n",
    xlab="Memory", ylab="Speed",cex.lab=1, xlim=c(-0.1,1.1))

text(as.matrix(d1[,2]), as.matrix(d1[,3]), 
     labels=as.matrix(d1[,1]), cex= 0.7, pos=3)

x_axis_range <- c(0,1) 
x_axis_labels <- c("Small","Large")
axis(1,at = x_axis_range, labels = x_axis_labels)

y_axis_range <- c(0,1) 
y_axis_labels <- c("Slow","Fast")
axis(2,at = y_axis_range, labels = y_axis_labels)

enter image description here

Upvotes: 1

Related Questions