(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
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)
# 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?