背景:我想制作一個 Shiny 應用程式,不使用 R 且未安裝它的同事可以將 .csv 檔案上傳到,然后使用他們的網路瀏覽器下載應用程式生成的報告。該報告將是一個可編輯的 Word 檔案,其中包括為提供的資料集中的每個不同組繪制的圖表(由特定列中的識別符號表示)。
同事將上傳一個或多個 .csv 檔案,這些檔案將包含具有相同列標題的多個資料集。理想情況下,這些將使用 rbind 或類似方法組合成單個資料幀。
然后,Shiny 應用程式將初始化 RMarkdown 報告,該報告將識別特定列中的所有不同識別符號,我想在 purrr/map 命令中使用這些識別符號按每個識別符號過濾資料集,并在 ggplot 中為每個識別符號繪制圖表。每個圖表上方還應該有一個標題,每個圖表下方應該有一個文本句子,這也需要由迭代函式產生。
我已經把到目前為止的內容放在下面(并且在此程序中嘗試了各種其他排列和命令)。我設法獲得了類似下面的函式來僅繪制圖形,但想要對其進行修改,以便在腳本移動到下一個專案,我正在努力解決這個問題。我也無法讓該函式與 ggplot 序列末尾的 annotate 和 xlim 命令一起使用,但如果它們也能正常作業,那將非常有幫助。
任何幫助或建議將不勝感激。太感謝了。
plot <- function(x) {
report.data %>%
filter(identifier == .data[[.x]]) %>%
ggplot(aes(x = sample.no, y = result))
geom_point(aes(colour = analyser))
geom_hline(aes(yintercept = mean(result) 2 * sd(result)), colour = "red", linetype = "dashed")
geom_hline(aes(yintercept = mean(result) - 2 * sd(result)), colour = "red", linetype = "dashed")
xlab("Sample number")
ylab("Result")
theme_classic() #
#annotate("text", x = max(sample.no) 2, y = mean(result), size = 3.5)
#xlim(0, max(sample.no) 2)
}
funs <- c(
header,
plot,
text
)
args <- list(unique(report.data$identifier))
report.data %>% map_df(~funs %>% map(exec, .x, !!!args))
生成示例輸入資料的代碼:
library(dplyr)
set.seed(1234)
test1.level1.analyser1 <- data.frame(
result = rnorm(25, mean = 2.5, sd = 0.2),
test = c("test1"),
level = c("level1"),
sample.no = c(1:25),
analyser = c("analyser1")
)
test1.level1.analyser2 <- data.frame(
result = rnorm(25, mean = 2.6, sd = 0.1),
test = c("test1"),
level = c("level1"),
sample.no = c(1:25),
analyser = c("analyser2")
)
test1 <- rbind(test1.level1.analyser1, test1.level1.analyser2)
test2.level1.analyser1 <- data.frame(
result = rnorm(25, mean = 10, sd = 2),
test = c("test2"),
level = c("level1"),
sample.no = c(1:25),
analyser = c("analyser1")
)
test2.level1.analyser2 <- data.frame(
result = rnorm(25, mean = 9.5, sd = 0.75),
test = c("test2"),
level = c("level1"),
sample.no = c(1:25),
analyser = c("analyser2"))
test2.level2.analyser1 <- data.frame(
result = rnorm(25, mean = 30, sd = 1.8),
test = c("test2"),
level = c("level2"),
sample.no = c(1:25),
analyser = c("analyser1")
)
test2.level2.analyser2 <- data.frame(
result = rnorm(25, mean = 9.5, sd = 0.75),
test = c("test2"),
level = c("level2"),
sample.no = c(1:25),
analyser = c("analyser2"))
test2.level1 <- rbind(test2.level1.analyser1, test2.level1.analyser2)
test2 <- rbind(test2.level1.analyser1, test2.level1.analyser2, test2.level2.analyser1, test2.level2.analyser2)
input.data <- rbind(test1, test2) %>% mutate(identifier = paste(test, level, sep = " "))
uj5u.com熱心網友回復:
是否可以將圖表上方的單個標題和圖表下方的句子直接集成到ggplot中?在這種情況下,我建議像這樣修改繪圖功能:
library(ggplot2)
library(dplyr)
library(purrr)
my_plot <- function(df) {
ggplot(df, aes(x = sample.no, y = result))
geom_point(aes(colour = analyser))
geom_hline(aes(yintercept = mean(result) 2 * sd(result)), colour = "red", linetype = "dashed")
geom_hline(aes(yintercept = mean(result) - 2 * sd(result)), colour = "red", linetype = "dashed")
theme_classic()
labs(
# the title above the plot, based on information in the filtered df
title = paste0("some title for identifier: ", unique(df$identifier)),
x = "Sample number",
y = "Result",
# the text below, based on data in the filtered data frame
caption = paste0("A short sentence about the mean result (", round(mean(df$result), 2), ") below the plot.")
)
coord_cartesian(xlim = c(0, max(df$sample.no) 2))
theme(
# configure the caption / sentence below
plot.caption=element_text(size=12, hjust = 0, margin = margin(t=20)),
# add some buffer at bottom as spacing between plots
plot.margin = margin(b=50)
)
}
plot_list <- purrr::map(unique(input.data$identifier),
function(x) {
# filter data before passing it to the plot function
input.data %>%
dplyr::filter(identifier == x) %>%
my_plot()
}
)
它會生成一個繪圖串列,然后可以像這樣列印在 Rmd 塊中。
```{r}
purrr::map(plot_list, ~plot(.x))

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/436077.html
下一篇:在R中更改聚類點標簽字體大小
