ggplot2方面的图表顺序

时间:2017-09-13 22:24:29

标签: r ggplot2

>Panel_Data
concentration Statistic                               name
1            100    39.100                     CD4 subset_but
2             10    39.700                     CD4 subset_but
3              1     0.012                     CD4 subset_but
4              0    41.200                     CD4 subset_but
5            100     3.970        CD4 subset/CD103 subset_but
6             10     2.940        CD4 subset/CD103 subset_but
7              1   100.000        CD4 subset/CD103 subset_but
8              0     2.620        CD4 subset/CD103 subset_but
9            100    57.900         CD4 subset/CD39 subset_but
10            10    31.200         CD4 subset/CD39 subset_but
11             1     0.000         CD4 subset/CD39 subset_but
12             0    30.600         CD4 subset/CD39 subset_but
13           100     8.090         CD4 subset/CD69 subset_but
14            10     6.530         CD4 subset/CD69 subset_but
15             1   100.000         CD4 subset/CD69 subset_but
16             0     4.930         CD4 subset/CD69 subset_but
17           100    49.700         CD4 subset/CD73 subset_but
18            10    51.300         CD4 subset/CD73 subset_but
19             1    25.000         CD4 subset/CD73 subset_but
20             0    49.800         CD4 subset/CD73 subset_but
21           100     4.520  CD4 subset/integrin B7 subset_but
22            10     4.230  CD4 subset/integrin B7 subset_but
23             1    75.000  CD4 subset/integrin B7 subset_but
24             0     4.370  CD4 subset/integrin B7 subset_but
25           100    34.300                    CD8a subset_but
26            10    36.700                    CD8a subset_but
27             1     0.012                    CD8a subset_but
28             0    33.500                    CD8a subset_but
29           100     4.610       CD8a subset/CD103 subset_but
30            10     3.400       CD8a subset/CD103 subset_but
31             1    75.000       CD8a subset/CD103 subset_but
32             0     4.060       CD8a subset/CD103 subset_but
33           100    74.900        CD8a subset/CD39 subset_but
34            10    56.900        CD8a subset/CD39 subset_but
35             1     0.000        CD8a subset/CD39 subset_but
36             0    54.300        CD8a subset/CD39 subset_but
37           100     7.320        CD8a subset/CD69 subset_but
38            10     6.870        CD8a subset/CD69 subset_but
39             1     0.000        CD8a subset/CD69 subset_but
40             0     5.480        CD8a subset/CD69 subset_but
41           100    72.500        CD8a subset/CD73 subset_but
42            10    73.200        CD8a subset/CD73 subset_but
43             1     0.000        CD8a subset/CD73 subset_but
44             0    73.700        CD8a subset/CD73 subset_but
45           100    16.400 CD8a subset/integrin B7 subset_but
46            10    17.000 CD8a subset/integrin B7 subset_but
47             1     0.000 CD8a subset/integrin B7 subset_but
48             0    19.900 CD8a subset/integrin B7 subset_but

我使用以下代码绘制了上述数据:

Panel_Data_Graph <- ggplot(Panel_Data, 
                           aes(concentration, Statistic)) +
  geom_point(color="black") + 
  facet_wrap(~ name, ncol = 3) +
  xlab("Concentration") + 
  ylab(expression("Statistic")) +
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE, size = 0.4) +
  theme_bw() 

print(Panel_Data_Graph)

但是,图表输出如下:

graph output

我希望图表按Panel_Data $ name排序。例如,左上行应该是CD4 subset_but,然后是CD4子集/ CD103 subset_but,然后是CD子集/ CD39 subset_but。

对于图中显示的对应正确图形的回归线,图形必须按以下顺序排列:

  

regrDF

                                 name              lbl
1                      CD4 subset_but y = -0.358x + 40
2         CD4 subset/CD103 subset_but  y = 0.396x + 20
3          CD4 subset/CD39 subset_but y = -0.334x + 40
4          CD4 subset/CD69 subset_but y = 0.0897x + 40
5          CD4 subset/CD73 subset_but y = -0.267x + 30
6   CD4 subset/integrin B7 subset_but  y = 0.139x + 30
7                     CD8a subset_but y = -0.263x + 30
8        CD8a subset/CD103 subset_but  y = 0.412x + 40
9         CD8a subset/CD39 subset_but  y = 0.0363x + 4
10        CD8a subset/CD69 subset_but   y = 0.27x + 50
11        CD8a subset/CD73 subset_but y = 0.0471x + 10
12 CD8a subset/integrin B7 subset_but  y = 0.128x + 20

2 个答案:

答案 0 :(得分:3)

以这种方式修改数据框

Panel_Data$name <- factor(Panel_Data$name, levels=c(order-you-want), ordered=TRUE)

您需要将因子级别设置为ordered=TRUE

请参阅此可重现的示例

data <- mtcars
data$cyl <- factor(data$cyl, levels=c(6,4,8), ordered=TRUE)
library(ggplot2)

# unordered
ggplot(data=mtcars, aes(x=carb, y=mpg, color=cyl)) +
  geom_point() +
  facet_wrap(~cyl, ncol=3)

# ordered
ggplot(data=data, aes(x=carb, y=mpg, color=cyl)) +
  geom_point() +
  facet_wrap(~cyl, ncol=3)

答案 1 :(得分:0)

levels(Panel_Data$name) <- Panel_Data$name[1:(i+3)==(i+3)]