只重新排列 ggplot 中的 2 个 x 轴刻度标签,其余的按因子顺序排列。

huangapple go评论128阅读模式
英文:

Reordering only 2 x-tick labels in a ggplot, while the rest are ordered by being factors

问题

Sure, here is the translated code section:

sum_tab_cond_Loc <- sum_tabnew[!condition == "empty",
                               list(n_samples=as.double(.N),
                                    sum_dark=as.double(sum(pixel_diff_by_sec)),
                                    mean=as.double(mean(pixel_diff_by_sec)),
                                    median=as.double(median(pixel_diff_by_sec)),
                                    sd=sd(pixel_diff_by_sec),
                                    sem=std.error(pixel_diff_by_sec)),
                               by=list(condition, loc_coord, loc_id)]

sum_tab_cond_Loc$condition <- factor(sum_tab_cond_Loc$condition,
                            levels = gtools::mixedsort(unique(sum_tab_cond_Loc$condition)))

p_mean_condition_time <- ggplot((sum_tab_cond_Loc), aes(x=condition, y=mean, color=condition, fill=condition, group=condition)) +
  geom_line(lwd=1) + 
  geom_point(size=5,shape=22, stroke=1.4, color="black") +
  labs(title=" ", x='Conditions', y = "Means of Pixel Differences") + 
  theme_classic(base_size = 20)+
  geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem), width=0.1, position=position_dodge(width=0.9))

I've removed the HTML encoding (e.g., &lt; and &quot;) and translated it to plain text.

英文:

Here is my data for this question:

&gt; sum_tab_cond_Loc
    condition loc_coord loc_id n_samples sum_dark         mean median          sd
 1:    WT_CTL        a1      1        89    14816 166.47191011      0 381.5107853
 2:     MUT10        a2      2        89     5748  64.58426966      0 156.9145547
 3:      MUT9        a3      3        89        0   0.00000000      0   0.0000000
 4:      MUT8        a4      4        89        0   0.00000000      0   0.0000000
 5:      MUT7        a5      5        89     1065  11.96629213      0 112.8897742
 6:      MUT6        a6      6        89        2   0.02247191      0   0.2119996
 7:      MUT5        a7      7        89    23891 268.43820225     85 409.9059469
 8:      MUT4        a8      8        89     4691  52.70786517      0 197.9350331
 9:      MUT3        a9      9        89    17244 193.75280899     37 330.9582259
