'In a bar plot in R, how to color only a specified rightmost x% of a selected bar?

In an R barplot, I can color a selected bar; e.g., with code like the following:

data <- c(3,4,2,1)
colors <- rep("#ffffff", 4)
colors[2] <- "#ff0000"
barplot(data, col=colors, space=c(0,0,0,0))

Question: How can I color only a specified rightmost x% of a selected bar, for 0<x<100?

E.g., coloring only the rightmost 70% of the 2nd bar in the above example would look about like this:

enter image description here

(I expected there to be an already-answered question about this, or something like it -- but if there is any, I haven't been able to find it. My apologies if I've missed it.)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source