Reputation: 729
Here is a reproducible sample data set that has a continuous fill based on Amount
. As you can see from the sample below, I also have a column in d
called flag
. What I want is to be able to visualize flag
by changing the border color of the bar to the color red.
# create reproducible data
library(ggplot2)
d <- read.csv(text='Day,Location,Length,Amount,flag
1,4,3,1.1,FALSE
1,3,1,2,FALSE
1,2,3,4,FALSE
1,1,3,5,FALSE
2,0,0,0,FALSE
3,3,3,1.8,TRUE
3,2,1,3.54,FALSE
3,1,3,1.1,FALSE',header=T)
ggplot(d, aes(x = Day, y = Length)) +
geom_bar(aes(fill = Amount, order = -Location), stat = "identity")
I have experimented with alpha
and that certainly does flag the desired bar but fails to visualize the data in the way I want:
ggplot(d, aes(x = Day, y = Length)) +
geom_bar(aes(fill = Amount, order = -Location, alpha = -flag), stat = "identity")
alpha
when using -flags
(note -
)also messes with the legend, which is undesirable.
Using the below command returns an error that I need 2 colour variables, but I only want 1, red
:
ggplot(d, aes(x = Day, y = Length)) +
geom_bar(aes(fill = Amount, order = -Location, alpha = -flag), stat = "identity") +
scale_colour_manual(values = alpha(c('red')))
To recap:
I want to show the bar where flag = TRUE
with a red outline (or something equally as noticeable) without changing the continuous scale already decided by Amount
.
I want the legend to reflect that flag = TRUE
with the colour red (or whatever matches).
I was not able to find an applicable solution perusing the web so any thoughts or suggestions are much appreciated!
Upvotes: 1
Views: 2389
Reputation: 56905
Use colour=flag
(colour
is the outline in a geom_bar
while fill is the fill). Then add + scale_colour_manual(values=c(NA, 'red'))
where NA
is used if flag
is false (no border) and 'red' is used if flag
is true.
ggplot(d, aes(x = Day, y = Length)) +
geom_bar(aes(fill = Amount, order = -Location, col=flag), stat = "identity") +
scale_colour_manual(values=c(NA, 'red'))
(Note: instead you could geom_bar(aes(...), col=ifelse(d$flag, 'red', NA))
and skip the scale_colour_manual
but then you don't get a legend).
If you want to increase the border width, add a lwd=<new line width>
in your geom_bar
(outside the aes
).
Upvotes: 3