10:      MUT2       a10     10        89    19653 220.82022472     84 294.4198286
11:      MUT1       a11     11        89    32244 362.29213483    187 390.1419090
12:   MUT_CTL       a12     12        89    20547 230.86516854    144 284.6004053
13:    WT_CTL        b1     13        89        9   0.10112360      0   0.6224027
14:     MUT10        b2     14        89      934  10.49438202      0  59.0065720
15:      MUT9        b3     15        89     2591  29.11235955      0 184.8712966
16:      MUT8        b4     16        89        5   0.05617978      0   0.5299989
17:      MUT7        b5     17        89     3146  35.34831461      0  95.4808526
18:      MUT6        b6     18        89        0   0.00000000      0   0.0000000
19:      MUT5        b7     19        89    12080 135.73033708      0 379.4003571
20:      MUT4        b8     20        89      855   9.60674157      0  78.7162075
21:      MUT3        b9     21        89    27055 303.98876404    140 412.2091949
22:      MUT2       b10     22        89    29327 329.51685393    225 348.2187176
23:      MUT1       b11     23        89    31765 356.91011236    186 416.8646674
24:   MUT_CTL       b12     24        89    22682 254.85393258    161 318.7144116
25:    WT_CTL        c1     25        89    16498 185.37078652    117 245.1523822
26:     MUT10        c2     26        89     1169  13.13483146      0  91.5546522
27:      MUT9        c3     27        89     6932  77.88764045      0 247.3136782
28:      MUT8        c4     28        89      228   2.56179775      0  23.6359487
29:      MUT7        c5     29        89      624   7.01123596      0  43.9693836
30:      MUT6        c6     30        89     5643  63.40449438      0 277.5699224
31:      MUT5        c7     31        89    19713 221.49438202     93 320.8856750
32:      MUT4        c8     32        89       26   0.29213483      0   1.4632020
33:      MUT3        c9     33        89    13328 149.75280899     21 290.6393141
34:      MUT2       c10     34        89    16007 179.85393258     73 248.5571797
35:      MUT1       c11     35        89    19859 223.13483146     94 315.2131382
36:   MUT_CTL       c12     36        89    38148 428.62921348    316 417.0589500
37:    WT_CTL        d1     37        89      692   7.77528090      0  61.8612364
38:     MUT10        d2     38        89     5397  60.64044944      0 268.0002649
39:      MUT9        d3     39        89       16   0.17977528      0   0.8469437
40:      MUT8        d4     40        89        0   0.00000000      0   0.0000000
41:      MUT7        d5     41        89     1958  22.00000000      0 129.0508180
42:      MUT6        d6     42        89    13699 153.92134831      0 318.4321758
43:      MUT5        d7     43        89     3734  41.95505618      0 119.0242441
44:      MUT4        d8     44        89       19   0.21348315      0   1.7219961
45:      MUT3        d9     45        89    19630 220.56179775     49 322.1956265
46:      MUT2       d10     46        89    26129 293.58426966    156 349.9139767
47:      MUT1       d11     47        89    46637 524.01123596    412 571.9543945
48:   MUT_CTL       d12     48        89    36407 409.06741573    235 469.2885524
49:    WT_CTL        e1     49        89    14722 165.41573034      0 295.4558423
50:     MUT10        e2     50        89     4546  51.07865169      0 231.8792899
51:      MUT9        e3     51        89      542   6.08988764      0  57.2378690
52:      MUT8        e4     52        89       11   0.12359551      0   1.1659977
53:      MUT7        e5     53        89      210   2.35955056      0  22.0465660
54:      MUT6        e6     54        89     8996 101.07865169      0 307.3379922
55:      MUT5        e7     55        89    17278 194.13483146      0 500.1860834
56:      MUT4        e8     56        89     1724  19.37078652      0 113.0606617
57:      MUT3        e9     57        89    11738 131.88764045     20 223.2198487
58:      MUT2       e10     58        89    27963 314.19101124    146 406.5002202
59:      MUT1       e11     59        89    32120 360.89887640    209 417.5670345
60:   MUT_CTL       e12     60        89    23596 265.12359551    127 358.5456678
61:    WT_CTL        f1     61        89    12156 136.58426966      0 322.6320663
62:     MUT10        f2     62        89    10804 121.39325843      0 247.2559227
63:      MUT9        f3     63        89       11   0.12359551      0   0.7510633
64:      MUT8        f4     64        89      307   3.44943820      0  32.3282213
65:      MUT7        f5     65        89       31   0.34831461      0   2.8005727
66:      MUT6        f6     66        89     5828  65.48314607      0 117.8844882
67:      MUT5        f7     67        89     8880  99.77528090      0 299.0273913
68:      MUT4        f8     68        89      227   2.55056180      0  23.6354949
69:      MUT3        f9     69        89    19924 223.86516854    128 323.9902563
70:      MUT2       f10     70        89    30185 339.15730337    269 391.6490167
71:      MUT1       f11     71        89    23295 261.74157303    182 327.0673939
72:   MUT_CTL       f12     72        89    33740 379.10112360    258 407.9149601
73:    WT_CTL        g1     73        89     1957  21.98876404      0 150.4140871
74:     MUT10        g2     74        89     1883  21.15730337      0 112.9026027
75:      MUT9        g3     75        89        9   0.10112360      0   0.7693702
76:      MUT8        g4     76        89        0   0.00000000      0   0.0000000
77:      MUT7        g5     77        89        0   0.00000000      0   0.0000000
78:      MUT6        g6     78        89    14625 164.32584270      0 367.9194198
79:      MUT5        g7     79        89    10308 115.82022472      0 282.0390673
80:      MUT4        g8     80        89     3891  43.71910112      0 235.0162948
81:      MUT3        g9     81        89    14606 164.11235955      3 408.9459132
82:      MUT2       g10     82        89    20714 232.74157303    118 317.0294125
83:      MUT1       g11     83        89    35864 402.96629213    236 489.6790797
84:   MUT_CTL       g12     84        89    30346 340.96629213    218 414.3528209
85:    WT_CTL        h1     85        89     5688  63.91011236      0 286.1771797
86:     MUT10        h2     86        89        1   0.01123596      0   0.1059998
87:      MUT9        h3     87        89        0   0.00000000      0   0.0000000
88:      MUT8        h4     88        89      536   6.02247191      0  53.8480559
89:      MUT7        h5     89        89     1071  12.03370787      0 113.5257729
90:      MUT6        h6     90        89     9495 106.68539326      0 367.4626015
91:      MUT5        h7     91        89       10   0.11235955      0   0.6980200
92:      MUT4        h8     92        89        0   0.00000000      0   0.0000000
93:      MUT3        h9     93        89    19009 213.58426966      0 395.8312258
94:      MUT2       h10     94        89    41792 469.57303371    303 487.6894477
95:      MUT1       h11     95        89    38532 432.94382022    387 383.2050353
96:   MUT_CTL       h12     96        89    23303 261.83146067    135 332.8016825
    condition loc_coord loc_id n_samples sum_dark         mean median          sd

