ggplot clustered bar help

Hello, I was wondering if someone could help me with this. My current clustered chart ends up putting the months in order of August, June, May for each salinity treatment, but I need it to be in the order May, June, August. Does anyone know how to fix this?

Also, is it possible to add clustered bars to above the 0.2 PPT salinity? The proportion alive was 0 for each of those trials but it isn't showing it on the x-axis.

Any help is appreciated. Thank you!

WithinSQ_Proportion = read.csv("/Users/haley/documents/Thesis/WithinSQ_Proportion.csv")
WithinSQ_Proportion$Trial <- as.factor(WithinSQ_Proportion$Trial)
sq <- ggplot(WithinSQ_Proportion, aes(x = factor(Salinity), y = Proportion, fill = Trial)) + 
  geom_col(position = "dodge") + ggtitle("Proportion Alive at End of Experiment") +
  theme(plot.title = element_text(hjust = 0.5)) + 
  xlab("Salinity (PPT)") + ylab("Proportion Alive")

sq + scale_color_manual(values=c("#AA4499", "#DDCC77", "#117733")) + 
  scale_fill_manual(values=c("#AA4499", "#DDCC77", "#117733"))

Hi @hhagemei , for better help try to put a reproducible example of data.

Paste the result of:

dput(head(WithinSQ_Proportion,50))
#paste the result for get a example of his data

For order the Trial do it:

WithinSQ_Proportion$Trial<- factor(WithinSQ_Proportion$Trial, levels = c("May", "June", "August"))

# next make the plot
dput(head(WithinSQ_Proportion,50))
structure(list(Alive = c(0L, 13L, 19L, 21L, 0L, 12L, 16L, 16L, 
0L, 1L, 7L, 12L), Dead = c(26L, 9L, 3L, 5L, 22L, 9L, 5L, 5L, 
15L, 11L, 11L, 14L), Proportion = c(0, 0.590909091, 0.863636364, 
0.807692308, 0, 0.571428571, 0.761904762, 0.761904762, 0, 0.083333333, 
0.388888889, 0.461538462), Salinity = c(0.2, 1, 3, 10, 0.2, 1, 
3, 10, 0.2, 1, 3, 10), Trial = structure(c(3L, 3L, 3L, 3L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L), levels = c("August", "June", "May"
), class = "factor")), row.names = c(NA, 12L), class = "data.frame")

What do you mean with this?

Could make an example of manually draw the final plot that you want for better understand this, please.

Because you could add a horizontal lines if you want.

I mean show that there are zeros there rather than have it look like there weren't any replicates at all for that treatment. Essentially just show that the proportion was zero for each of the 3 trials for that one salinity rather than it look like there are no values at all. I am unsure if this makes sense or not. I apologize for the awful keyboard drawing.

A fast solution is put a small number when the Proportion was 0.0
Im put 0.01 when is 0.0.

Because if is 0.0 ggplot dont put a bars because is 0.

WithinSQ_Proportion <- structure(list(Alive = c(0L, 13L, 19L, 21L, 0L, 12L, 16L, 16L, 
                         0L, 1L, 7L, 12L), Dead = c(26L, 9L, 3L, 5L, 22L, 9L, 5L, 5L, 15L, 11L, 11L, 14L), 
               Proportion = c(0, 0.590909091, 0.863636364, 0.807692308, 0, 0.571428571, 0.761904762, 0.761904762, 0, 0.083333333, 
                                                                                        0.388888889, 0.461538462), 
               Salinity = c(0.2, 1, 3, 10, 0.2, 1,3, 10, 0.2, 1, 3, 10), 
               Trial = structure(c(3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), 
                                 levels = c("August", "June", "May"), class = "factor")),
          row.names = c(NA, 12L), class = "data.frame")

WithinSQ_Proportion$Trial<- factor(WithinSQ_Proportion$Trial, levels = c("May", "June", "August"))

WithinSQ_Proportion <- WithinSQ_Proportion %>%
  mutate(Proportion = case_when(
    Proportion == 0 ~ 0.01,  # maybe you coul put more close to 0.0 like 0.001
    TRUE ~ Proportion))

sq <- ggplot(WithinSQ_Proportion, aes(x = factor(Salinity), y = Proportion, fill = Trial)) + 
  geom_col(position = "dodge") + ggtitle("Proportion Alive at End of Experiment") +
  theme(plot.title = element_text(hjust = 0.5)) + 
  xlab("Salinity (PPT)") + ylab("Proportion Alive")

sq + scale_color_manual(values=c("#AA4499", "#DDCC77", "#117733")) + 
  scale_fill_manual(values=c("#AA4499", "#DDCC77", "#117733"))

Oh thank you so much! That is very helpful!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.