0

Given df with labelled column and row names,

      A   B
ANM  16  26
ANO  35  28
PRI   4  14
WHP   0   4
STY 127 474
SUN  30   3
BLD 153  29
TBS   1  96
UCH   1  12
SKA   0   3
IRF   1   3
DOV  28   2

I wish to make the below plot using ggplot2. (I've made the below plot in base R but am aware ggplot2 is the superior) grouped double bar plot base R

I can, of course, replicate the column and row names into the form noted in documentation here or is there a way to express this directly with the aes() function given my data frame?

M--
  • 25,431
  • 8
  • 61
  • 93

1 Answers1

1
library(dplyr)
# library(tidyr) # pivot_longer
library(ggplot2)
tibble::rownames_to_column(quux) %>%
  tidyr::pivot_longer(-rowname) %>%
  ggplot(aes(rowname, value, fill = name)) +
  geom_col(width = 0.5, position = position_dodge(width = 0.5)) +   
  theme_bw() +
  theme(panel.grid = element_blank())

basic ggplot barplot

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • There is way too much time to be "spent" on customizing this further, look at `?ggplot2::theme` for many of them, then https://r-graph-gallery.com/ for many examples of this and other plots. – r2evans Feb 07 '23 at 02:11
  • What is the `pivot_longer(-rowname)` doing? @r2evans – Chris Kouts Feb 07 '23 at 04:38
  • It reshapes the data from "wide" to "long", see https://stackoverflow.com/q/2185252/3358272, https://stackoverflow.com/q/68058000/3358272 – r2evans Feb 07 '23 at 12:56