英文:
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 <- 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)
# Plotting Charts and adding a secondary axis
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 = "Total Assets ($M)"))`
答案1
得分: 1
你可以使用 aes() 和 sec_axis() 手动设置第二个 y 轴。
编辑
- 
你可以使用
theme(panel.grid.major.y)添加水平线,或选择其他ggplot2主题。 - 
关于图例,你应该在
aes()中指定颜色,并使用scale_color_manual()。 
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)
banks <- data.frame(year = year, failures = failures, assets = assets)
adjust <- 2000 # 用于调整第二个 y 轴
# 绘制图表并添加第二个轴
library(ggplot2)
colors <- c("Bank failure" = "dodgerblue4",
            "Total assets" = "darkorange2") # 用于添加图例。
ggplot(banks, aes(x=year))+  
  geom_col(aes(y = failures, color="Bank failure"), lwd = 1 , fill = "white") +
  geom_line(aes(y = assets/adjust, color="Total assets"), lwd = 1.5, group = 1)+
  scale_y_continuous(
    name = "银行失败次数",
    breaks = seq(0,300,50),
    limits = c(0,300),
    sec.axis = sec_axis(~.*adjust, name = "总资产(百万美元)",
                        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 = "top")
<!-- -->
<sup>创建于2023-05-15,使用 reprex v2.0.2</sup>
英文:
You could manually set the second y-axis using aes(), and sec_axis().
Edit
- 
You could add horizontal lines with
theme(panel.grid.major.y), or select anotherggplot2themes. - 
When it comes to legend, you should specify colors inside of
aes()along withscale_color_manual(). 
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)
banks <- data.frame(year = year, failures = failures, assets = assets)
adjust <- 2000 # for adjusting second y-axis
# Plotting Charts and adding a secondary axis
library(ggplot2)
colors <- c("Bank failure" = "dodgerblue4",
            "Total assets" = "darkorange2") # For adding a legend.
ggplot(banks, aes(x=year))+  
  geom_col(aes(y = failures, color="Bank failure"), lwd = 1 , fill = "white") +
  geom_line(aes(y = assets/adjust, color="Total assets"), lwd = 1.5, group = 1)+
  scale_y_continuous(
    name = "Number of Bank Failures",
    breaks = seq(0,300,50),
    limits = c(0,300),
    sec.axis = sec_axis(~.*adjust, name = "Total Assets ($M)",
                        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 = "top")
<!-- -->
<sup>Created on 2023-05-15 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论