中美贸易近10年的进出口总额分别为446582、484674、520749、555124、557023、519716、583676、633519、541560、586980、755776其中贸易顺差分别为:202324、218879、216064、237002、261405、250826、275785、323273、295768、316478、396374。用RStudio怎么做详细的曲线图?
要在RStudio中制作详细的曲线图,可以按照以下步骤进行操作:
-
打开RStudio,创建一个新的R脚本文件。
-
将给定的进出口总额和贸易顺差数据输入到R脚本中,可以使用以下代码:
year <- c(2010:2020)
total_trade <- c(4465.82, 4846.74, 5207.49, 5551.24, 5570.23, 5197.16, 5836.76, 6335.19, 5415.60, 5869.80, 7557.76)
trade_surplus <- c(2023.24, 2188.79, 2160.64, 2370.02, 2614.05, 2508.26, 2757.85, 3232.73, 2957.68, 3164.78, 3963.74)
- 使用以下代码绘制进出口总额的曲线图:
plot(year, total_trade, type = "o", col = "blue", xlab = "Year", ylab = "Total Trade", main = "Total Trade of China and US")
该代码使用plot函数绘制曲线图,其中type = "o"表示绘制连线和数据点,col = "blue"表示曲线的颜色为蓝色,xlab = "Year"和ylab = "Total Trade"分别表示x轴和y轴的标签,main = "Total Trade of China and US"表示图表的标题。
- 使用以下代码添加贸易顺差的曲线图:
lines(year, trade_surplus, type = "o", col = "red")
legend("topright", legend = c("Total Trade", "Trade Surplus"), col = c("blue", "red"), lty = 1)
该代码使用lines函数添加贸易顺差的曲线图,type = "o"表示绘制连线和数据点,col = "red"表示曲线的颜色为红色。legend函数用于添加图例,"topright"表示图例位于图表的右上角,legend = c("Total Trade", "Trade Surplus")表示图例的标签,col = c("blue", "red")表示图例的颜色,lty = 1表示图例的线型为实线。
- 运行代码,即可生成详细的曲线图。
完整的R代码如下:
year <- c(2010:2020)
total_trade <- c(4465.82, 4846.74, 5207.49, 5551.24, 5570.23, 5197.16, 5836.76, 6335.19, 5415.60, 5869.80, 7557.76)
trade_surplus <- c(2023.24, 2188.79, 2160.64, 2370.02, 2614.05, 2508.26, 2757.85, 3232.73, 2957.68, 3164.78, 3963.74)
plot(year, total_trade, type = "o", col = "blue", xlab = "Year", ylab = "Total Trade", main = "Total Trade of China and US")
lines(year, trade_surplus, type = "o", col = "red")
legend("topright", legend = c("Total Trade", "Trade Surplus"), col = c("blue", "red"), lty = 1)
希望这可以帮助到你
原文地址: https://www.cveoy.top/t/topic/hDoj 著作权归作者所有。请勿转载和采集!