我现在尝试了超过1.5小时,但我无法让标签工作......: - \
我有以下矩阵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
我尝试用以下命令绘制它
ggplot() + geom_point(data=data.frame(clVrd), mapping=aes(x=clVrd[,1], y=clVrd[,2], label=clVrd[,3]))
即使我使用
在clVrd中设置了rownamesrownames(clVrd) <- seq(1:14)
它仍然无效。情节绘制,但没有标签...... 有人可以告诉我为什么吗?
感谢您的帮助!
更新: daroczig提到的方式有效,但我需要在更复杂的情节中添加图层。
整个代码是:
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,
-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))
这不起作用...... 首先绘制该图层不是一个选项
答案 0 :(得分:10)
首先,显示您的数据应该可以轻松粘贴到其他R会话中(正如我在其他主题中所写的那样),例如:
> 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))
或者像你一样,但没有行名!无论如何:
如果我是对的,geom_point
只是没有label
参数,所以不值得尝试这种方式。您应该在打印点后添加额外的图层,例如geom_text
的帮助,如:
ggplot(clVrd, aes(x=x, y=y, label=label)) + geom_point() + geom_text()
您可能希望将/ left / right等下面的文本从点移开。
更新:基于OP更新
首先,正如@hadley指出的那样:将矩阵转换为数据框并命名变量是一种很好的做法:
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')
基于以上命令,扩展您非常复杂的解决方案很简单:
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()
我还添加theme_bw()
只是为了满足我的口味:)这导致: