如何格式化组合柱状图和折线图

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

How to format combined bar and line chart

问题

我正在尝试生成一个组合的柱状图和折线图。我需要帮助正确格式化我的代码。我需要将第一个y轴标题更改为“银行倒闭数量”,将第二个y轴上的数字表达为$100,000,并调整y轴缩放,以使图表以可视友好的方式呈现,使柱形可见。

# 输入数据
year <- c("2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009",
          "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018",
          "2019", "2020", "2021", "2022", "2023")

failures <- c(4, 11, 3, 4, 0, 0, 3, 25, 140, 157, 92, 51, 24, 18, 8,
              5, 8, 0, 4, 4, 0, 0, 3)

assets <- c(2358.6, 2705.4, 1045.2, 163.1, 0, 0, 2602.5, 373588.8, 
            170909.4, 96514, 36012.2, 12055.8, 6101.7, 3088.4, 
            6727.5, 278.8, 6530.7, 0, 214.1, 458, 0, 0, 548500)

# 绘制图表并添加次要轴
library(ggplot2)
ggplot(banks, aes(x=year)) +  
  geom_col(aes(y = failures), size = 1, color = "dodgerblue4", fill = "white") +
  geom_line(aes(y = assets), size = 1.5, color = "darkorange2", group = 1) +
  scale_y_continuous(sec.axis = sec_axis(~./3, name = "总资产($M)"))
英文:

I am trying to produce a combined bar and line chart. I need assistance with properly formatting my code. I need to change the first y-axis title to "Number of Bank Failures", change the expression of the numbers on the second y-axis to read $100,000, and adjust the y scaling so that the graph renders in a visually friendly where the bars are visible.

# Entering data
year &lt;- c(&quot;2001&quot;, &quot;2002&quot;, &quot;2003&quot;, &quot;2004&quot;, &quot;2005&quot;, &quot;2006&quot;, &quot;2007&quot;, &quot;2008&quot;, &quot;2009&quot;,
      &quot;2010&quot;, &quot;2011&quot;, &quot;2012&quot;, &quot;2013&quot;, &quot;2014&quot;, &quot;2015&quot;, &quot;2016&quot;, &quot;2017&quot;, &quot;2018&quot;, 
      &quot;2019&quot;, &quot;2020&quot;, &quot;2021&quot;, &quot;2022&quot;, &quot;2023&quot;)

failures &lt;- c(4, 11, 3,	4,	0,	0,	3,	25,	140,	157,	92,	51,	24,	18,	8,
          5,	8,	0, 4,	4,	0,	0,	3)

assets &lt;- c(2358.6,	2705.4,	1045.2,	163.1,	0,	0,	2602.5,	373588.8,	
        170909.4,	96514,	36012.2,	12055.8,	6101.7,	3088.4,	
        6727.5,	278.8,	6530.7,	0,	214.1,	458,	0,	0,	548500)
# Plotting Charts and adding a secondary axis
library(ggplot2)
ggplot(banks, aes(x=year))+  
geom_col(aes(y = failures), size = 1, color = &quot;dodgerblue4&quot;, fill = &quot;white&quot;)+
geom_line(aes(y = assets), size = 1.5, color=&quot;darkorange2&quot;, group = 1)+
scale_y_continuous(sec.axis = sec_axis(~./3, name = &quot;Total Assets ($M)&quot;))`

答案1

得分: 1

你可以使用 aes()sec_axis() 手动设置第二个 y 轴。

编辑

  1. 你可以使用 theme(panel.grid.major.y) 添加水平线,或选择其他 ggplot2 主题。

  2. 关于图例,你应该在 aes() 中指定颜色,并使用 scale_color_manual()

year &lt;- c(&quot;2001&quot;, &quot;2002&quot;, &quot;2003&quot;, &quot;2004&quot;, &quot;2005&quot;, &quot;2006&quot;, &quot;2007&quot;, &quot;2008&quot;, &quot;2009&quot;,
          &quot;2010&quot;, &quot;2011&quot;, &quot;2012&quot;, &quot;2013&quot;, &quot;2014&quot;, &quot;2015&quot;, &quot;2016&quot;, &quot;2017&quot;, &quot;2018&quot;, 
          &quot;2019&quot;, &quot;2020&quot;, &quot;2021&quot;, &quot;2022&quot;, &quot;2023&quot;)

failures &lt;- c(4, 11, 3, 4,  0,  0,  3,  25, 140,    157,    92, 51, 24, 18, 8,
              5,    8,  0, 4,   4,  0,  0,  3)

assets &lt;- c(2358.6, 2705.4, 1045.2, 163.1,  0,  0,  2602.5, 373588.8,   
            170909.4,   96514,  36012.2,    12055.8,    6101.7, 3088.4, 
            6727.5, 278.8,  6530.7, 0,  214.1,  458,    0,  0,  548500)

banks &lt;- data.frame(year = year, failures = failures, assets = assets)

adjust &lt;- 2000 # 用于调整第二个 y 轴

# 绘制图表并添加第二个轴
library(ggplot2)
colors &lt;- c(&quot;Bank failure&quot; = &quot;dodgerblue4&quot;,
            &quot;Total assets&quot; = &quot;darkorange2&quot;) # 用于添加图例。
ggplot(banks, aes(x=year))+  
  geom_col(aes(y = failures, color=&quot;Bank failure&quot;), lwd = 1 , fill = &quot;white&quot;) +
  geom_line(aes(y = assets/adjust, color=&quot;Total assets&quot;), lwd = 1.5, group = 1)+
  scale_y_continuous(
    name = &quot;银行失败次数&quot;,
    breaks = seq(0,300,50),
    limits = c(0,300),
    sec.axis = sec_axis(~.*adjust, name = &quot;总资产(百万美元)&quot;,
                        labels = scales::dollar,
                        breaks= seq(0,600000,100000))) +
  theme_classic() +
  scale_color_manual(values=colors)+
  theme(axis.title.x=element_blank(),
        panel.grid.major.y = element_line(),
        legend.title = element_blank(),
        legend.position = &quot;top&quot;)

如何格式化组合柱状图和折线图<!-- -->

<sup>创建于2023-05-15,使用 reprex v2.0.2</sup>

英文:

You could manually set the second y-axis using aes(), and sec_axis().

Edit

  1. You could add horizontal lines with theme(panel.grid.major.y), or select another ggplot2 themes.

  2. When it comes to legend, you should specify colors inside of aes() along with scale_color_manual().

year &lt;- c(&quot;2001&quot;, &quot;2002&quot;, &quot;2003&quot;, &quot;2004&quot;, &quot;2005&quot;, &quot;2006&quot;, &quot;2007&quot;, &quot;2008&quot;, &quot;2009&quot;,
          &quot;2010&quot;, &quot;2011&quot;, &quot;2012&quot;, &quot;2013&quot;, &quot;2014&quot;, &quot;2015&quot;, &quot;2016&quot;, &quot;2017&quot;, &quot;2018&quot;, 
          &quot;2019&quot;, &quot;2020&quot;, &quot;2021&quot;, &quot;2022&quot;, &quot;2023&quot;)

failures &lt;- c(4, 11, 3, 4,  0,  0,  3,  25, 140,    157,    92, 51, 24, 18, 8,
              5,    8,  0, 4,   4,  0,  0,  3)

assets &lt;- c(2358.6, 2705.4, 1045.2, 163.1,  0,  0,  2602.5, 373588.8,   
            170909.4,   96514,  36012.2,    12055.8,    6101.7, 3088.4, 
            6727.5, 278.8,  6530.7, 0,  214.1,  458,    0,  0,  548500)

banks &lt;- data.frame(year = year, failures = failures, assets = assets)

adjust &lt;- 2000 # for adjusting second y-axis

# Plotting Charts and adding a secondary axis
library(ggplot2)
colors &lt;- c(&quot;Bank failure&quot; = &quot;dodgerblue4&quot;,
            &quot;Total assets&quot; = &quot;darkorange2&quot;) # For adding a legend.
ggplot(banks, aes(x=year))+  
  geom_col(aes(y = failures, color=&quot;Bank failure&quot;), lwd = 1 , fill = &quot;white&quot;) +
  geom_line(aes(y = assets/adjust, color=&quot;Total assets&quot;), lwd = 1.5, group = 1)+
  scale_y_continuous(
    name = &quot;Number of Bank Failures&quot;,
    breaks = seq(0,300,50),
    limits = c(0,300),
    sec.axis = sec_axis(~.*adjust, name = &quot;Total Assets ($M)&quot;,
                        labels = scales::dollar,
                        breaks= seq(0,600000,100000))) +
  theme_classic() +
  scale_color_manual(values=colors)+
  theme(axis.title.x=element_blank(),
        panel.grid.major.y = element_line(),
        legend.title = element_blank(),
        legend.position = &quot;top&quot;)

如何格式化组合柱状图和折线图<!-- -->

<sup>Created on 2023-05-15 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月15日 08:58:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250288.html
匿名

发表评论

匿名网友

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

确定