Reputation: 187
Follow up question from Assign colours to values and plot horizontal bars in R
with all the data values, how can i change to following code such that the colour coded on hmin2 bar switches. the left side of the line should be blue and the right side should be red.
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")
}
In addition, how do i add a legend to the plot indicating the what the colours represent?
Red represents a negative change in parameters and the blue represents a positive change in parameters.
Been stuck for hours. Any help will be appreciated thank you.
Upvotes: 0
Views: 307
Reputation: 2299
One option could be add polygon
for hmin2
outside the loop. In order to add the legend, you should increase the plot area (xlim
and ylim
), so the legend will be visible.
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")
If you want to add a title to your plot you must use main
inside plot
. To add a label to your line, you can use 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)
Upvotes: 1