中美贸易2011、2012、2013、2014、2015、2016、2017、2018、2019、2020、2021年的进出口总额分别为:446582、484674、520749、555124、557023、519716、583676、633519、541560、586980、755776其中贸易顺差分别为:202324、218879、216064、237002、261405、250826、275
要使用RStudio制作详细的曲线图,可以按照以下步骤进行操作:
-
打开RStudio,创建一个新的R脚本。
-
将进出口总额和贸易顺差的数据输入到R脚本中,可以使用以下代码:
year <- c(2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021)
total <- c(4465.82, 4846.74, 5207.49, 5551.24, 5570.23, 5197.16, 5836.76, 6335.19, 5415.60, 5869.80, 7557.76)
surplus <- c(2023.24, 2188.79, 2160.64, 2370.02, 2614.05, 2508.26, 2757.85, 3232.73, 2957.68, 3164.78, 3963.74)
- 安装并加载必要的包,以绘制曲线图。
install.packages("ggplot2") # 如果之前没有安装ggplot2包,请先运行这行代码安装
library(ggplot2)
- 使用ggplot函数创建一个图形对象,并使用geom_line函数添加曲线。
df <- data.frame(year, total, surplus)
ggplot(df, aes(x = year)) +
geom_line(aes(y = total, color = "进出口总额")) +
geom_line(aes(y = surplus, color = "贸易顺差")) +
labs(x = "年份", y = "金额", color = "指标") +
scale_color_manual(values = c("进出口总额" = "blue", "贸易顺差" = "red")) +
theme_minimal()
- 运行代码,将生成的曲线图显示在RStudio的绘图窗口中。
这样,你就可以在RStudio中制作并查看中美贸易进出口总额和贸易顺差的详细曲线图了
原文地址: https://www.cveoy.top/t/topic/hDoU 著作权归作者所有。请勿转载和采集!