So I have this code in which I am trying to generate a figure. The conditions along the x-axis are declared to be factors. What I am wondering is if it is possible to move two of these condition labels to the left, so that the order is WT_CTL, MUT_CTL, MUT1, MUT2, MUT3, etc. Is it possible to move only two of the x-tick labels and have the rest sorted as factors?

sum_tab_cond_Loc &lt;- sum_tabnew[!condition == &quot;empty&quot;,
                               list(n_samples=as.double(.N),
                                    sum_dark=as.double(sum(pixel_diff_by_sec)),
                                    mean=as.double(mean(pixel_diff_by_sec)),
                                    median=as.double(median(pixel_diff_by_sec)),
                                    sd=sd(pixel_diff_by_sec),
                                    sem=std.error(pixel_diff_by_sec)),
                               by=list(condition, loc_coord, loc_id)]

sum_tab_cond_Loc$condition &lt;- factor(sum_tab_cond_Loc$condition,
                            levels = gtools::mixedsort(unique(sum_tab_cond_Loc$condition)))

p_mean_condition_time &lt;- ggplot((sum_tab_cond_Loc), aes(x=condition, y=mean, color=condition, fill=condition, group=condition)) +
  geom_line(lwd=1) + 
  geom_point(size=5,shape=22, stroke=1.4, color=&quot;black&quot;) +
  labs(title=&quot; &quot;, x=&#39;Conditions&#39;, y = &quot;Means of Pixel Differences&quot;) + 
  theme_classic(base_size = 20)+
  geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem), width=0.1, position=position_dodge(width=0.9))

答案1

得分: 1

以下是代码的翻译部分:

这里是一份演示,使用一些更简单的数据。

library(ggplot2)
mtcars <- datasets::mtcars
mtcars$gear2 = factor(paste0("gear", mtcars$gear))

Before(之前):

ggplot(mtcars, aes(x=gear2, y=mpg, color=cyl, fill=cyl, group=cyl)) +
  geom_line(lwd=1, position=position_dodge(width=0.9)) + 
  geom_point(size=5,shape=22, stroke=1.4, color="black", position=position_dodge(width=0.9)) +
  labs(title=" ", x='Conditions', y = "Means of Pixel Differences") + 
  theme_classic(base_size = 20)+
  geom_errorbar(aes(ymin=mpg-2, ymax=mpg+2), width=0.1, position=position_dodge(width=0.9))

After(之后),使用相同的绘图代码:

[![enter image description here][2]][2]

请注意,这里的代码是基于你提供的信息进行翻译的。如果需要进一步的帮助,请告诉我。

英文:

Here is a demonstration with some simpler data.

library(ggplot2)
mtcars &lt;- datasets::mtcars
mtcars$gear2 = factor(paste0(&quot;gear&quot;, mtcars$gear))

