如何使用 r 在 ChartSeries() 柱的顶部添加数字
How to add numbers on top of ChartSeries() bars using r
我基本上想做的是在每个栏的顶部放置 s$cluster
个数字
library("quantmod")
# xts object
s <-
structure(c(1300, 1301.349976, 1281.199951, 1316.900024, 1312.310059,
1278, 1304.439941, 1304.709961, 1313.900024, 1323.089966, 1314.98999,
1301.719971, 1291.630005, 1271.01001, 1281.199951, 1311.040039,
1288.329956, 1273, 1301.189941, 1287.030029, 1307.890015, 1317.030029,
1288.959961, 1299.699951, 372300, 453800, 347600, 376400, 488200,
567300, 1301.189941, 1287.030029, 1307.890015, 1317.030029, 1288.959961,
1299.699951, 0.0034153392307692, 0.00258192266643587, 0.0255230051909361,
0.00470038870619693, 0.00204214772387123, 0.0185602276995305,
-0.00643845769230766, -0.0233142248891853, 0, -0.0044498328599013,
-0.0182731991083518, -0.00391236306729259, 0.000915339230769252,
-0.0110039169048249, 0.02083208321946, 9.87204781157658e-05,
-0.0177931258240853, 0.016979617370892, 4, 3, 1, 4, 3, 1), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", src = "yahoo", updated = structure(1459599733.74441, class = c("POSIXct",
"POSIXt")), class = c("xts", "zoo"), index = structure(c(1458777600,
1459123200, 1459209600, 1459296000, 1459382400, 1459468800), tzone = "UTC", tclass = "Date"), .Dim = c(6L,
10L), .Dimnames = list(NULL, c("PCLN.Open", "PCLN.High", "PCLN.Low",
"PCLN.Close", "PCLN.Volume", "PCLN.Adjusted", "h", "l", "c",
"cluster")))
我试过以下方法:
# plot
chart_Series(s)
# add text on top of each bar:
text(x = s, y = s$cluster, label = as.character(s$cluster))
我曾尝试在线搜索,但似乎无法找到 chartSeries()
图的答案。提前致谢
在 text
调用中,x
应该是要绘制的文本位置的 x 坐标的数值向量,但您提供了 xts/matrix.我猜 text
调用使用第一列的前 6 个观察值,因为这是您绘制的图表中的观察值数量。前 6 个值介于 1278 和 1317 之间,即 >> 6,因此它们不会出现在您绘制的图上。
同样,y
应该是要绘制的文本位置的 y 坐标。您提供一个范围在 1 到 4 之间的向量,而您绘制的数据的 y 值在 1250-1350 范围内,因此您的文本坐标不在绘制的图上。
x 坐标应该只是 1:nrow(s)
,y 坐标应该接近 Lo(s)
和 Hi(s)
每次观察。例如:
text(x = seq(nrow(s)), y = Hi(s)+1, label = as.character(s$cluster))
我基本上想做的是在每个栏的顶部放置 s$cluster
个数字
library("quantmod")
# xts object
s <-
structure(c(1300, 1301.349976, 1281.199951, 1316.900024, 1312.310059,
1278, 1304.439941, 1304.709961, 1313.900024, 1323.089966, 1314.98999,
1301.719971, 1291.630005, 1271.01001, 1281.199951, 1311.040039,
1288.329956, 1273, 1301.189941, 1287.030029, 1307.890015, 1317.030029,
1288.959961, 1299.699951, 372300, 453800, 347600, 376400, 488200,
567300, 1301.189941, 1287.030029, 1307.890015, 1317.030029, 1288.959961,
1299.699951, 0.0034153392307692, 0.00258192266643587, 0.0255230051909361,
0.00470038870619693, 0.00204214772387123, 0.0185602276995305,
-0.00643845769230766, -0.0233142248891853, 0, -0.0044498328599013,
-0.0182731991083518, -0.00391236306729259, 0.000915339230769252,
-0.0110039169048249, 0.02083208321946, 9.87204781157658e-05,
-0.0177931258240853, 0.016979617370892, 4, 3, 1, 4, 3, 1), .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC", src = "yahoo", updated = structure(1459599733.74441, class = c("POSIXct",
"POSIXt")), class = c("xts", "zoo"), index = structure(c(1458777600,
1459123200, 1459209600, 1459296000, 1459382400, 1459468800), tzone = "UTC", tclass = "Date"), .Dim = c(6L,
10L), .Dimnames = list(NULL, c("PCLN.Open", "PCLN.High", "PCLN.Low",
"PCLN.Close", "PCLN.Volume", "PCLN.Adjusted", "h", "l", "c",
"cluster")))
我试过以下方法:
# plot
chart_Series(s)
# add text on top of each bar:
text(x = s, y = s$cluster, label = as.character(s$cluster))
我曾尝试在线搜索,但似乎无法找到 chartSeries()
图的答案。提前致谢
在 text
调用中,x
应该是要绘制的文本位置的 x 坐标的数值向量,但您提供了 xts/matrix.我猜 text
调用使用第一列的前 6 个观察值,因为这是您绘制的图表中的观察值数量。前 6 个值介于 1278 和 1317 之间,即 >> 6,因此它们不会出现在您绘制的图上。
同样,y
应该是要绘制的文本位置的 y 坐标。您提供一个范围在 1 到 4 之间的向量,而您绘制的数据的 y 值在 1250-1350 范围内,因此您的文本坐标不在绘制的图上。
x 坐标应该只是 1:nrow(s)
,y 坐标应该接近 Lo(s)
和 Hi(s)
每次观察。例如:
text(x = seq(nrow(s)), y = Hi(s)+1, label = as.character(s$cluster))