0

Has anyone get any advise on how to make this look plot better?

library(ggplot2)
data1  <- c(280, 577, 363, 307)
data2 <- c(584, 493, 1188, 1017)
category <- c("2019", "2020", "2021", "2022")

category <- factor( category, levels = category )

df1 <- data.frame(category, 
                  data1 = data1, 
                  data2 = data2)
library(reshape2)
df1_melted <- melt(df1, id.vars = "category")

p <- ggplot(df1_melted, aes(x = category, y = value, fill = variable)) + 
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Number and Duration of CSO Spills into the Porter Brook",
       x = "Year",
       y = "Number of Spills",
       fill = "Year") +
  theme(plot.title = element_text(hjust = 0.5, vjust = 0.5)) +
  scale_y_continuous(sec.axis = sec_axis(~ . + 0, name = "Duration (Hours)")) + 
 

  scale_fill_discrete(name = "", labels = c("Number of Spills", "Duration in Hours")) +
  theme(legend.position = "bottom")
p 

The barplot depicts the number and duration of Combined Sewer Overflow spills into a river. It doesn't look to bad as it is but I was wondering how I could optimise it? Such as formatting the axis to look better.

Any advice would be greatly appreciated. Thank you.

For example, I've been trying to figure out how to format the Y-axix using:

expand_limits(x = 0, y = 0) + coord_cartesian(expand = FALSE)

but I can't get it to work.

Similarly, I've tried to label the axis values to correspond with values (0-1200 for Duration)

Thank you.

Veg
  • 3
  • 1

1 Answers1

0

Your question is very open. I do not know exactly what you want, but I have some suggestions.

First, you can divide in two panels by the variable using facet_wrap. You can also put the values above the bars with geom_text.

p <- ggplot(df1_melted, aes(x = category, y = value)) + 
  geom_bar(stat = "identity", position = "dodge") +
  facet_wrap(~variable)+
  labs(title = "Number and Duration of CSO Spills into the Porter Brook",
       x = "Year",
       y = "Number of Spills") +
  theme(plot.title = element_text(hjust = 0.5, vjust = 0.5)) +
  geom_text(label = df1_melted$value, nudge_y = 30)

p

Result:

enter image description here

You can also customize chart elements using theme.

p + theme(axis.text = element_text(size = 12),
            axis.ticks = element_line(size = 1),
            axis.line = element_line(size = 0.8, color = "grey"))

Note the axis color and size and also the size of the axis ticks:

enter image description here

  • That's brilliant, thank you. The values on the bars is a nice touch. Much appreciated. – Veg May 06 '23 at 22:04
  • It was a pleasure helping you! If my answer was useful, please consider giving it an upvote. Thank you! – Marcio Cure May 07 '23 at 00:21
  • I tried to but I haven't get enough reputation apparently. – Veg May 07 '23 at 11:32
  • One thing though, how can I change the titles of the respective plots Data 1 and 2? – Veg May 07 '23 at 11:33
  • There is already a post about this question [here](https://stackoverflow.com/questions/48860158/changing-ggplot2facet-wrap-title-from-the-default). You have to add the `labeller` argument in the facet_wrap function. The new line would be: `facet_wrap(~variable, labeller = labeller(variable = c("data1" = "New name 1", "data2" = "New name 2")))`. – Marcio Cure May 07 '23 at 12:43
  • Apologies, I didn't realise that, thanks again for your help. – Veg May 07 '23 at 14:10