How to use different color value labels in stacked bar chart

2,033 views
Skip to first unread message

Greg Blevins

unread,
Oct 21, 2014, 11:58:45 AM10/21/14
to ggp...@googlegroups.com
Hello

The following code from Winston Chang's book, R Graphics Cookbook, generates a stacked bar chart with the values on each bar displayed in white text.

I was wondering if it is possible to get the labels in the lower three bars to display in black text while the upper three bars display values in white text?

I have searched the ggplot2 groups and stackoverflow but I have not found a solution and my experimentation has all failed.  

library( plyr) 
ce <- arrange( cabbage_exp, Date, Cultivar)
ce <- ddply( ce, "Date", transform, label_y = cumsum( Weight))

ggplot(ce, aes( x = Date, y = Weight, fill = Cultivar)) + 
geom_bar( stat ="identity") + 
geom_text( aes( y = label_y, label = Weight), vjust = 1.5, colour ="white")

Thank you,

Greg Blevins

Dennis Murphy

unread,
Oct 21, 2014, 2:54:21 PM10/21/14
to Greg Blevins, ggplot2
Hi:

There are a couple of ways you could go:

(1) Define a standalone vector of colors.
(2) Create a factor inside ce containing the vector of colors.

#-----------
library(gcookbook)
library(plyr)
library(ggplot2)

ce <- arrange( cabbage_exp, Date, Cultivar)
ce <- ddply( ce, "Date", transform, label_y = cumsum( Weight))

# (1)
cols <- gl(2, 1, length = 6, labels = c("black", "white"))

ggplot(ce, aes( x = Date, y = Weight, fill = Cultivar)) +
geom_bar( stat ="identity") +
geom_text( aes( y = label_y, label = Weight), vjust = 1.5, colour = cols)

# (2)
ce <- mutate(ce, cols = gl(2, 1, length = 6, labels = c("black", "white"))

ggplot(ce, aes( x = Date, y = Weight, fill = Cultivar)) +
geom_bar( stat ="identity") +
geom_text( aes( y = label_y, label = Weight, colour = cols), vjust = 1.5)
#-------------

In (2), cols is inside aes() because cols is a column of the input
data frame ce. In (1), it is a standalone vector that is not part of
ce so cannot be 'seen' by aes(); therefore, it must be defined outside
of aes().

Method 2 is safer, especially if this is to be reused. (1) is OK for
one-off use at the command line.

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Greg Blevins

unread,
Oct 21, 2014, 3:55:34 PM10/21/14
to ggp...@googlegroups.com
Thank you Joan and Dennis!  That was very helpful.

Dennis on your solution #2 I was not getting the desired outcome until I moved the ) to the right of cols back to just after Weight, as shown below.

   geom_text( aes( y = label_y, label = Weight, colour = cols), vjust = 1.5)

   geom_text( aes( y = label_y, label = Weight), colour = cols, vjust = 1.5)

Greg

JCMld

unread,
Oct 21, 2014, 4:48:01 PM10/21/14
to Greg Blevins, ggp...@googlegroups.com

Hi Greg,

 

Try this:

 

ggplot(ce, aes( x = Date, y = Weight, fill = Cultivar)) +

     geom_bar( stat ="identity") +

     geom_text( aes( y = label_y, label = Weight), vjust = 1.5, colour =ifelse(ce$Cultivar=="c52","white","black"))

 

Joan.

--

Reply all
Reply to author
Forward
0 new messages