我试图在 R 语言的绘图中使用 3 种颜色标签,但我似乎无法做到。我已经尝试了这里提出的解决方案,似乎不适用于我的问题。这是生成我的绘图的代码:
pdf("report/500ERR_plots.pdf", width=10, height=7)
allDataTable_500$Date <- as.Date(allDataTable_500$Date)
totalTs <- allDataTable_500$Total
plot(allDataTable_500$Date, allDataTable_500$Total, xlab="Date", ylab="Total", las=2, xaxt="n", type="o", xlim=as.Date(c(allDataTable_500$Date[1], allDataTable_500$Date[16])), ylim=c(0, max(totalTs) + 200), cex = 0.8)
axis.Date(side = 1, at = as.Date(allDataTable_500$Date), format = "%d %b", las=2, cex = 0.1)
grid(NULL, NULL, col = "lightgray", lty = "dashed", lwd = par("lwd"), equilogs = TRUE)
title(main=paste("All 500 Errors from ", allDataTable_500$Date[1], " to ", allDataTable_500$Date[16], "nReport generated on ", allDataTable_500$Date[16], " at ", allDataTable_500$ProducedAt[1]), col.main="red", font.main=2)
text(x = allDataTable_500$Date, y = allDataTable_500$Total, label = paste(allDataTable_500$Total , "n", totalRequestDataTable$Total,"n(",round((allDataTable_500$Total / totalRequestDataTable$Total * 100), 3), "%)"), pos = 3, cex = 0.7, col = "blue")
以下是标签目前的外观:标签示例 我希望标签的第一行是红色的,第二行是蓝色的,第三行是黑色的,这可行吗?
PS:我已经尝试为 col 参数添加多种颜色,但 Rstudio 没有输出我想要的图形。这是在 col 参数中使用多种颜色的外观:MultipleColorPlot。例如,在此图上的峰值,我希望 184 为红色,452964为蓝色,(0.041%( 为黑色。
您可以将标签拆分为三组并添加 3 个标签并使用偏移量进行调整:
或者您也可以使用ggplot2
和ggrepel
,如果标签重叠,它们将调整标签。
allDataTable_500 <- data.frame(
Date = runif(10, 1, 100),
Total = runif(10, 100, 400)
)
plot(allDataTable_500$Date,
allDataTable_500$Total, xlab="Date", ylab="Total", las=2, xaxt="n", type="o",
cex = 0.8)
axis(side = 1, at = (allDataTable_500$Date), cex = 0.1)
grid(NULL, NULL, col = "lightgray", lty = "dashed", lwd = par("lwd"), equilogs = TRUE)
text(x = allDataTable_500$Date, y = allDataTable_500$Total,
label = paste(allDataTable_500$Total , "n"),
pos = 3, cex = 0.7,
col = c("black"))
text(x = allDataTable_500$Date, y = allDataTable_500$Total,
label = paste(allDataTable_500$Total , "n"),
pos = 3, cex = 0.7, offset = 1,
col = c("blue"))
text(x = allDataTable_500$Date, y = allDataTable_500$Total,
label = paste(allDataTable_500$Total , "n"),
pos = 3, cex = 0.7, offset = 1.5,
col = c("red"))