Before:

ggplot(mtcars, aes(x=gear2, y=mpg, color=cyl, fill=cyl, group=cyl)) +
  geom_line(lwd=1, position=position_dodge(width=0.9)) + 
  geom_point(size=5,shape=22, stroke=1.4, color=&quot;black&quot;, position=position_dodge(width=0.9)) +
  labs(title=&quot; &quot;, x=&#39;Conditions&#39;, y = &quot;Means of Pixel Differences&quot;) + 
  theme_classic(base_size = 20)+
  geom_errorbar(aes(ymin=mpg-2, ymax=mpg+2), width=0.1, position=position_dodge(width=0.9))

只重新排列 ggplot 中的 2 个 x 轴刻度标签,其余的按因子顺序排列。

Here we can adjust the ordering of the gear2 factor:

mtcars$gear2 = forcats::fct_relevel(mtcars$gear2, &quot;gear4&quot;, after = 0)

After, with same plot code:

只重新排列 ggplot 中的 2 个 x 轴刻度标签,其余的按因子顺序排列。

答案2

得分: 0

以下是翻译好的部分:

"Another potential solution is to 'manually' specify the levels, i.e. put MUT_CTL and WT_CTL at the beginning of the levels and exclude MUT_CTL and WT_CTL from the sorting, e.g.

library(tidyverse)

sum_tab_cond_Loc <- read.table(text = "    condition loc_coord loc_id n_samples sum_dark         mean median          sd
 1:    WT_CTL        a1      1        89    14816 166.47191011      0 381.5107853
 2:     MUT10        a2      2        89     5748  64.58426966      0 156.9145547
 3:      MUT9        a3      3        89        0   0.00000000      0   0.0000000
 4:      MUT8        a4      4        89        0   0.00000000      0   0.0000000
 5:      MUT7        a5      5        89     1065  11.96629213      0 112.8897742
...
96:   MUT_CTL       h12     96        89    23303 261.83146067    135 332.8016825",
header = TRUE)

sum_tab_cond_Loc %>%
  mutate(sem = sd / sqrt(nrow(sum_tab_cond_Loc)),
         condition = factor(
           condition,
           levels = c("WT_CTL", "MUT_CTL",
                      gtools::mixedsort(unique(sum_tab_cond_Loc$condition)[!unique(sum_tab_cond_Loc$condition) %in% c("WT_CTL", "MUT_CTL")])))) %>%
  ggplot(aes(x=condition, y=mean,
             color=condition, fill=condition,
             group=condition)) +
  geom_line(lwd=1) + 
  geom_point(size=5,shape=22, stroke=1.4, color="black") +
  labs(title=" ", x='Conditions', y = "Means of Pixel Differences") + 
  theme_classic(base_size = 20)+
  geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem), width=0.1,
                position=position_dodge(width=0.9))

只重新排列 ggplot 中的 2 个 x 轴刻度标签,其余的按因子顺序排列。"

英文:

Another potential solution is to 'manually' specify the levels, i.e. put MUT_CTL and WT_CTL at the beginning of the levels and exclude MUT_CTL and WT_CTL from the sorting, e.g.

library(tidyverse)

