rggplot2bar-chartvisualization

How to narrow out a single barchart using ggplot in r


I am having some issues. I am trying to narrow out a single bar in this barchart but I was trying to avoid making a new dataframe/vector/object/etc.. If that's the only way to narrow out the bars then that is what I will do haha. I am new to graphing things in R so any help would be appreciated! :)

My data kind of looks like this:

CBCLedit <- data.frame(Timepoint=c("Baseline", "Exit","Baseline", "Exit",
                         "Baseline", "Exit","Baseline", "Exit",
                         "Baseline", "Exit","Baseline", "Exit",
                         "Baseline", "Exit","Baseline", "Exit",
                         "Baseline", "Exit","Baseline", "Exit",
                         "Baseline", "Exit","Baseline", "Exit"),
                       Range=c('Normal', 'Borderline', 'Normal','Clinical',
                               'Normal', 'Normal', 'Normal','Normal',
                               'Normal', 'Normal', 'Normal','Borderline',
                               'Normal', 'Clinical', 'Normal','Clinical',
                               'Normal', 'Clinical', 'Normal','Normal',
                               'Normal', 'Clinical', 'Normal','Normal'))

The output:

   Timepoint      Range
1   Baseline     Normal
2       Exit Borderline
3   Baseline     Normal
4       Exit   Clinical
5   Baseline     Normal
6       Exit     Normal
7   Baseline     Normal
8       Exit     Normal
9   Baseline     Normal
10      Exit     Normal
11  Baseline     Normal
12      Exit Borderline
13  Baseline     Normal
14      Exit   Clinical
15  Baseline     Normal
16      Exit   Clinical
17  Baseline     Normal
18      Exit   Clinical
19  Baseline     Normal
20      Exit     Normal
21  Baseline     Normal
22      Exit   Clinical
23  Baseline     Normal
24      Exit     Normal

And using ggplot, I used the following to make a graph:

CBCLedit %>% 
  ggplot(aes(Timepoint, ..count..)) + 
  geom_bar(aes(fill = Range), 
           position = "dodge",
           color = 'black',
           width = 0.5)+
  mdthemes::md_theme_classic() +
  xlab('Timepoint') +
  ylab('Frequency') +
  labs (
    title = '**Anxiety Range Score**',
    fill='Range Categories') +
  scale_fill_manual(values=c("#FF2327",
                             "#30B5DE",
                             "#83D72F"))+
  scale_y_continuous(expand = c(0,0),
                     limits = c(0,12),
                     breaks = c(0,6,12))

enter image description here

This is what is spit out. Is there any way to make the wide green bar as thin as the ones on the right? The original dataset and my r workspace is a bit hefty so I wanted to avoid adding in more data objects, but I definitely understand that that's what I might have to do. I am a newbie w visualization in r so I appreciate any feedback -thank you for reading!


Solution

  • You need to change your position argument - specifically position_dodge2 will give you the same size bars and plot baseline in the correct slot.

    CBCLedit %>% 
      ggplot(aes(Timepoint, ..count..)) + 
      geom_bar(aes(fill = Range), 
               position = position_dodge2(preserve = "single"),
               color = 'black',
               width = 0.5)+
      #mdthemes::md_theme_classic() +
      xlab('Timepoint') +
      ylab('Frequency') +
      labs (
        title = '**Anxiety Range Score**',
        fill='Range Categories') +
      scale_fill_manual(values=c("#FF2327",
                                 "#30B5DE",
                                 "#83D72F")) +
      scale_y_continuous(expand = c(0,0),
                         limits = c(0,12),
                         breaks = c(0,6,12))
    

    enter image description here