【问题标题】:change to colour of polygon plot and add legend更改多边形图的颜色并添加图例
【发布时间】:2019-07-25 21:08:58
【问题描述】:

来自Assign colours to values and plot horizontal bars in R的后续问题

使用所有数据值,我如何更改为以下代码,以便 hmin2 条上的颜色编码切换。线的左侧应为蓝色,右侧应为红色。

FullRange = range(dataframe123, na.rm=TRUE)

BoxRanges = lapply(dataframe123, range, na.rm=TRUE)


plot(NULL, xlim=FullRange, ylim=c(0,3),  yaxt="n", xlab="Value", ylab="")
abline(v=19.293)
axis(2, at=(0:2)+0.4, labels=c("hmin1", "hmin2","hmin3"), 
lty=0, las=2)

for(i in 1:3) {
polygon(c(BoxRanges[[i]][1], BoxRanges[[i]][1], 19.293, 19.293), 
    c(i-1,i-0.2,i-0.2,i-1), col="red")
polygon(c(19.293, 19.293, BoxRanges[[i]][2], BoxRanges[[i]][2]), 
    c(i-1,i-0.2,i-0.2,i-1), col="blue")
}

此外,我如何在绘图中添加一个图例来指示颜色代表什么?

红色代表参数的负变化,蓝色代表参数的正变化。

卡了几个小时。任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: r plot colors range polygon


    【解决方案1】:

    一个选项可以是在循环外为hmin2 添加polygon。为了添加图例,您应该增加绘图区域(xlimylim),这样图例才会可见。

    plot(NULL, xlim=c(FullRange[1]-1, FullRange[2] +1), ylim=c(-1,4),  yaxt="n", xlab="Value", ylab="")
    abline(v=19.293)
    axis(2, at=(0:2)+0.4, labels=c("hmin1", "hmin2","hmin3"), 
         lty=0, las=2)
    
    for(i in c(1,3)) {
      polygon(c(BoxRanges[[i]][1], BoxRanges[[i]][1], 19.293, 19.293), 
              c(i-1,i-0.2,i-0.2,i-1), col="red")
      polygon(c(19.293, 19.293, BoxRanges[[i]][2], BoxRanges[[i]][2]), 
              c(i-1,i-0.2,i-0.2,i-1), col="blue")
    }
    i = 2
    polygon(c(BoxRanges[[i]][1], BoxRanges[[i]][1], 19.293, 19.293), 
            c(i-1,i-0.2,i-0.2,i-1), col="blue")
    polygon(c(19.293, 19.293, BoxRanges[[i]][2], BoxRanges[[i]][2]), 
            c(i-1,i-0.2,i-0.2,i-1), col="red")
    
    legend(x = 20, y = 4.5, legend = "negative changes",
         border = NULL, fill = "red",
         bty = "n",
         bg = "n")
    legend(x = 20, y = 4, legend = "positive changes",
           border = NULL, fill = "blue",
           bty = "n",
           bg = "n")
    

    编辑

    如果你想给你的情节添加一个标题,你必须在plot 中使用main。要为您的线路添加标签,您可以使用text

    plot(NULL, xlim=c(FullRange[1]-1, FullRange[2] +1), ylim=c(-1,4),  yaxt="n", xlab="Value", ylab="", main = "Range of H min values with parameter changes")
    abline(v=19.293)
    axis(2, at=(0:2)+0.4, labels=c("hmin1", "hmin2","hmin3"), 
         lty=0, las=2)
    
    for(i in c(1,3)) {
      polygon(c(BoxRanges[[i]][1], BoxRanges[[i]][1], 19.293, 19.293), 
              c(i-1,i-0.2,i-0.2,i-1), col="red")
      polygon(c(19.293, 19.293, BoxRanges[[i]][2], BoxRanges[[i]][2]), 
              c(i-1,i-0.2,i-0.2,i-1), col="blue")
    }
    i = 2
    polygon(c(BoxRanges[[i]][1], BoxRanges[[i]][1], 19.293, 19.293), 
            c(i-1,i-0.2,i-0.2,i-1), col="blue")
    polygon(c(19.293, 19.293, BoxRanges[[i]][2], BoxRanges[[i]][2]), 
            c(i-1,i-0.2,i-0.2,i-1), col="red")
    
    legend(x = 20, y = 4.5, legend = "negative changes",
           border = NULL, fill = "red",
           bty = "n",
           bg = "n")
    legend(x = 20, y = 4, legend = "positive changes",
           border = NULL, fill = "blue",
           bty = "n",
           bg = "n")
    text(x = 17.5, y = -0.9, labels ="19.293", col = "black", cex = 0.9)
    

    【讨论】:

    • 感谢您的回复!作为扩展,我可以问如何在该图中添加标题“H min values with parameter changes”并将垂直线标记为“19.293”吗?谢谢你。 @patL
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多