R 逐ggplot在密度图中添加部分数据
我有一个文件有两个不同的类别,其中大多数都在一个类别中。这些类别是:R 逐ggplot在密度图中添加部分数据,r,ggplot2,density-plot,R,Ggplot2,Density Plot,我有一个文件有两个不同的类别,其中大多数都在一个类别中。这些类别是:in和out file1_ggplot.txt status scores in 44 in 55 out 12 out 23 out 99 out 13 为了绘制密度分布,我使用了这段代码,但我想添加一个类别的摘要以及在中带有has的行: library(data.table) library(ggplot2) library(plyr) filenames <- list.f
in
和out
file1_ggplot.txt
status scores
in 44
in 55
out 12
out 23
out 99
out 13
为了绘制密度分布,我使用了这段代码,但我想添加一个类别的摘要以及在中带有has的行:
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()
以及
为此,我尝试使用表grob
:
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))
然后在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))
data2这里有一个解决方案,假设您想要的表的格式:
单个绘图
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)
现在应该有一个“plot.pdf”文件,每个plot有3页
请注意,您应该根据您的日期调整表格GROB的位置,我不知道将表格放在哪里,您也可以根据汇总值计算位置。您能否提供一个可复制的示例,使用最小的数据集,请参见:但我已经做了..?数据集在这里,但这个例子目前还无法重现。顺便说一句,我需要对这里的文件名进行一些修改,我不清楚您想要的输出,一个在和摘要中包含的表,其中包含摘要的图例@bVa/scores
是您为ggplot
放置输入文件的目录(在本例中为file1\u ggplot.txt
)。是的!ggplot中的行在
中有,并且摘要
它抱怨此错误:attr(数据,“tsp”)中有错误问题可能与数据的格式有关。不幸的是,如果没有关于错误的更多信息(测试的代码、产生错误的行、使用的数据等等),就很难提供帮助。
cat file1_ggplot.txt | grep -w "in" > only-in.txt
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))
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()