我有看起來像這樣的資料:

我想將具有相同名稱的列繪制在一起,例如:Enterobacteriaceaetreatment 1。
所以它看起來像這樣: x 行 - 將包含治療:治療 1_1 治療 1_2 等等。y 行將包含值。另外,我想添加中值和線性回歸線。
問題是我不斷收到錯誤訊息,因為有多個具有相同名稱的列,而 R 認為這是將多個具有相同名稱的列繪制在一起的問題。
我該怎么辦?我應該嘗試合并具有相同名稱的列嗎?
uj5u.com熱心網友回復:
為了單獨繪制每個組和列,您可以將每個組和列放入一個嵌套串列中,以便我們可以利用purrr函式。然后,ggplot為每個資料幀創建物件。
library(tidyverse)
library(ggpubr)
# First, split all columns into separate dataframes.
c_df <- df %>%
map(function(x)
as.data.frame(x)) %>%
# Then, you can bind the treatment column back to those dataframes.
map(function(x)
cbind(x, df$treatment)) %>%
# Remove "treatment" dataframe.
head(-1) %>%
# Then, split the original from treatment dataframes.
purrr::map(function(x)
split(x, f = str_detect(df$treatment, "treatment1")))
# Getting the names of the taxon (i.e., original column heading).
taxa_names <- names(c_df) %>%
rep(each = 2)
# Flatten list.
c_df <- c_df %>%
purrr::flatten() %>%
# Rename the 2 column names in all dataframes.
map( ~ .x %>%
dplyr::rename(value = "x", treatment = "df$treatment"))
# Replace the list names with the taxon names.
names(c_df) <- taxa_names
# Create a plotting function.
plot_treatment <- function(z, n) {
ggplot(data = z, aes(x = treatment, y = value))
geom_point()
theme_bw()
ggtitle(n)
}
# Use the plotting function to create all of the ggplot objects.
all_plots <- c_df %>%
purrr::map2(.y = names(c_df), .f = plot_treatment)
# Can plot in one figure.
ggarrange(all_plots[[1]],
all_plots[[2]],
all_plots[[3]],
all_plots[[4]],
ncol = 2,
nrow = 2)
輸出(示例)

資料
df <-
structure(
list(
Enterobacteriaceae = c(
0.60720596,
0.17991846,
0.76333618,
0.34825876,
0.60720596,
0.17991846,
0.76333618,
0.34825876
),
Enterobacteriaceae = c(
0.05291531,
0.38634377,
0.622598,
0.50749286,
0.05291531,
0.38634377,
0.622598,
0.50749286
),
Enterobacteriaceae = c(
0.3861723,
0.466643,
0.83439861,
0.99024876,
0.3861723,
0.466643,
0.83439861,
0.99024876
),
Methylococcaceae = c(
0.49516461,
0.16735156,
0.77037345,
0.50080786,
0.49516461,
0.16735156,
0.77037345,
0.50080786
),
Methylococcaceae = c(
0.18810595,
0.7514854,
0.05479668,
0.11263293,
0.18810595,
0.7514854,
0.05479668,
0.11263293
),
treatment = c(
"Original Sample1",
"Original Sample2",
"Original Sample3",
"Original Sample4",
"treatment1_1",
"treatment1_2",
"treatment1_3",
"treatment1_4"
)
),
class = "data.frame",
row.names = c(NA,-8L)
)
通常,使用ggplot,最容易將資料轉換為長格式,這樣您就可以按組繪圖。我創建了一些虛擬資料作為示例。我仍然不確定這是否是您正在尋找的輸出。
library(tidyverse)
df %>%
tidyr::pivot_longer(!treatment, names_to = "taxa", values_to = "value") %>%
# You can change this to "Original" to get the other plot.
dplyr::filter(str_detect(treatment, "treatment1")) %>%
ggplot(aes(x = treatment, y = value, color = taxa))
geom_point()
theme_bw()
輸出

資料
df <-
structure(
list(
Enterobacteriaceae = c(0.60720596, 0.17991846, 0.76333618, 0.34825876),
Enterobacteriaceae = c(0.05291531, 0.38634377, 0.622598, 0.50749286),
Enterobacteriaceae = c(0.3861723, 0.466643, 0.83439861, 0.99024876),
Methylococcaceae = c(0.49516461, 0.16735156, 0.77037345, 0.50080786),
Methylococcaceae = c(0.18810595, 0.7514854, 0.05479668, 0.11263293),
treatment = c(
"treatment1_1",
"treatment1_2",
"treatment1_3",
"treatment1_4"
)
),
class = "data.frame",
row.names = c(NA,-4L)
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/376608.html
下一篇:Android-自定義貨幣符號
