在包含两个图的散点图上标记点

时间:2015-11-20 03:41:23

标签: r plot ggplot2 reshape2

我想在散点图上标注点。我使用了以下代码。

d1 <- structure(list(x1 = c(31.7858,25.8623,23.7758,30.1564,28.179,25.3966,31.5879,40.3233,28.8774,27.4434,37.6055,38.2847,40.5864,38.586,32.5489,38.2273,45.6876,52.3452,42.6654,49.8273), y1 = c(3.62,2.38,1.98,2.71,2.25,2.37,2.28,2.30,3.06,2.22,2.66,2.83,4.16,2.83,3.10,3.00,4.52,5.61,3.95,6.45)), .Names = c("x1", "y1"), class = "data.frame", row.names = c(NA, 
-20L))
d2 <- structure(list(x2 = c(22.5824,17.8283,21.4613,20.0485,24.5318,23.0919,23.3925,27.2867,23.7026,23.7338,25.4886,26.7058,25.6673,24.991,25.2346,18.7816,26.2372,25.9479,25.819,25.1697), y2 = c(2.12,1.55,1.65,1.52,1.80,1.77,1.68,1.46,2.05,1.63,1.59,1.85,2.53,1.98,2.32,1.66,2.09,2.51,2.25,2.54)), .Names = c("x2", "y2"), class = "data.frame", row.names = c(NA, -20L))
library(reshape2)
library(ggplot2)
names(d2) <- c("x1", "y2")
df <- rbind(melt(d1, id.vars = "x1"), melt(d2, id.vars = "x1"))
ggplot(df, aes(x1, y = value, colour = variable)) + 
  geom_point() + labs(x = "x", y = "y") +
  scale_colour_manual(values = c("red", "blue"), labels = c("d1", "d2"))

如何标记积分?

标签= c(&#34; L&#34;,&#34; K&#34;,&#34; D&#34;,&#34; E&#34;,&#34; N&#34 ;,&#34; Q&#34;, &#34; S&#34;,&#34; G&#34;,&#34; H&#34;,&#34; W&#34; ,&#34; A&#34;,&#34; P&#34;,&#34; Y&#34;,&#34; V&#34;,&#34; M&#34;,&#34; C&#34 ;, &#34; R&#34;,&#34; F&#34;,&#34; I&#34;,&#34; T&#34;)

1 个答案:

答案 0 :(得分:0)

labels=c("L", "K", "D", "E", "N", "Q", "S", "G", "H", "W" , "A", "P", "Y", "V", "M", "C", "R", "F", "I", "T")
labels <- rep(labels, 2)

ggplot(df, aes(x1, y = value, colour = variable)) + geom_point() +
labs(x = "x", y = "y") + geom_text(aes(label=labels), vjust=-0.5) + 
scale_colour_manual(values = c("red", "blue"), labels = c("d1", "d2"))