注释/标记图中的区域

时间:2018-05-09 20:01:18

标签: r plot polygon

我有以下图表:

Graph

图表(红色字母除外)由以下代码生成:

boundary12 <- c(0.01, 0.005, 0.002, 0.001)
boundary23 <- c(0.1, 0.05, 0.02, 0.01)
boundary34 <- c(0.5, 0.6, 0.7, 0.55)

plot(boundary12, type="lines", ylim=c(0,1), xlab="Pollution level", ylab="Probability of event")
lines(boundary23)
lines(boundary34)

问题:

1)如果我生成了很多图表,有没有办法半自动将红色字母放入?

2)是否有办法为区域着色,例如区域1是蓝色,区域2是黄色,区域3是绿色?

2 个答案:

答案 0 :(得分:2)

可以通过计算多边形质心自动放置注释。

plot(boundary12, type="n", ylim=c(0,1), xlab="Pollution level",
  ylab="Probability of event")

poly12 <- data.frame(x=c(1:4, 4:1), y=c(boundary12, rev(boundary23)))
poly23 <- data.frame(x=c(1:4, 4:1), y=c(boundary23, rev(boundary34)))
poly34 <- data.frame(x=c(1:4, 4:1), y=c(boundary34, rep(1, 4)))

polygon(poly12, border="#00000066", col="skyblue")
polygon(poly23, border="#00000066", col="gold")
polygon(poly34, border="#00000066", col="darkolivegreen3")

library(maps)
centroid <- maps:::centroid.polygon

p12 <- centroid(poly12)
p23 <- centroid(poly23)
p34 <- centroid(poly34)

textcoor <- rbind(p12, p23, p34)
textcoor[1, 1] <- textcoor[1 ,1] * 0.8 # to smooch nr1 a bit to the left

text(textcoor, labels=1:3, col="darkblue", cex=1.2)

enter image description here

答案 1 :(得分:1)

这是使用面函数执行填充的解决方案。这需要扩展为更通用的解决方案,以处理更多的点和可变的上限。

=IMPORTHTML("https://www.baseball-reference.com/teams/BOS/2018.shtml","table",10)