如何改进这个图表?

huangapple go评论84阅读模式
英文:

How can I make this plot better?

问题

以下是翻译好的部分:

"Has anyone get any advise on how to make this look plot better?"
有人获得了如何改善这个图表外观的建议吗?

"The barplot depicts the number and duration of Combined Sewer Overflow spills into a river."
这个条形图描述了污水管道溢出到河流中的数量和持续时间。

"It doesn't look too bad as it is but I was wondering how I could optimize 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-axis using:"
例如,我一直在尝试找出如何使用以下方式格式化Y轴:
expand_limits(x = 0, y = 0) + coord_cartesian(expand = FALSE)
但我无法使其起作用。

"Similarly, I've tried to label the axis values to correspond with values (0-1200 for Duration)"
类似地,我尝试过为轴线上的值添加标签,以与值(0-1200代表持续时间)相对应。
谢谢。

英文:

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.

答案1

得分: 0

你的问题非常开放。我不知道你确切想要什么,但我有一些建议。

首先,你可以使用 facet_wrap 按照 variable 分成两个面板。你还可以使用 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

结果:

如何改进这个图表?

你还可以使用 theme 自定义图表元素。

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

注意轴的颜色和大小,以及轴刻度的大小:

如何改进这个图表?

英文:

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 &lt;- ggplot(df1_melted, aes(x = category, y = value)) + 
  geom_bar(stat = &quot;identity&quot;, position = &quot;dodge&quot;) +
  facet_wrap(~variable)+
  labs(title = &quot;Number and Duration of CSO Spills into the Porter Brook&quot;,
       x = &quot;Year&quot;,
       y = &quot;Number of Spills&quot;) +
  theme(plot.title = element_text(hjust = 0.5, vjust = 0.5)) +
  geom_text(label = df1_melted$value, nudge_y = 30)

p

Result:

如何改进这个图表?

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 = &quot;grey&quot;))

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

如何改进这个图表?

huangapple
  • 本文由 发表于 2023年5月7日 03:10:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定