1

I´m trying to graph a series of 11 variables from 5 groups in a stacked barplot. This is my data and code

inf_source <- structure(list(inf_episode = c("First", "Second", 
                                     "Third", "Fourth", "Fifth"), Pneumonia = c(16,6,2,0,0), 
                        IAI = c(6,2,0,0,0), SSTI = c(7,0,2,0,0), Bacteremia = c(4,14,8,6,2),
                        Sinusitis =c(14,10,0,0,0), Other =c(8,7,7,4,0),
                        UTI=c(14,13,1,0,0), CDI=c(0,0,3,0,2), Dental=c(1,3,1,0,0), 
                        Colitis=c(2,2,1,0,0), CLABSI=c(0,0,3,0,0)), 
                   .Names = c("inf_episode", "Pneumonia", "IAI", "SSTI", 
                              "Bacteremia", "Sinusitis", "Other", "UTI", "CDI", "Dental", "Colitis", "CLABSI"), 
                   class = "data.frame", row.names = c(NA, 5L))

inf_source$inf_episode <- factor(inf_source$inf_episode,
                        levels = c("First", "Second", "Third", "Fourth", "Fifth"))

inf_source$inf_episode <- reorder(inf_source$inf_episode, rowSums(inf_source[-1]))

md <- melt(inf_source, id=(c("inf_episode")))

ggplot(data = md, aes(x = reorder(inf_episode, value, sum), y = value, fill = variable)) +
geom_col()

The graph I got is this:

enter image description here

The bars start with "Fifth" and end with "First", and I cannot change them to start with "First" and finish with "Fifth". Also, I would like to add the number of time each variable appears in every group, and if possible, the stack size to reflect the number. Any ideas?

I have tried reordering the groups but it´s not working.

neilfws
  • 32,751
  • 5
  • 50
  • 63
Sandra R
  • 11
  • 2
  • 2
    You are using this for your x: `reorder(inf_episode, value, sum)`. If you want them the other way, use the decreasing argument :`reorder(inf_episode, value, sum, decreasing = TRUE)`. Alternatively, order by negative value `reorder(inf_episode, -value, sum)` – Axeman May 30 '23 at 22:19
  • what do you mean with : the stack size to reflect the number. – user12256545 May 30 '23 at 22:54

2 Answers2

0

You're specifically reordering the factors such that Fifth is the first level. Try this instead. Making sure to set ordered = TRUE when calling factor().

You can add text to each bar using after_stat by applying a function used by stat. In this case summary(), then pass in the sum function to your aes call.

inf_source$inf_episode <- factor(inf_source$inf_episode, ordered = TRUE, levels = c("First", "Second", "Third", "Fourth", "Fifth"))

# inf_source$inf_episode <- reorder(inf_source$inf_episode, rowSums(inf_source[-1]))

md <- melt(inf_source, id=(c("inf_episode")))

ggplot(data = md, aes(x = inf_episode, y = value, fill = variable)) + 
  geom_col() +
  geom_text(
    aes(label = after_stat(y), group = inf_episode), 
    stat = 'summary', fun = sum, vjust = -1
  )

enter image description here

Jamie
  • 1,793
  • 6
  • 16
0

You can use factor with levels!

(I see i am late to the party: )

ggplot(data = md,
       aes(x = factor(inf_episode,level=c("First", "Second", "Third", "Fourth", "Fifth")),
           y = value,
           fill = variable))+
        geom_col()+
        geom_text(aes(label=value),
        position = position_stack(vjust = 0.5),check_overlap = T)

enter image description here

This is not a perfect annotation (0 values ) but should get you started.

user12256545
  • 2,755
  • 4
  • 14
  • 28