control color in stacked barplot; make it grayscale

1,907 views
Skip to first unread message

Jacob Wegelin

unread,
May 6, 2011, 1:14:14 PM5/6/11
to ggp...@googlegroups.com

A simple reproducible example is below these two questions.

(1) How could I with a single, simple command make everything in a particular plot black and white, or grayscale?

I do not want to set all the plots in the current R session to grayscale, just a particular plot.

(2) How could I assign three colors of my choice in a particular plot, something like the following base graphics command?

col=c("gray", "white", "gray")

Example: We want to make a stacked barplot from the following dataframe.

TINYDATA<-data.frame( diagnosis=c("A", "B", "C"), FREQ=c(5,9, 1))

require(ggplot2)

# The following makes a single black bar with the groups indistinguishable.
print( ggplot(data=TINYDATA, aes( x=0, y=FREQ, group=diagnosis)) + geom_bar())

# The following assigns a different, seemingly arbitrary color to each diagnosis group:
print( ggplot(data=TINYDATA, aes( x=0, y=FREQ, group=diagnosis, fill=diagnosis)) + geom_bar())

# Curiously, the following does not change the bars to black and white. Instead
# it eliminates the gray background of the grid inside the plot:
print( ggplot(data=TINYDATA, aes( x=0, y=FREQ, group=diagnosis, fill=diagnosis)) + geom_bar() + theme_bw() )

# The following also produces a brightly colored plot, surprisingly:
print(
ggplot(data=TINYDATA, aes( x=0, y=FREQ, group=diagnosis, fill=diagnosis)) + geom_bar()
+ scale_colour_manual(values=c("gray", "black", "white"))
)

# The following makes a two-color plot and labels these colors "black" and "white" in a legend:
print( ggplot(data=TINYDATA, aes( x=0, y=FREQ, group=diagnosis, fill=c("black", "white", "black"))) + geom_bar() )

There must be a simple solution?

The environment in which I tried this code:

> sessionInfo()
R version 2.12.2 (2011-02-25)
Platform: i386-apple-darwin9.8.0/i386 (32-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] splines grid stats graphics grDevices utils datasets
methods base

other attached packages:
[1] Hmisc_3.8-3 survival_2.36-5 gridExtra_0.8 ggplot2_0.8.8
proto_0.3-8 reshape_0.8.3 plyr_1.2.1

loaded via a namespace (and not attached):
[1] boot_1.2-43 cluster_1.13.3 digest_0.4.2 lattice_0.19-17
tools_2.12.2
>

Thanks for any info

Jacob Wegelin

Jacob Wegelin

unread,
May 6, 2011, 1:29:00 PM5/6/11
to ggp...@googlegroups.com

This is my email of a few minutes ago with the addition of a base graphics approach for comparison.

A simple reproducible example is below these two questions.

(1) How could I with a single, simple command make everything in a particular plot black and white, or grayscale?

I do not want to set all the plots in the current R session to grayscale, just a particular plot.

(2) How could I assign three colors of my choice in a particular plot, something like the following base graphics command?

col=c("gray", "white", "gray")

Example: We want to make a stacked barplot from the following dataframe.

TINYDATA<-data.frame( diagnosis=c("A", "B", "C"), FREQ=c(5,9, 1))

# Here is a base graphics approach, although without a legend to
# identify the diagnostic groups.

TINYDATA$mycolor<-c("gray", "white", "gray")
barplot( as.matrix(TINYDATA[,"FREQ", drop=F]) , beside=F, col=TINYDATA$mycolor)

David Kahle

unread,
May 6, 2011, 1:29:29 PM5/6/11
to Jacob Wegelin, ggp...@googlegroups.com
Hi Jacob -

# The following makes a single black bar with the groups indistinguishable.
print( ggplot(data=TINYDATA, aes( x=0,  y=FREQ, group=diagnosis)) + geom_bar())

# The following assigns a different, seemingly arbitrary color to each diagnosis group:
print( ggplot(data=TINYDATA, aes( x=0,  y=FREQ, group=diagnosis, fill=diagnosis)) + geom_bar())

These are the default colors in a categorical color scale.

# Curiously, the following does not change the bars to black and white. Instead
# it eliminates the gray background of the grid inside the plot:
print( ggplot(data=TINYDATA, aes( x=0,  y=FREQ, group=diagnosis, fill=diagnosis)) + geom_bar() + theme_bw()  )

Here you have changed the theme of the plot, not the plot itself.  The theme deals with thing like background color, font size, etc, but not plot elements themselves.

# The following also produces a brightly colored plot, surprisingly:
print(
ggplot(data=TINYDATA, aes( x=0,  y=FREQ, group=diagnosis, fill=diagnosis)) + geom_bar()
+ scale_colour_manual(values=c("gray", "black", "white"))
)

You are attempting to change the colour aesthetic with the scale_ command, not the fill aesthetic...

# The following makes a two-color plot and labels these colors "black" and "white" in a legend:
print( ggplot(data=TINYDATA, aes( x=0,  y=FREQ, group=diagnosis, fill=c("black", "white", "black"))) + geom_bar() )

It thinks that "black" and "white" are levels of a categorical variable, not colors.

There must be a simple solution?

ggplot(aes(x = 0, y = FREQ, group = diagnosis, fill = diagnosis), data = TINYDATA) +
  geom_bar() +
  scale_fill_grey()

And for colors of your choice...

ggplot(aes(x = 0, y = FREQ, group = diagnosis, fill = diagnosis), data = TINYDATA) +
  geom_bar() +
  scale_fill_manual(values = c('A' = 'blue', 'B' = 'red', 'C' = 'green'))  

Hope it helps 
david.
Reply all
Reply to author
Forward
0 new messages