提取柱状图RGB
a = read.table('./pd.txt', header = T, sep = '\t')
# 安装和加载所需的包
# install.packages("ggplot2")
library(ggplot2)
a = a[a$M == 'surfavg',]
# 按照 FDR 值从小到大对数据框进行排序
sorted_a <- a[order(a$FDR), ]
# 创建柱状图,按照排序后的数据框绘制
p <- ggplot(sorted_a, aes(x = reorder(A, FDR), y = FDR, fill = FDR)) +
geom_bar(stat = 'identity', width = 0.3) +
scale_fill_gradient(low = 'red', high = 'gray') +
labs(x = NULL, y = "FDR", title = "Sorted Bar Chart")
# 展示柱状图
print(p)
p_data <- ggplot_build(p)$data[[1]]
# 打印每个柱子的填充颜色的 RGB 值和对应的 a 数据的 A 列
for (i in seq_along(p_data$fill)) {
rgb <- col2rgb(p_data$fill[i])
a_value <- sorted_a$A[i]
print(paste("RGB:", rgb, "A:", a_value))
}