I tried it now for over 1.5h but I can't get the labeling to work... :-\
I have the following matrix clVrd
[,1] [,2] [,3]
[1,] 0.6618725 -0.04065907 1
[2,] 0.4646620 0.09859806 2
[3,] 0.9388307 0.05681554 3
[4,] 1.1809942 0.12906415 4
[5,] 1.5476428 0.49644973 5
[6,] -0.1855485 0.30445869 6
[7,] 0.4525888 0.49559198 7
[8,] -0.4004534 -0.06419374 8
[9,] -1.0669191 0.17292748 9
[10,] -0.9372038 0.02601539 10
[11,] 0.5617849 -5.21857716 11
[12,] -0.9370099 -0.05539107 12
[13,] 0.6803453 0.21223368 13
[14,] 1.3040601 0.47598799 14
I try to plot it with the following command
ggplot() + geom_point(data=data.frame(clVrd), mapping=aes(x=clVrd[,1], y=clVrd[,2], label=clVrd[,3]))
Even if I set the rownames in clVrd with
rownames(clVrd) <- seq(1:14)
It still don't work. The plot is drawn, but without labels... Can someone tell my why?
Thanks for your help!
Update: The way daroczig mentioned works, but I need to add the layer in a more complex plot.
The whole code is:
ggplot() + geom_segment(data=data.frame(test), aes(x=lines[,1], y=lines[,2], xend=lines[,3], yend=lines[,4]), color='grey', alpha = I(0.2)) +
layer(data=data.frame(clVrd), mapping=aes(x=clVrd[,1], y=clVrd[,2], label=clVrd[,3]), geom = "point", stat="identity", size = I(4), color='black', shape=2) +
layer(data=data.frame(mid), mapping=aes(x=mid[,1], y=mid[,2]), geom = "point", stat="identity", size = I(4), color='black', shape=22) +
opts(legend.position = "none") +
scale_x_continuous('Dimension 1') +
scale_y_continuous('Dimension 2') +
opts(axis.title.y = theme_text(vjust=0.2, angle=90)) +
opts(axis.title.x = theme_text(vjust=0.2, angle=0))
clVrd <- structure(c(0.661872475367366, 0.464661963750941, 0.938830748772559,
1.18099419280727, 1.54764276890947, -0.185548540145922, 0.452588804431998,
-0.400453362842998, -1.06691910257802, -0.937203794581474, 0.56178488859136,
-0.937009852049824, 0.680345320757602, 1.30406005677017, -0.0406590744874299,
0.0985980588613432, 0.0568155367351373, 0.129064147637601, 0.496449734351345,
0.304458694719769, 0.495591983450893, -0.0641937419346143, 0.172927477311218,
0.0260153913187109, -5.21857716109913, -0.0553910678963988, 0.212233679072247,
0.475987991276203, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14), .Dim = c(14L, 3L))
mid <- structure(c(-0.195877196644293, 0.511580674503935, -0.658596511353487,
0.468086299528672, -1.24317797052758, -2.35077729859073, -0.55472838558667,
0.131409158984094, 0.759205542363384, 0.824786100242867, -1.86673280298486,
-0.482522295670435, 0.301242780883547, 0.414223673077983, 0.262357298984787,
-1.49648725810236, 0.238301422916755, 0.0954886028288132, -2.13935195209412,
0.340899933083442, -0.816182528789355, 0.515281428093344, 0.134447522792538,
-4.09755608083992), .Dim = c(12L, 2L))
lines <- structure(c(0.661872475367366, 0.661872475367366, 0.661872475367366,
0.661872475367366, 0.661872475367366, 0.661872475367366, 0.661872475367366,
0.661872475367366, 0.661872475367366, 0.661872475367366, 0.464661963750941,
0.464661963750941, 0.464661963750941, 0.464661963750941, 0.464661963750941,
0.464661963750941, 0.464661963750941, 0.464661963750941, 0.464661963750941,
0.464661963750941, 0.938830748772559, 0.938830748772559, 0.938830748772559,
0.938830748772559, 0.938830748772559, 0.938830748772559, 0.938830748772559,
0.938830748772559, 0.938830748772559, 0.938830748772559, 1.18099419280727,
1.18099419280727, 1.18099419280727, 1.18099419280727, 1.18099419280727,
1.18099419280727, 1.18099419280727, 1.18099419280727, 1.18099419280727,
1.18099419280727, 1.54764276890947, 1.54764276890947, 1.54764276890947,
1.54764276890947, 1.54764276890947, 1.54764276890947, 1.54764276890947,
1.54764276890947, 1.54764276890947, -0.185548540145922, -0.185548540145922,
-0.185548540145922, -0.185548540145922, -0.185548540145922, -0.185548540145922,
-0.185548540145922, -0.185548540145922, -0.185548540145922, -0.185548540145922,
-0.185548540145922, 0.452588804431998, 0.452588804431998, 0.452588804431998,
0.452588804431998, 0.452588804431998, 0.452588804431998, 0.452588804431998,
0.452588804431998, 0.452588804431998, 0.452588804431998, -0.400453362842998,
-0.400453362842998, -0.400453362842998, -0.400453362842998, -0.400453362842998,
-0.400453362842998, -0.400453362842998, -0.400453362842998, -0.400453362842998,
-0.400453362842998, -0.400453362842998, -1.06691910257802, -1.06691910257802,
-1.06691910257802, -1.06691910257802, -1.06691910257802, -1.06691910257802,
-1.06691910257802, -1.06691910257802, -1.06691910257802, -1.06691910257802,
-1.06691910257802, -1.06691910257802, -0.937203794581474, -0.937203794581474,
-0.937203794581474, -0.937203794581474, -0.937203794581474, -0.937203794581474,
-0.937203794581474, -0.937203794581474, -0.937203794581474, -0.937203794581474,
-0.937203794581474, -0.937203794581474, 0.56178488859136, 0.56178488859136,
0.56178488859136, 0.56178488859136, -0.937009852049824, -0.937009852049824,
-0.937009852049824, -0.937009852049824, -0.937009852049824, -0.937009852049824,
-0.937009852049824, -0.937009852049824, -0.937009852049824, -0.937009852049824,
-0.937009852049824, -0.937009852049824, 0.680345320757602, 0.680345320757602,
0.680345320757602, 0.680345320757602, 0.680345320757602, 0.680345320757602,
0.680345320757602, 0.680345320757602, 0.680345320757602, 0.680345320757602,
-0.0406590744874299, -0.0406590744874299, -0.0406590744874299,
-0.0406590744874299, -0.0406590744874299, -0.0406590744874299,
-0.0406590744874299, -0.0406590744874299, -0.0406590744874299,
-0.0406590744874299, 0.0985980588613432, 0.0985980588613432,
0.0985980588613432, 0.0985980588613432, 0.0985980588613432, 0.0985980588613432,
0.0985980588613432, 0.0985980588613432, 0.0985980588613432, 0.0985980588613432,
0.0568155367351373, 0.0568155367351373, 0.0568155367351373, 0.0568155367351373,
0.0568155367351373, 0.0568155367351373, 0.0568155367351373, 0.0568155367351373,
0.0568155367351373, 0.0568155367351373, 0.129064147637601, 0.129064147637601,
0.129064147637601, 0.129064147637601, 0.129064147637601, 0.129064147637601,
0.129064147637601, 0.129064147637601, 0.129064147637601, 0.129064147637601,
0.496449734351345, 0.496449734351345, 0.496449734351345, 0.496449734351345,
0.496449734351345, 0.496449734351345, 0.496449734351345, 0.496449734351345,
0.496449734351345, 0.304458694719769, 0.304458694719769, 0.304458694719769,
0.304458694719769, 0.304458694719769, 0.304458694719769, 0.304458694719769,
0.304458694719769, 0.304458694719769, 0.304458694719769, 0.304458694719769,
0.495591983450893, 0.495591983450893, 0.495591983450893, 0.495591983450893,
0.495591983450893, 0.495591983450893, 0.495591983450893, 0.495591983450893,
0.495591983450893, 0.495591983450893, -0.0641937419346143, -0.0641937419346143,
-0.0641937419346143, -0.0641937419346143, -0.0641937419346143,
-0.0641937419346143, -0.0641937419346143, -0.0641937419346143,
-0.0641937419346143, -0.0641937419346143, -0.0641937419346143,
0.172927477311218, 0.172927477311218, 0.172927477311218, 0.172927477311218,
0.172927477311218, 0.172927477311218, 0.172927477311218, 0.172927477311218,
0.172927477311218, 0.172927477311218, 0.172927477311218, 0.172927477311218,
0.0260153913187109, 0.0260153913187109, 0.0260153913187109, 0.0260153913187109,
0.0260153913187109, 0.0260153913187109, 0.0260153913187109, 0.0260153913187109,
0.0260153913187109, 0.0260153913187109, 0.0260153913187109, 0.0260153913187109,
-5.21857716109913, -5.21857716109913, -5.21857716109913, -5.21857716109913,
-0.0553910678963988, -0.0553910678963988, -0.0553910678963988,
-0.0553910678963988, -0.0553910678963988, -0.0553910678963988,
-0.0553910678963988, -0.0553910678963988, -0.0553910678963988,
-0.0553910678963988, -0.0553910678963988, -0.0553910678963988,
0.212233679072247, 0.212233679072247, 0.212233679072247, 0.212233679072247,
0.212233679072247, 0.212233679072247, 0.212233679072247, 0.212233679072247,
0.212233679072247, 0.212233679072247, -0.195877196644293, 0.511580674503935,
-0.658596511353487, 0.468086299528672, -1.24317797052758, -0.55472838558667,
0.131409158984094, 0.759205542363384, 0.824786100242867, -0.482522295670435,
-0.195877196644293, 0.511580674503935, -0.658596511353487, 0.468086299528672,
-1.24317797052758, -0.55472838558667, 0.131409158984094, 0.759205542363384,
0.824786100242867, -0.482522295670435, -0.195877196644293, 0.511580674503935,
-0.658596511353487, 0.468086299528672, -1.24317797052758, -0.55472838558667,
0.131409158984094, 0.759205542363384, 0.824786100242867, -0.482522295670435,
-0.195877196644293, 0.511580674503935, -0.658596511353487, 0.468086299528672,
-1.24317797052758, -0.55472838558667, 0.131409158984094, 0.759205542363384,
0.824786100242867, -0.482522295670435, -0.195877196644293, 0.511580674503935,
-0.658596511353487, 0.468086299528672, -0.55472838558667, 0.131409158984094,
0.759205542363384, 0.824786100242867, -0.482522295670435, -0.195877196644293,
0.511580674503935, -0.658596511353487, 0.468086299528672, -1.24317797052758,
-0.55472838558667, 0.131409158984094, 0.759205542363384, 0.824786100242867,
-1.86673280298486, -0.482522295670435, -0.195877196644293, 0.511580674503935,
-0.658596511353487, 0.468086299528672, -1.24317797052758, -0.55472838558667,
0.131409158984094, 0.759205542363384, 0.824786100242867, -0.482522295670435,
-0.195877196644293, 0.511580674503935, -0.658596511353487, 0.468086299528672,
-1.24317797052758, -0.55472838558667, 0.131409158984094, 0.759205542363384,
0.824786100242867, -1.86673280298486, -0.482522295670435, -0.195877196644293,
0.511580674503935, -0.658596511353487, 0.468086299528672, -1.24317797052758,
-2.35077729859073, -0.55472838558667, 0.131409158984094, 0.759205542363384,
0.824786100242867, -1.86673280298486, -0.482522295670435, -0.195877196644293,
0.511580674503935, -0.658596511353487, 0.468086299528672, -1.24317797052758,
-2.35077729859073, -0.55472838558667, 0.131409158984094, 0.759205542363384,
0.824786100242867, -1.86673280298486, -0.482522295670435, 0.468086299528672,
-0.55472838558667, 0.759205542363384, -0.482522295670435, -0.195877196644293,
0.511580674503935, -0.658596511353487, 0.468086299528672, -1.24317797052758,
-2.35077729859073, -0.55472838558667, 0.131409158984094, 0.759205542363384,
0.824786100242867, -1.86673280298486, -0.482522295670435, -0.195877196644293,
0.511580674503935, -0.658596511353487, 0.468086299528672, -1.24317797052758,
-0.55472838558667, 0.131409158984094, 0.759205542363384, 0.824786100242867,
-0.482522295670435, 0.301242780883547, 0.414223673077983, 0.262357298984787,
-1.49648725810236, 0.238301422916755, -2.13935195209412, 0.340899933083442,
-0.816182528789355, 0.515281428093344, -4.09755608083992, 0.301242780883547,
0.414223673077983, 0.262357298984787, -1.49648725810236, 0.238301422916755,
-2.13935195209412, 0.340899933083442, -0.816182528789355, 0.515281428093344,
-4.09755608083992, 0.301242780883547, 0.414223673077983, 0.262357298984787,
-1.49648725810236, 0.238301422916755, -2.13935195209412, 0.340899933083442,
开发者_StackOverflow中文版-0.816182528789355, 0.515281428093344, -4.09755608083992, 0.301242780883547,
0.414223673077983, 0.262357298984787, -1.49648725810236, 0.238301422916755,
-2.13935195209412, 0.340899933083442, -0.816182528789355, 0.515281428093344,
-4.09755608083992, 0.301242780883547, 0.414223673077983, 0.262357298984787,
-1.49648725810236, -2.13935195209412, 0.340899933083442, -0.816182528789355,
0.515281428093344, -4.09755608083992, 0.301242780883547, 0.414223673077983,
0.262357298984787, -1.49648725810236, 0.238301422916755, -2.13935195209412,
0.340899933083442, -0.816182528789355, 0.515281428093344, 0.134447522792538,
-4.09755608083992, 0.301242780883547, 0.414223673077983, 0.262357298984787,
-1.49648725810236, 0.238301422916755, -2.13935195209412, 0.340899933083442,
-0.816182528789355, 0.515281428093344, -4.09755608083992, 0.301242780883547,
0.414223673077983, 0.262357298984787, -1.49648725810236, 0.238301422916755,
-2.13935195209412, 0.340899933083442, -0.816182528789355, 0.515281428093344,
0.134447522792538, -4.09755608083992, 0.301242780883547, 0.414223673077983,
0.262357298984787, -1.49648725810236, 0.238301422916755, 0.0954886028288132,
-2.13935195209412, 0.340899933083442, -0.816182528789355, 0.515281428093344,
0.134447522792538, -4.09755608083992, 0.301242780883547, 0.414223673077983,
0.262357298984787, -1.49648725810236, 0.238301422916755, 0.0954886028288132,
-2.13935195209412, 0.340899933083442, -0.816182528789355, 0.515281428093344,
0.134447522792538, -4.09755608083992, -1.49648725810236, -2.13935195209412,
-0.816182528789355, -4.09755608083992, 0.301242780883547, 0.414223673077983,
0.262357298984787, -1.49648725810236, 0.238301422916755, 0.0954886028288132,
-2.13935195209412, 0.340899933083442, -0.816182528789355, 0.515281428093344,
0.134447522792538, -4.09755608083992, 0.301242780883547, 0.414223673077983,
0.262357298984787, -1.49648725810236, 0.238301422916755, -2.13935195209412,
0.340899933083442, -0.816182528789355, 0.515281428093344, -4.09755608083992
), .Dim = c(131L, 4L))
and this don't work... Plotting this layer first is not an option
First of all, showing your data should be in a way that could be easily pasted to other's R sessions (as I wrote in your other topic also), like:
> dput(clVrd)
structure(list(x = c(0.464662, 0.9388307, 1.1809942, 1.5476428,
-0.1855485, 0.4525888, -0.4004534, -1.0669191, -0.9372038, 0.5617849,
-0.9370099, 0.6803453, 1.3040601), y = c(0.09859806, 0.05681554,
0.12906415, 0.49644973, 0.30445869, 0.49559198, -0.06419374,
0.17292748, 0.02601539, -5.21857716, -0.05539107, 0.21223368,
0.47598799), label = 2:14), .Names = c("x", "y", "label"), class = "data.frame", row.names = c(NA,
-13L))
Or like you did, but without row names! Anyway:
If I am right, geom_point
just do not have a label
parameter, so it is not worth trying this way. You should add an extra layer after printing your points with e.g. the help of geom_text
, like:
ggplot(clVrd, aes(x=x, y=y, label=label)) + geom_point() + geom_text()
You might want to shift the text under/left/right etc. away from the points.
UPDATE: based on OP update
First, as @hadley pointed out: converting your matrices to data frames and naming variablesis a good practice:
clVdr <- as.data.frame(clVdr)
names(clVdr) <- c('x', 'y', 'label')
mid <- as.data.frame(mid)
names(mid) <- c('x', 'y')
lines <- as.data.frame(lines)
names(lines) <- c('x', 'y', 'xend', 'yend')
Extending your quite complex solution is easy based on the above command:
ggplot(mapping = aes(x, y)) +
geom_segment(data=lines, aes(xend = xend, yend = yend),
color = 'grey', alpha = 0.2) +
geom_point(data = clVdr, size = 4, shape = 2) +
geom_point(data = mid, size = 4, shape = 22) +
geom_text(data = clVdr, aes(label=label), hjust = 0, vjust = 0) +
opts(legend.position = "none") +
scale_x_continuous('Dimension 1') +
scale_y_continuous('Dimension 2') +
opts(axis.title.y = theme_text(vjust = 0.2, angle = 90)) +
opts(axis.title.x = theme_text(vjust = 0.2, angle = 0)) +
theme_bw()
I also added theme_bw()
just to supply my taste :) This results in:
精彩评论