【发布时间】:2021-09-28 13:09:36
【问题描述】:
我一直在尝试找到一种方法来绘制只有两列的数据框:一列用于值,另一列用于标签。然后,绘图可以有 3 种不同的颜色(每个标签一个颜色)。 这是我的数据框的一部分:
dN Label
0.0293 S
0.0273 S
0.0041 S
...
0.3070 E
0.3070 E
...
所以我使用这个数据框以单独的方式创建一个箱线图,我有 3 个与上面类似的数据框。然后我使用 multiplot 函数进行绘图:
multiplot(dN_plot, dS_plot, omega_plot, cols=3)
该情节很好,但是,我需要更改顺序并且需要使用其他功能。 因此,在上一篇文章中,另一位用户帮助我使用以下代码绘制了这个箱线图:
list(all_dN, all_dS, all_omega) %>%
set_names(c("S", "M", "E")) %>%
map_dfr(bind_rows, .id = "df") %>%
pivot_longer(-df) %>%
mutate(df = factor(df, unique(df))) %>%
ggplot() +
geom_boxplot(aes(x = name, y = value, color = "label"),
fill = "blue",
color = "blue",
alpha = 0.2,
notch = T,
notchwidth = 0.8) +
facet_wrap(~df, nrow = 1)
我知道上面的代码有效,因为我达到了用非常相似的数据框绘制数据的目标。 将此代码与我的新数据框一起使用的问题是此错误:
Error: Can't combine `dN` <double> and `label` <character>.
Run `rlang::last_error()` to see where the error occurred.
我想问题出在标签或数据框只有两列,也许吧? 我的问题是:有一种方法可以使用 list 函数修复该错误,还是我需要更改 set_names? 有什么建议吗? 如果您需要重现错误,这里是数据框的一部分:
all_dN:
dN label
1 0.0293 S
2 0.0273 S
3 0.0041 M
4 0.0273 M
5 0.0041 M
6 0.0000 M
7 0.0276 S
8 0.0042 S
9 0.0000 S
10 0.0000 S
11 0.0281 E
12 0.0056 E
13 0.0015 S
14 0.0015 S
15 0.0015 S
16 0.0274 S
17 0.0071 S
18 0.0064 S
...
all_dS:
dS label
1 0.0757 S
2 0.0745 M
3 0.0085 M
4 0.0745 M
5 0.0109 M
6 0.0024 M
7 0.0741 S
8 0.0086 S
9 0.0000 S
10 0.0024 S
11 0.0798 E
12 0.0109 E
13 0.0048 E
14 0.0073 E
15 0.0049 S
16 0.0810 S
17 0.0170 S
18 0.0183 S
...
all_omega:
Omega label
1 0.3872 S
2 0.3668 M
3 0.4851 E
4 0.3668 S
5 0.3767 S
6 -1.0000 E
7 0.3730 S
8 0.4847 S
9 -1.0000 S
10 -1.0000 E
11 0.3521 E
12 0.5141 E
13 0.3078 S
14 0.2049 S
15 0.3076 S
16 0.3379 S
17 0.4189 S
18 0.3482 M
【问题讨论】: