-1

I want to make a grouped bar chart where each month is clustered together, then within that cluster each variable gets its own bar. So for the sample code below (my actual data has more than 2 rows) I want a bar for x1, for x2, for x3 and for x4 in January, then again a bar for x1, for x2, for x3 and for x4 in February, etc.

x1 x2 x3 x4 Month
7.8 3.4 9.8 2.1 Jan
5.5 2.3 6.1 0.7 Jan

I must be missing something fairly obvious but I can't figure out how to make each column a bar. This is my best guess so far,

Levels = c(x1, x2, x3, x4)
Plot <- ggplot(data, aes(x = Month, fill = Levels)) + geom_col(position = 'dodge')

which is obviously wrong because Levels isn't connected to the data in any way.

Any ideas would be appreciated.

  • 1
    Should the second row have Month = Feb? And have you done any searching? I'm guessing the number of prior questions regarding grouped bar graphs is huge. – IRTFM Dec 03 '21 at 22:15
  • No, there are multiple rows for each month. I have I haven't found any specifically where there are subgroups that are columns. – mulletjson Dec 03 '21 at 22:22
  • 1
    Probably because they were all told that the data should be made "long" rather than "wide". – IRTFM Dec 03 '21 at 22:25
  • There are so many ways you could have chosen to answer, I'm not sure why you chose to be condescending. I have not been told that data should be made "long" rather than "wide" so if you wouldn't mind explaining what you mean I would appreciate it. – mulletjson Dec 03 '21 at 22:33
  • 1
    I've chosen the way I responded because StackOverflow attempts to get new users to do searching before thay post questions. Duplicates of prior questions do not add to the value of the site. SO also expects prior effort at self-study. I'm pretty sure that any tutorial worth its salt on ggplot2 graphing startegies will start out by saying the data needs to be in dataframes that are arranged with the data in "long" configuration. It should explain what that means to persons who are not yet familiar with database terminology and the "first normal form". – IRTFM Dec 03 '21 at 22:46
  • Here's an example of what I think you might have found if you had spent time searching: https://stackoverflow.com/questions/48940156/ggplot-geom-bar-where-x-multiple-columns The search strategy was to use terms in your question title along with a sort by highest vote: [r] [ggplot2] "multiple columns" bar graph So now I'll follow through and close the question. BTW that was not my downvote so I'm not the only one who thought there was a problem with the question. – IRTFM Dec 03 '21 at 22:50
  • The link you posted does not help me. It also does not answer my question, most use facet wrap, which is not what I want and only one answer is a grouped bar chart, which I specifically asked for. It might be obvious to you how to apply that question to my problem but I am a beginner, it is not obvious to me that is why I asked my question. Your responses have made this an incredibly unpleasant experience and honestly make me want to give up instead of trying so thanks. – mulletjson Dec 03 '21 at 23:17
  • Perhaps you should describe in greater detail what you expect this chart to look like? You had an answer from TarJae that used two different months. But you apparently didn't like and asked for something that didn't require "a list of variables" which made no sense to me since he didn't have a list of variables. – IRTFM Dec 03 '21 at 23:49

1 Answers1

0
library(tidyverse)
df %>% 
  pivot_longer(
    -Month
  ) %>% 
  ggplot(aes(x = Month, y = value, fill=name)) + 
  geom_bar(position="dodge", stat="identity")

enter image description here

data:

df <- structure(list(x1 = c(7.8, 5.5, 4.8, 5.5), x2 = c(3.4, 2.3, 6.4, 
7.3), x3 = c(9.8, 6.1, 9.8, 3.1), x4 = c(2.1, 0.7, 2.1, 7.7), 
    Month = c("Jan", "Jan", "Feb", "Feb")), class = "data.frame", row.names = c(NA, 
-4L))
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • My actual dataset is much bigger than 2 rows, is there a way to generalize this so that I don't have to make a list of variables? – mulletjson Dec 03 '21 at 22:24
  • Just try with your dataset it should work, independent of the row count. I just added two more rows as an example. – TarJae Dec 03 '21 at 22:27