i'd like to choose colors and fill patterns for individual bars in a bar graph (Highlight a single "bar")
The bars, that i'd like to fill in another color are "Str" and "RB"
ggplot(GZ.mean, aes(x=Group.1, y=B)) +theme_bw(base_size=20, base_family="Times")+
geom_bar(stat="identity",colour="black", width=.6, position = "dodge", ,fill="gainsboro") +geom_errorbar(my.limits, width=0.2) +
theme(axis.text.x=element_text(family="Times", colour="black", size=rel(1.2), angle=30, hjust=1, vjust=1))+
theme(axis.text.y=element_text(family="Times", colour="black", size=rel(1.2))) + scale_y_continuous(limits=c(0,170))+geom_text(size=6,aes(label=c("a","d","c","e","b","d","d","b","bc","d", "bc"),hjust=offset.h, vjust=offset.v)) +
scale_x_discrete(limits=c("JdC", "Stu", "Str", "Bol", "WBr", "Rij4", "Bif", "ErL", "ZtG", "PdV", "RB")) +labs(x= "Variety", y= "Total Sugar concentration [mg * g-1 FW]")
i tried already with "scale_fill_manual" and "scale_color_manual" but it still doesn't work.
Highlighting specific bar in barplot To create manual color in bars we use the fill property of the ggplot2 plot with scale_fill_manual() function.
R barplot() – Set Colors for Bars in Bar Plot To set colors for bars in Bar Plot drawn using barplot() function, pass the required color value(s) for col parameter in the function call. col parameter can accept a single value for color, or a vector of color values to set color(s) for bars in the bar plot.
scale_fill_manual
should work. Create a column in your data that indicates whether the bar should be highlighted or not, then feed that column to the fill
aesthetic. scale_fill_manual
can then be used to assign colors as desired. Here's a representative example:
library( tidyverse )
library( ggplot2 )
## Generate some data to plot
X <- mtcars %>% group_by( cyl ) %>% summarize( mpg = mean(mpg) ) %>% ungroup
## Add a column indicating whether the category should be highlighted
X <- X %>% mutate( ToHighlight = ifelse( cyl == 6, "yes", "no" ) )
## Data looks like this
## A tibble: 3 x 3
## cyl mpg ToHighlight
## <dbl> <dbl> <chr>
##1 4 26.66364 no
##2 6 19.74286 yes
##3 8 15.10000 no
## Plot the data
ggplot( X, aes( x = cyl, y = mpg, fill = ToHighlight ) ) +
geom_bar( stat = "identity" ) +
scale_fill_manual( values = c( "yes"="tomato", "no"="gray" ), guide = FALSE )
Note guide = FALSE
that hides the legend associated with the ToHighlight
column.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With