0

My data frame looks like this enter image description here

I would like to have all four columns have 4 bar for each tactics. IE Reconnaissance would have 4 bar one going at 76 one at 0 one at 0 and one at 2.

enter image description here

similarly to this.

I managed to make a one column data bar chart work sadly i can't find a way to add more data column to the graph not to mention i am trying to do it dynamically.

many thanks for reading i look forward to reading your advise and suggestion

It is important to keep in mind that column name will change and are created dynamically depending on the data inputted. Unlike in this question Creating grouped bar-plot of multi-column data in R.

kind regards,

I am expecting to have 4 bar for each x.

  • To make a barchart for all your columns you have to reshape your dataset using e.g. `tidyr::pivot_longer()`. See e.g. [Creating grouped bar-plot of multi-column data in R](https://stackoverflow.com/questions/10212106/creating-grouped-bar-plot-of-multi-column-data-in-r) – stefan Apr 19 '23 at 18:12

1 Answers1

0

Firstly let's switch that dataset so you can use variables as factors for x axis and fill color, then apply ggplot logic to build your char. Below

df = data.frame(tactics = as.factor(c("A","B","C")),
                    count = c(1,2,3),
                    abc1 = c(2,3,4),
                    abc2 = c(10,11,12),
                    abc3 = c(15,13,10))
library(ggplot2)
library(dplyr)
df %>% tidyr::pivot_longer(cols = abc1:abc3) %>%
      ggplot(aes(x = name, y = value, fill = tactics))+ geom_bar(stat = 'identity', position = 'dodge')

enter image description here

rkabuk
  • 267
  • 3
  • 15
  • note that i pivoted the data excluding count. that is a mistake. you should include yours – rkabuk Apr 19 '23 at 18:22
  • If you made a mistake in your answer, you should edit it to include the correction to provide a complete answer – jpsmith Apr 19 '23 at 18:31