Home > database >  Migrate base R stacked bar plot to ggplot2 and distinguish bar levels with B/W structures instead of
Migrate base R stacked bar plot to ggplot2 and distinguish bar levels with B/W structures instead of

Time:11-21

I have a table like this:

p_well <- structure(c(0, 0, 0.45, 0, 0, 0, 68.1, 0, 0, 0, 0, 0.45, 0.23, 
                      0, 12.22, 0.23, 0, 0, 0, 0.23, 0.23, 1.36, 1.13, 0.23, 0, 0.23, 
                      0, 0.45, 0, 0, 0, 0.23, 0.23, 0, 0, 0.45, 0, 0.45, 0, 0.9, 0.68, 
                      0.9, 0, 1.13, 0, 0, 0, 0, 0, 0.9, 0, 0.23, 0.23, 0.23, 0, 0, 
                      0.45, 0.23, 0, 0.45, 0.23, 0, 0, 0, 0, 5.88, 0, 0.45, 0, 0, 0, 
                      0.23), class = "table", .Dim = 8:9, .Dimnames = structure(list(
                        c("adjective", "as_well", "dispreferred_marker", "manner_adverb", 
                          "quote_marker", "restart_marker", "turn_preface", "unclear"
                        ), c("W1", "W2", "W3", "W4", "W5", "W6", "W7", "W8", "W9"
                        )), .Names = c("", "")))

and want to draw a stacked bar plot in ggplot2. I know how to do it in base R, like so:

par(mar = c(4.4,4,3,1))
barplot(p_well, main = "Functions of 'well' by positions in turn", cex.main = 0.9,
        cex.axis = 0.8, cex.lab = 0.8, cex.names = 0.8,
        names.arg = colnames(p_well),
        xlab = "Positions", ylab = "%",
        col = c("red", "orange", "yellow", "green", "darkgreen", "lightblue", "blue"))
legend("topright", rownames(p_well), 
       fill =  c("red", "orange", "yellow", "green", "darkgreen", "lightblue", "blue"),
       bty = "n", cex = 0.8)

enter image description here

However, I would like to migrate the plot to ggplot2. Also, instead of colors to distinguish the different levels of the bars, I need to use black/white structures. How can that be done?

CodePudding user response:

I think this is might be what you're looking for

p_well <- structure(c(0, 0, 0.45, 0, 0, 0, 68.1, 0, 0, 0, 0, 0.45, 0.23, 
                        0, 12.22, 0.23, 0, 0, 0, 0.23, 0.23, 1.36, 1.13, 0.23, 0, 0.23, 
                        0, 0.45, 0, 0, 0, 0.23, 0.23, 0, 0, 0.45, 0, 0.45, 0, 0.9, 0.68, 
                        0.9, 0, 1.13, 0, 0, 0, 0, 0, 0.9, 0, 0.23, 0.23, 0.23, 0, 0, 
                        0.45, 0.23, 0, 0.45, 0.23, 0, 0, 0, 0, 5.88, 0, 0.45, 0, 0, 0, 
                        0.23), class = "table", .Dim = 8:9, .Dimnames = structure(list(
                          c("adjective", "as_well", "dispreferred_marker", "manner_adverb", 
                            "quote_marker", "restart_marker", "turn_preface", "unclear"
                          ), c("W1", "W2", "W3", "W4", "W5", "W6", "W7", "W8", "W9"
                          )), .Names = c("", "")))

p_well <- as.data.frame(p_well)

ggplot(p_well, aes(fill=Var1, x=Var2, y=Freq ))   
  geom_bar(position="stack", stat="identity")  
  xlab("Positions")  
  ylab("%")  
  theme_bw()  
  scale_fill_grey(start = 0, end = .9)  
  theme(legend.title=element_blank(),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        legend.position = c(.8,.7))

example

If you want to remove the gap from the bottom

ggplot(p_well, aes(fill=Var1, x=Var2, y=Freq ))   
  geom_bar(position="stack", stat="identity")  
  xlab("Positions")  
  ylab("%")  
  theme_bw()  
  scale_fill_grey(start = 0, end = .9)  
  scale_y_continuous(expand = c(0,0))  
  theme(legend.title=element_blank(),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        legend.position = c(.8,.7))

example2

You can also reverse the order using forcats::fct_rev() but you will also need to change the black and white colors from scale_fill_grey(start = 0, end = .9) to scale_fill_grey(start = .9, end = 0)

ggplot(p_well, aes(x=Var2, y=Freq,fill = forcats::fct_rev(Var1)))   
  geom_bar(position="stack", stat="identity")  
  xlab("Positions")  
  ylab("%")  
  theme_bw()  
  scale_fill_grey(start = .9, end = 0)  
  scale_y_continuous(expand = c(0,0))  
  theme(legend.title=element_blank(),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        legend.position = c(.8,.7))

example3

Do add patterns to the plot you can use ggpattern(). However, using a black and white scale and the size of some of the bars, it makes it difficult to tell the difference between the groups

library(ggpattern)
ggplot(p_well, aes(x=Var2, y=Freq))   
  geom_bar_pattern(position="stack",stat="identity",
                   mapping=aes(pattern=Var1))  
  xlab("Positions")  
  ylab("%")  
  theme_bw()  
  scale_fill_grey(start = .9, end = 0)  
  scale_y_continuous(expand = c(0,0))  
  theme(legend.title=element_blank(),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        legend.position = c(.8,.7)) 

example4

To have the color of the fill to vary in different shades of grey

ggplot(p_well, aes(x=Var2, y=Freq, fill=Var1))   
  geom_bar_pattern(position="stack",stat="identity",
                   mapping=aes(pattern=Var1))  
  xlab("Positions")  
  ylab("%")  
  theme_bw()  
  scale_fill_grey(start = .9, end = 0)  
  scale_y_continuous(expand = c(0,0))  
  theme(legend.title=element_blank(),
        axis.line = element_line(colour = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        panel.background = element_blank(),
        legend.position = c(.8,.7)) 

example5

  • Related