R散点图中的点标签

时间:2017-07-16 10:34:55

标签: r plot

我有以下玩具数据

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

在R中尝试使用第1列标记的点的简单2D图。

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()

但情节并没有正确。我遇到的一些问题:轴标签搞砸了(它显示为.matrix ...,而不是我指定的标签),并且绘图的边距很小,节点标签被截断。我是新手使用R和情节,感谢你的帮助。

enter image description here

1 个答案:

答案 0 :(得分:1)

解决问题的一个简单方法是在plot函数中定义轴标签和轴范围。

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