R绘制股票日波动线图 中国海油600938

r 复制代码
# Set the working directory
setwd("C:/Users/czliu/Documents/python")

# Read the CSV file
df <- read.csv("stock_data_numeric.csv")
colnames(df)

# View the first few rows of the data
head(df)

# 处理缺失值
# df <- read.csv("data.csv", na.strings = c("NULL", "?"))  # 读取数据
df <- na.omit(df)  # 移除缺失值

# 转换日期列(确保列名正确,这里是小写的'date')
df$date <- as.Date(df$date)  # 假设原始日期列名为'date'

# 绘制线图
plot(df$close ~ df$date,
     type = "l",
     main = "Stock Price Daily Change",
     xlab = "Date",
     ylab = "Close Price",
     ylim = c(min(df$close), max(df$close))
)

# 添加多条水平刻度线--Advanced

# 1. 平均值 - 红色虚线
mean_price <- mean(df$close)
abline(h = mean_price, col = "red", lwd = 3, lty = 2)

# 2. 中位数 - 蓝色实线
median_price <- median(df$close)
abline(h = median_price, col = "blue", lwd = 3, lty = 1)


# 3. 上下四分位数 - 黑色点线
q1_price <- quantile(df$close, 0.25)
q3_price <- quantile(df$close, 0.75)
abline(h = q1_price, col = "black", lwd = 2, lty = 3)
abline(h = q3_price, col = "black", lwd = 2, lty = 3)

# 添加图例说明
legend("topright", 
       legend = c(paste("Mean: ", round(mean_price, 2)),
                 paste("Median: ", round(median_price, 2)),
                 paste("Q1: ", round(q1_price, 2)),
                 paste("Q3: ", round(q3_price, 2))),
       col = c("red", "blue", "black", "black"),
       lwd = c(2, 2, 1, 1),
       lty = c(2, 1, 3, 3),
       cex = 0.8)

图形

相关推荐
yiangL_1 天前
课后作业4:基于 Python 的古诗词词云可视化(P205)
开发语言·python
geovindu1 天前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
skywalk81631 天前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
敲上瘾1 天前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
听情歌落俗1 天前
嵌入式学习1-C语言基础语法与STM32实践
c语言·开发语言
小飞在努力1 天前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端
落羽的落羽1 天前
【C++】现代容器: tuple、optional、variant、any、string_view
linux·开发语言·数据结构·c++·人工智能·计算机网络·算法
小飞在努力2 天前
【实现负载均匀在线判题④】接入 cpp-httplib 实现编译服务 HTTP 网络化
c语言·开发语言·c++·笔记·http
炸薯条!2 天前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python
CappuccinoRose2 天前
前端开发交互体系 - 汇总篇
开发语言·前端·javascript·css·html·交互