sum_tab_cond_Loc &lt;- read.table(text = &quot;    condition loc_coord loc_id n_samples sum_dark         mean median          sd
 1:    WT_CTL        a1      1        89    14816 166.47191011      0 381.5107853
 2:     MUT10        a2      2        89     5748  64.58426966      0 156.9145547
 3:      MUT9        a3      3        89        0   0.00000000      0   0.0000000
 4:      MUT8        a4      4        89        0   0.00000000      0   0.0000000
 5:      MUT7        a5      5        89     1065  11.96629213      0 112.8897742
 6:      MUT6        a6      6        89        2   0.02247191      0   0.2119996
 7:      MUT5        a7      7        89    23891 268.43820225     85 409.9059469
 8:      MUT4        a8      8        89     4691  52.70786517      0 197.9350331
 9:      MUT3        a9      9        89    17244 193.75280899     37 330.9582259
10:      MUT2       a10     10        89    19653 220.82022472     84 294.4198286
11:      MUT1       a11     11        89    32244 362.29213483    187 390.1419090
12:   MUT_CTL       a12     12        89    20547 230.86516854    144 284.6004053
13:    WT_CTL        b1     13        89        9   0.10112360      0   0.6224027
14:     MUT10        b2     14        89      934  10.49438202      0  59.0065720
15:      MUT9        b3     15        89     2591  29.11235955      0 184.8712966
16:      MUT8        b4     16        89        5   0.05617978      0   0.5299989
17:      MUT7        b5     17        89     3146  35.34831461      0  95.4808526
18:      MUT6        b6     18        89        0   0.00000000      0   0.0000000
19:      MUT5        b7     19        89    12080 135.73033708      0 379.4003571
20:      MUT4        b8     20        89      855   9.60674157      0  78.7162075
21:      MUT3        b9     21        89    27055 303.98876404    140 412.2091949
22:      MUT2       b10     22        89    29327 329.51685393    225 348.2187176
23:      MUT1       b11     23        89    31765 356.91011236    186 416.8646674
24:   MUT_CTL       b12     24        89    22682 254.85393258    161 318.7144116
25:    WT_CTL        c1     25        89    16498 185.37078652    117 245.1523822
26:     MUT10        c2     26        89     1169  13.13483146      0  91.5546522
27:      MUT9        c3     27        89     6932  77.88764045      0 247.3136782
28:      MUT8        c4     28        89      228   2.56179775      0  23.6359487
29:      MUT7        c5     29        89      624   7.01123596      0  43.9693836
30:      MUT6        c6     30        89     5643  63.40449438      0 277.5699224
31:      MUT5        c7     31        89    19713 221.49438202     93 320.8856750
32:      MUT4        c8     32        89       26   0.29213483      0   1.4632020
33:      MUT3        c9     33        89    13328 149.75280899     21 290.6393141
34:      MUT2       c10     34        89    16007 179.85393258     73 248.5571797
35:      MUT1       c11     35        89    19859 223.13483146     94 315.2131382
36:   MUT_CTL       c12     36        89    38148 428.62921348    316 417.0589500
37:    WT_CTL        d1     37        89      692   7.77528090      0  61.8612364
38:     MUT10        d2     38        89     5397  60.64044944      0 268.0002649
39:      MUT9        d3     39        89       16   0.17977528      0   0.8469437
40:      MUT8        d4     40        89        0   0.00000000      0   0.0000000
41:      MUT7        d5     41        89     1958  22.00000000      0 129.0508180
42:      MUT6        d6     42        89    13699 153.92134831      0 318.4321758
43:      MUT5        d7     43        89     3734  41.95505618      0 119.0242441
44:      MUT4        d8     44        89       19   0.21348315      0   1.7219961
45:      MUT3        d9     45        89    19630 220.56179775     49 322.1956265
46:      MUT2       d10     46        89    26129 293.58426966    156 349.9139767
47:      MUT1       d11     47        89    46637 524.01123596    412 571.9543945
48:   MUT_CTL       d12     48        89    36407 409.06741573    235 469.2885524
49:    WT_CTL        e1     49        89    14722 165.41573034      0 295.4558423
50:     MUT10        e2     50        89     4546  51.07865169      0 231.8792899
51:      MUT9        e3     51        89      542   6.08988764      0  57.2378690
52:      MUT8        e4     52        89       11   0.12359551      0   1.1659977
53:      MUT7        e5     53        89      210   2.35955056      0  22.0465660
54:      MUT6        e6     54        89     8996 101.07865169      0 307.3379922
55:      MUT5        e7     55        89    17278 194.13483146      0 500.1860834
56:      MUT4        e8     56        89     1724  19.37078652      0 113.0606617
57:      MUT3        e9     57        89    11738 131.88764045     20 223.2198487
58:      MUT2       e10     58        89    27963 314.19101124    146 406.5002202
59:      MUT1       e11     59        89    32120 360.89887640    209 417.5670345
60:   MUT_CTL       e12     60        89    23596 265.12359551    127 358.5456678
61:    WT_CTL        f1     61        89    12156 136.58426966      0 322.6320663
62:     MUT10        f2     62        89    10804 121.39325843      0 247.2559227
63:      MUT9        f3     63        89       11   0.12359551      0   0.7510633
64:      MUT8        f4     64        89      307   3.44943820      0  32.3282213
65:      MUT7        f5     65        89       31   0.34831461      0   2.8005727
66:      MUT6        f6     66        89     5828  65.48314607      0 117.8844882
67:      MUT5        f7     67        89     8880  99.77528090      0 299.0273913
68:      MUT4        f8     68        89      227   2.55056180      0  23.6354949
69:      MUT3        f9     69        89    19924 223.86516854    128 323.9902563
70:      MUT2       f10     70        89    30185 339.15730337    269 391.6490167
71:      MUT1       f11     71        89    23295 261.74157303    182 327.0673939
72:   MUT_CTL       f12     72        89    33740 379.10112360    258 407.9149601
73:    WT_CTL        g1     73        89     1957  21.98876404      0 150.4140871
74:     MUT10        g2     74        89     1883  21.15730337      0 112.9026027
75:      MUT9        g3     75        89        9   0.10112360      0   0.7693702
76:      MUT8        g4     76        89        0   0.00000000      0   0.0000000
77:      MUT7        g5     77        89        0   0.00000000      0   0.0000000
78:      MUT6        g6     78        89    14625 164.32584270      0 367.9194198
79:      MUT5        g7     79        89    10308 115.82022472      0 282.0390673
80:      MUT4        g8     80        89     3891  43.71910112      0 235.0162948
81:      MUT3        g9     81        89    14606 164.11235955      3 408.9459132
82:      MUT2       g10     82        89    20714 232.74157303    118 317.0294125
83:      MUT1       g11     83        89    35864 402.96629213    236 489.6790797
84:   MUT_CTL       g12     84        89    30346 340.96629213    218 414.3528209
85:    WT_CTL        h1     85        89     5688  63.91011236      0 286.1771797
86:     MUT10        h2     86        89        1   0.01123596      0   0.1059998
87:      MUT9        h3     87        89        0   0.00000000      0   0.0000000
88:      MUT8        h4     88        89      536   6.02247191      0  53.8480559
89:      MUT7        h5     89        89     1071  12.03370787      0 113.5257729
90:      MUT6        h6     90        89     9495 106.68539326      0 367.4626015
91:      MUT5        h7     91        89       10   0.11235955      0   0.6980200
92:      MUT4        h8     92        89        0   0.00000000      0   0.0000000
93:      MUT3        h9     93        89    19009 213.58426966      0 395.8312258
94:      MUT2       h10     94        89    41792 469.57303371    303 487.6894477
95:      MUT1       h11     95        89    38532 432.94382022    387 383.2050353
96:   MUT_CTL       h12     96        89    23303 261.83146067    135 332.8016825&quot;,
header = TRUE)

sum_tab_cond_Loc %&gt;%
  mutate(sem = sd / sqrt(nrow(sum_tab_cond_Loc)),
         condition = factor(
           condition,
           levels = c(&quot;WT_CTL&quot;, &quot;MUT_CTL&quot;,
                      gtools::mixedsort(unique(sum_tab_cond_Loc$condition)[!unique(sum_tab_cond_Loc$condition) %in% c(&quot;WT_CTL&quot;, &quot;MUT_CTL&quot;)])))) %&gt;%
  ggplot(aes(x=condition, y=mean,
             color=condition, fill=condition,
             group=condition)) +
  geom_line(lwd=1) + 
  geom_point(size=5,shape=22, stroke=1.4, color=&quot;black&quot;) +
  labs(title=&quot; &quot;, x=&#39;Conditions&#39;, y = &quot;Means of Pixel Differences&quot;) + 
  theme_classic(base_size = 20)+
  geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem), width=0.1,
                position=position_dodge(width=0.9))

只重新排列 ggplot 中的 2 个 x 轴刻度标签,其余的按因子顺序排列。<!-- -->

<sup>Created on 2023-04-20 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年4月20日 07:09:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定