我有一个包含两个不同类别的文件,其中大多数属于一个类别。类别包括:in
和out
。
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()
我想添加一个包含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))
但它给出了上面只有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))
如果没有先在in
中使用grep
,那么我可以做些什么才能在情节旁边正确看到这些表格以及bash
行?
答案 0 :(得分:2)
这是一个解决方案,假设您想要的表格格式:
个人情节
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]])
# Map it
pdf("plot.pdf")
map(data_list, your_function)
dev.off()
你现在应该有一个&#34; plot.pdf&#34;每个情节都有3页的文件。
请注意,您应根据日期调整tableGrob
的位置,我不知道放置桌子的位置,您还可以根据汇总值计算位置。