2015-11-20 68 views
0

我想在散點圖上標記點。我使用了下面的代碼。散點圖上的標記點包含兩個圖

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

我該如何標記要點? (「L」,「K」,「D」,「E」,「N」,「Q」, 「S」,「G」,「H」,「W」,「 A」, 「P」, 「Y」, 「V」, 「M」, 「C」, 「R」, 「F」, 「I」, 「T」)

+0

@TheTime謝謝爲了您的評論。我必須在哪裏添加此代碼?我試過但錯誤來了。 – avinash

+0

爲什麼不使用'geom_text(aes(labels = df $ labels,x = x,y = y))' –

回答

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")) 
+0

非常感謝您的回答。我試過你的代碼。但我得到的錯誤如下錯誤:ggplot2不知道如何處理類函數的數據 – avinash

+0

Hhhmmm ...這很奇怪,代碼在RGui和RStudio中工作無故障 –

+0

@Elzwawi謝謝。 – avinash