Reputation: 29
Im trying to have a 3 colors labels in my plot in R language, but i dont seems to be able to. I have tried the solution proposed here, dont seems to work for my problem. Here is the code to generate my plot:
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")
Here is how the labels looks at the moment: labelExample I want the first line of the label to be red, the second to be blue and the third one to be black, is this feasable?
PS: I've already tried to add multiple colors to the col argument but Rstudio does not output the graph that i want. Here what it look like with multiple color in the col argument: MultipleColorPlot. For example, where the peak is on this graph i would like the 184 to be red, the 452964 to be blue and the (0.041%) to be black.
Upvotes: 0
Views: 373
Reputation: 9819
You could split the labels in the three groups and add 3 labels and adjust them with an offset:
Or you could also use ggplot2
and ggrepel
, which will adjust the labels if they overlap.
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"))
Upvotes: 1