gpt4 book ai didi

r - 通过ggplot在密度图中添加部分数据

转载 作者:行者123 更新时间:2023-12-02 01:02:58 25 4
gpt4 key购买 nike

我有一个包含两个不同类别的文件,其中大部分属于一个类别。类别是:inout

file1_ggplot.txt

status scores
in 44
in 55
out 12
out 23
out 99
out 13

为了绘制密度分布图,我正在使用这段代码,但我想添加类别摘要和具有 in 的行:

library(data.table)
library(ggplot2)
library(plyr)
filenames <- list.files("./scores",pattern="*ggplot.txt", full.names=TRUE)
pdf("plot.pdf")
for(file in filenames){
library(tools)
bases <- file_path_sans_ext(file)
data1 <- fread(file)
cdat <- ddply(data1, "status", summarise, scores.mean=mean(scores))
data1ggplot <- ggplot(data1, aes(x=scores, colour=status)) + geom_density() + geom_vline(data=cdat, aes(xintercept=scores.mean, colour=status), linetype="dashed", size=1)
print(data1ggplot + ggtitle(basename(bases)))

}
dev.off()

哪个输出: ggplot for two categories

我想添加一个框,其中包含 in 行:

in     44
in 55

还有,

> summary(data1$scores)
Min. 1st Qu. Median Mean 3rd Qu. Max.
12.00 15.50 33.50 41.00 52.25 99.00

为此,我正在尝试使用 tableGrob:

data1ggplot <- ggplot(data1, aes(x=scores, colour=status)) + geom_density() + geom_vline(data=cdat, aes(xintercept=scores.mean, colour=status), linetype="dashed", size=1) +  annotation_custom(tableGrob(summary(data1$scores))

ggplot2.2

但它给出了上面相同的图,其中只有 summary 的数字。

然后,我用 in.

搜索了这些行
cat file1_ggplot.txt | grep -w "in" > only-in.txt

然后在 R 中:

data2<-fread("only-in.txt")

trs <- as.data.frame(t(data2))
trs
V1 V2
V1 in in
V2 44 55
data1ggplot <- ggplot(data1, aes(x=scores, colour=status)) + geom_density() + geom_vline(data=cdat, aes(xintercept=scores.mean, colour=status), linetype="dashed", size=1) + annotation_custom(tableGrob(trs))

它输出: ggplot2.3

如果不先在 bash 中使用 grep,我该怎么做才能在绘图旁边正确查看这些表格,以及带有 in 的行?

最佳答案

这是一个解决方案,假设您想要的表格格式:

enter image description here

个别情节

library(tidyverse)
library(gridExtra) # tableGrob
library(broom) # glance

df_summary <- t(broom::glance(summary(data1$scores)))
data1 %>%
ggplot(., aes(x = scores, colour = status)) +
geom_density() +
geom_vline(data = . %>%
group_by(status) %>%
summarise(scores.mean = mean(scores)),
aes(xintercept = scores.mean, colour = status),
linetype = "dashed",
size = 1) +
annotation_custom(tableGrob(rbind(data.frame(data1 %>% filter(status == "in") %>% rename(var = status, val = scores)),
data.frame(var = row.names(df_summary), val = df_summary, row.names = NULL)),
rows = NULL, cols = NULL),
xmin = 60, xmax = 100,
ymin = 0.1, ymax = 0.4)

应用于数据框列表

# Mock data
set.seed(1)
data_list = list(data1,
data.frame(status = data1$status, scores = c(40, 60, 15, 21, 97, 10)),
data.frame(status = data1$status, scores = c(45, 56, 11, 25, 95, 14)))

# Create a function

your_function <- function(df) {
df_summary <- t(broom::glance(summary(df$scores)))
df %>%
ggplot(., aes(x = scores, colour = status)) +
geom_density() +
geom_vline(data = . %>%
group_by(status) %>%
summarise(scores.mean = mean(scores)),
aes(xintercept = scores.mean, colour = status),
linetype = "dashed",
size = 1) +
annotation_custom(tableGrob(rbind(data.frame(df %>% filter(status == "in") %>% rename(var = status, val = scores)),
data.frame(var = row.names(df_summary), val = df_summary, row.names = NULL)), rows = NULL, cols = NULL),
xmin = 60, xmax = 100,
ymin = 0.1, ymax = 0.4)

}

# Check if it works
your_function(data_list[[2]])
your_function(data_list[[3]])

enter image description here enter image description here

# Map it
pdf("plot.pdf")
map(data_list, your_function)
dev.off()

您现在应该有一个“plot.pdf”文件,每个图有 3 页。

注意tableGrob的位置要根据你的日期来调整,我不知道把表格放在哪里,你也可以根据汇总值计算位置。

关于r - 通过ggplot在密度图中添加部分数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49295307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com