rnso
rnso

Reputation: 24555

black and white (preferably with pattern) barchart using ggplot2 in R

I am using following data and code to make a grouped barchart in ggplot2:

mm = structure(list(name = c("A", "B", "C", "D", "A", "B", "C", "D", 
"A", "B", "C", "D"), variable = structure(c(1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("one", "two", "three"
), class = "factor"), value = c(1L, 5L, 6L, 9L, 5L, 7L, 8L, 9L, 
15L, 14L, 10L, 9L)), row.names = c(NA, -12L), .Names = c("name", 
"variable", "value"), class = "data.frame")

mm
   name variable value
1     A      one     1
2     B      one     5
3     C      one     6
4     D      one     9
5     A      two     5
6     B      two     7
7     C      two     8
8     D      two     9
9     A    three    15
10    B    three    14
11    C    three    10
12    D    three     9

ggplot(mm) + geom_bar(aes(x=name, y=value, fill=variable), stat='identity', position="dodge")

enter image description here

However, it is colored and I need black and white plot (with either grey or shaded bars). I tried following but was not successful:

ggplot(mm) + geom_bar(aes(x=name, y=value, fill=variable), stat='identity', position="dodge")+theme_classic()

ggplot(mm) + geom_bar(aes(x=name, y=value, fill=variable), stat='identity', position="dodge")+theme_bw()

ggplot(mm) + geom_bar(aes(x=name, y=value, group=variable), stat='identity', position="dodge")

How can I get black and white (grey or preferably patterned with different styles as in: http://support.sas.com/kb/45/663.html (click on results tab)) barchart with ggplot? Thanks for your help.

Upvotes: 2

Views: 3214

Answers (1)

jazzurro
jazzurro

Reputation: 23574

if you want to have mesh effects on bars, that seems to be really challenging. I have not personally seen ggplots figures with mesh effects on bars. The only thing I could think was to draw lines on top of bars using geom_segment(). I did not create mesh bars, but I created a bar with vertical lines. Since I had to draw so many lines, I just wanted to show how much work one would have in this way. It would be great if we can use mesh bar chart with ggplot. If there is no such thing, I would try to deliver same/similar visual effects in different ways.

ana <- ggplot(mm,aes(x=name, y=value, fill = variable)) +
    geom_bar(stat='identity', position="dodge") +
    scale_fill_manual(values=c("grey", "grey", "grey")) +
    theme_bw() +
    geom_segment(aes(x = 1.85, y = 0, xend = 1.85, yend = 7)) +
    geom_segment(aes(x = 1.9, y = 0, xend = 1.9, yend = 7)) +
    geom_segment(aes(x = 1.95, y = 0, xend = 1.95, yend = 7)) +
    geom_segment(aes(x = 2, y = 0, xend = 2, yend = 7)) +
    geom_segment(aes(x = 2.05, y = 0, xend = 2.05, yend = 7)) +
    geom_segment(aes(x = 2.1, y = 0, xend = 2.1, yend = 7)) +
    geom_segment(aes(x = 2.15, y = 0, xend = 2.15, yend = 7)) 
ana

enter image description here

Upvotes: 1

Related Questions