R语言 ggplot2中因子的水平混乱

fdx2calv  于 2023-03-15  发布在  其他
关注(0)|答案(1)|浏览(122)

我正在构建一个图,其中显示了两个因子水平的平均响应,一个在三个水平,另一个在七个水平。但是,使用此代码:

library(dplyr)
library(ggplot2)
library(tidyr)
desfactorial %>%
  mutate(across(Solvent:Water_added, as.factor)) %>%
  pivot_longer(Solvent:Water_added) %>%
  mutate(value = factor(value, levels(factor(value))[
    c(10:12, 6, 9, 3, 5, 7:8, 1:2, 4)])) %>%
  ggplot(aes(value, PCR, group = name, color = name)) + 
  geom_point(stat = 'summary', fun = mean) +
  geom_errorbar(stat = 'summary', width = 0.1, alpha = 0.5) +
  geom_line(stat = 'summary', fun = mean) +
  geom_text(stat = 'summary', fun = mean, color = 'black',
            aes(label = after_stat(round(y, 2))),
            position = position_nudge(0.4, 0.01)) +
  facet_grid(~name, scales = 'free_x', switch = 'x') +
  scale_color_brewer(palette = 'Set1', guide = 'none') +
  coord_cartesian(clip = 'off') +
  geom_vline(data = data.frame(a = 0.4, name = 'Water_added'),
             aes(xintercept = a)) +
  theme_minimal(base_size = 20) +
  theme(strip.placement = 'outside',
        strip.background = element_blank(),
        axis.title.x = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.spacing.x = unit(0, 'mm'),
        axis.ticks = element_line(),
        axis.line.x = element_line())

我得到了这个图表:

我不知道为什么会出现一条垂直线,但最让我恼火的是water_added的级别没有按升序显示(0 - 3 - 6等)。此外,我想自定义x轴,但只是右边的部分。
以下是我的数据集:

desfactorial <- read.table(text="Solvent Water_added       PRY      CRY       PCR
1       Water           0  5.147654 11.56463 0.4451206
2       Water           0  5.242064 11.96381 0.4381602
3       Water           3  5.388539 12.21016 0.4413159
4       Water           3  5.542092 12.12285 0.4571607
5       Water           6  5.596923 12.15505 0.4604608
6       Water           6  5.337580 11.81707 0.4516839
7       Water          20  6.356056 12.12505 0.5242085
8       Water          20  6.113984 12.47696 0.4900217
9   ChCl:2Gly           0  5.627548 10.40986 0.5405979
10  ChCl:2Gly           0  5.558506 10.05448 0.5528388
11  ChCl:2Gly           3  6.568054 11.31326 0.5805625
12  ChCl:2Gly           3  6.713675 11.97304 0.5607327
13  ChCl:2Gly           6  6.825598 12.11337 0.5634763
14  ChCl:2Gly           6  7.041888 11.61301 0.6063791
15  ChCl:2Gly          20  7.903524 12.64268 0.6251462
16  ChCl:2Gly          20  8.249468 12.13060 0.6800542
17 ChCl:2Urea           0  7.221118 11.65261 0.6196993
18 ChCl:2Urea           0  6.841036 10.71166 0.6386534
19 ChCl:2Urea           3  6.988471 11.77654 0.5934233
20 ChCl:2Urea           3  7.292778 11.88234 0.6137494
21 ChCl:2Urea           6  8.809266 12.42277 0.7091226
22 ChCl:2Urea           6  8.927845 12.00003 0.7439853
23 ChCl:2Urea          20 10.011425 13.70436 0.7305283
24 ChCl:2Urea          20  9.407607 12.48654 0.7534196
25 ChCl:2Urea          45  8.323769 12.89661 0.6454228
26 ChCl:2Urea          45  8.520286 13.17925 0.6464923
27      Water          10  6.163627 12.38129 0.4978178
28      Water          10  6.226446 12.72711 0.4892268
29      Water          30  6.500778 11.83811 0.5491398
30      Water          30  6.347181 11.95258 0.5310303
31      Water          45  5.796056 12.29078 0.4715776
32      Water          45  6.262255 12.25520 0.5109877
33  ChCl:2Gly          10  6.936715 11.76254 0.5897291
34  ChCl:2Gly          10  7.871232 13.16848 0.5977327
35  ChCl:2Gly          30  7.516940 12.43334 0.6045792
36  ChCl:2Gly          30  8.069310 12.97053 0.6221263
37  ChCl:2Gly          45  8.423233 13.11659 0.6421818
38  ChCl:2Gly          45  8.229903 12.98646 0.6337295
39 ChCl:2Urea          10  8.954759 12.44479 0.7195587
40 ChCl:2Urea          10  8.353048 13.21604 0.6320385
41 ChCl:2Urea          30  8.243016 12.90292 0.6388490
42 ChCl:2Urea          30  8.281513 13.20763 0.6270248")
llew8vvj

llew8vvj1#

将每列转换为具有自己规则的因子可能会更容易。

desfactorial %>%
  mutate(
    Solvent=factor(Solvent, levels=c("Water", "ChCl:2Gly", "ChCl:2Urea")),
    Water_added = factor(Water_added, levels=unique(sort(Water_added)))
  ) %>% 
  pivot_longer(Solvent:Water_added) %>%
  ggplot(aes(value, PCR, group = name, color = name))  + 
   ...

相关问题