非常感謝您的幫助
這是我的資料:
| 樣品 | 小號 | 單身人士 | 雙胞胎 | 超1 | 杰克1 |
|---|---|---|---|---|---|
| 1 | 43.92 | 32.04 | 8.88 | 101.8 | 44.1 |
| 2 | 63.8 | 33.08 | 17.87 | 96.43 | 84.71 |
| 3 | 76.51 | 32.78 | 18.42 | 106.6 | 102.78 |
| 4 | 86.25 | 34.27 | 17.5 | 120.16 | 117.02 |
| 5 | 94.4 | 36.13 | 16.44 | 134.01 | 128.17 |
我正在嘗試使用上面顯示的資料更改表示物種積累曲線的圖形中的圖例順序。我為此使用的代碼是這樣的:
p2 <- ggplot(data = data, aes(x = Samples))
geom_point(aes(y = S.est.), shape = 22, fill = "turquoise")
geom_point(aes(y = Singletons.Mean), shape = 17, fill = "purple")
geom_point(aes(y = Doubletons.Mean))
geom_point(aes(y = Chao.1.Mean), shape = 21, fill = "red")
geom_point(aes(y = Jack.1.Mean))
geom_line(aes(y = S.est., colour = "S (est)"), size = 1)
geom_line(aes(y = Singletons.Mean, colour="Singletons"), size = 1)
geom_line(aes(y = Doubletons.Mean, colour="Doubletons"), size = 1)
geom_line(aes(y = Chao.1.Mean, colour = "Chao 1"), size = 1)
geom_line(aes(y = Jack.1.Mean, colour = "Jack 1"), size = 1)
labs(x = "Sampling days", y = "Number of species")
scale_x_continuous(limits = c(1, 12), breaks = c(0, 2, 4, 6, 8, 10, 12))
scale_y_continuous(limits = c(0, 200), breaks = c(0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200))
scale_color_manual(name = "Richness estimators ",
values=c("#CC0000","#006600", "#669999", "#00CCCC",
"#660099"))
theme_bw()
我嘗試使用 scale_color_manual、scale_fill_discrete 等許多命令來執行此操作,但沒有任何效果。它只是沒有改變什么。我想要的順序是 S、Singl、Doubl、Chao 和 Jack。

此外,任何可以幫助我使這張圖更美觀的建議都會受到好評。
非常感謝,晚安。
uj5u.com熱心網友回復:
對于問題的排序部分,典型的方法是重新塑造你的資料,并使系列成為一個有序的因素。
鑒于此資料:
my_data <- data.frame(
Samples = c(1L, 2L, 3L, 4L, 5L),
S = c(43.92, 63.8, 76.51, 86.25, 94.4),
Singletons = c(32.04, 33.08, 32.78, 34.27, 36.13),
Doubletons = c(8.88, 17.87, 18.42, 17.5, 16.44),
Chao1 = c(101.8, 96.43, 106.6, 120.16, 134.01),
Jack1 = c(44.1, 84.71, 102.78, 117.02, 128.17)
)
我們可以重塑資料,定義因子的順序,并繪制:
library(tidyverse)
my_data %>%
pivot_longer(-Samples, names_to = "estimator") %>%
mutate(estimator = factor(
estimator,
levels = c("S", "Singletons", "Doubletons", "Chao1", "Jack1"))) %>%
ggplot(aes(Samples, value, color = estimator))
geom_line()
theme_bw()

作為一個因素,如果我們愿意,我們可以進一步操縱估計器的順序。例如,%>% fct_rev在“估計器”公式的末尾添加將顛倒順序:

...或%>% fct_reorder(value) %>% fct_rev按價值順序獲取它們(默認情況下為中值):

uj5u.com熱心網友回復:
如果您堅持以寬格式進行(我真的建議按照@Jon Spring 的建議進行),那么我們必須進行更改scale_color_manual:
首先創建兩個向量,一個按你喜歡的順序寫上你的名字,然后是顏色,然后添加到scale_color_manual:
示例資料取自 Jon Spring 并稍作調整:
library(ggplot2)
my_colors <- c("#CC0000","#006600", "#669999", "#00CCCC", "#660099")
my_labels <- c("S", "Singletons", "Doubletons", "Chao1", "Jack1")
ggplot(data = data, aes(x = Samples))
geom_point(aes(y = S), shape = 22, fill = "turquoise")
geom_point(aes(y = Singletons), shape = 17, fill = "purple")
geom_point(aes(y = Doubletons))
geom_point(aes(y = Chao1), shape = 21, fill = "red")
geom_point(aes(y = Jack1))
geom_line(aes(y = S, colour = "S (est)"), size = 1)
geom_line(aes(y = Singletons, colour="Singletons"), size = 1)
geom_line(aes(y = Doubletons, colour="Doubletons"), size = 1)
geom_line(aes(y = Chao1, colour = "Chao 1"), size = 1)
geom_line(aes(y = Jack1, colour = "Jack 1"), size = 1)
labs(x = "Sampling days", y = "Number of species")
scale_x_continuous(limits = c(1, 6), breaks = c(0, 2, 4, 6))
scale_y_continuous(limits = c(0, 200), breaks = c(0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200))
scale_color_manual(name = "Richness estimators ",
labels = my_labels,
values= my_colors)
theme_bw()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/493095.html
上一篇:條形圖的不同列顏色
