1

So I am trying to plot 8 lines in the same plot using ggplot. The first 4 data tables are

table1
       lat1       value1
1:     -40     301.02466
2:     -30     207.82735
3:     -20     126.60357
4:       0      61.59146
5:     -10      79.90467
6:      70     530.99759
7:      60     508.62757
8:      20     163.81903
9:      40     353.74023
10:      50     444.52367
11:      30     262.61068
12:      10     92.77790

table2
       lat2       value2
1:     -40     466
2:     -30     735
3:     -20     357
4:       0     146
5:     -10     467
6:      70     759
7:      60     757
8:      20     903
9:      40     23
10:      50    67
11:      30    68
12:      10    90

table3
       lat3     value3
1:     -40      301
2:     -30      207
3:     -20      126
4:       0      61
5:     -10      79
6:      70      530
7:      60      508
8:      20      163
9:      40      353
10:      50     444
11:      30     262
12:      10     92

table4
       lat4     value4
1:     -40      66
2:     -30      35
3:     -20      57
4:       0      61
5:     -10      79
6:      70      59
7:      60      57
8:      20      903
9:      40      23
10:      50     467
11:      30     268
12:      10     92

So lat1,lat2,lat3 and lat4 in the first 4 tables are the same,but value1, value2,value3 and value4 are different.

And then the other 4 comes from 4 data tables that are somewhat different from each other

table5
  lat_bin  value5   
1  -89.5 42.55258
2    -88 44.48275
3    -86 40.20298
4    -84 44.27084
5    -82 36.07924
6    -80 31.11763
7    -78 45.79151
8    -76 54.29885
9    -74 57.80390
10   -72 48.83430
11   -70 60.94235
12   -68 58.93437
13   -66 42.33511
14   -64 53.37018
15   -62 53.15876
16   -60 51.00019
17   -58 38.27228
18   -56 45.49484
19   -54 38.54724
20   -52 51.12241
21   -50 62.65382
22   -48 18.66394
23   -46 65.11314
24   -44 40.40835
25   -42 47.90962
26   -40 39.36127
27   -38 60.47418
28   -36 65.47417
29   -34 49.92712
30   -32 49.74337
31   -30 44.54881
32   -28 39.05619
33   -26 50.83410
34   -24 43.42512
35   -22 53.96446
36   -20 57.05264
37   -18 47.70423
38   -16 49.89761
39   -14 59.55105
40   -12 50.19142
41   -10 40.91756
42    -8 58.55612
43    -6 47.79473
44    -4 47.82496
45    -2 48.71782
46     0 66.58965
47     2 35.91426
48     4 49.55786
49     6 52.65538
50     8 34.92646
51    10 42.75266
52    12 35.45590
53    14 58.28427
54    16 63.78742
55    18 64.06479
56    20 64.63604
57    22 41.83663
58    24 49.60322
59    26 66.76320
60    28 60.16386
61    30 39.19644
62    32 31.61131
63    34 60.40856
64    36 65.01639
65    38 53.00441
66    40 35.36090
67    42 37.07637
68    44 57.88424
69    46 69.61711
70    48 51.82013
71    50 40.27362
72    52 28.46597
73    54 51.31858
74    56 45.76476
75    58 43.43081
76    60 33.12539
77    62 41.98876
78    64 50.48216
79    66 54.47632
80    68 53.48455
81    70 42.26042
82    72 66.94307
83    74 38.10385
84    76 73.20288
85    78 65.91729
86    80 45.25387
87    82 39.97535
88    84 61.79927
89    86 44.12366
90  89.5 50.88704



table6
 lat_bin value6
1   -89 33.25081
2   -86 50.57526
3   -82 51.03720
4   -78 45.04662
5   -74 44.21023
6   -70 44.78959
7   -66 49.58004
8   -62 31.55569
9   -58 65.84564
10  -54 45.57504
11  -50 48.27276
12  -46 41.21168
13  -42 46.59545
14  -38 48.37215
15  -34 57.53817
16  -30 63.89909
17  -26 38.17253
18  -22 53.53277
19  -18 57.68382
20  -14 64.66252
21  -10 68.73180
22   -6 38.83478
23   -2 41.99954
24    2 39.97559
25    6 62.67517
26   10 40.38488
27   14 56.76487
28   18 46.85102
29   22 29.00399
30   26 66.07107
31   30 39.43762
32   34 46.47184
33   38 42.84949
34   42 27.49553
35   46 43.40828
36   50 41.62040
37   54 44.94795
38   58 50.79928
39   62 35.14443
40   66 55.07089
41   70 48.42919
42   74 57.75584
43   78 59.52185
44   82 56.02233
45   86 40.92509
46   89 49.09111

 table7
 lat_bin  value7
1  -89.5 51.35416
2    -88 31.67682
3    -86 49.86942
4    -84 44.45800
5    -82 33.43398
6    -80 57.51636
7    -78 56.17223
8    -76 40.08851
9    -74 69.38469
10   -72 44.52990
11   -70 45.97725
12   -68 39.24309
13   -66 38.05944
14   -64 53.56062
15   -62 29.40178
16   -60 33.88924
17   -58 67.60847
18   -56 51.07180
19   -54 49.41184
20   -52 44.62908
21   -50 58.48811
22   -48 54.42683
23   -46 39.18355
24   -44 36.91459
25   -42 60.29483
26   -40 60.63588
27   -38 46.57548
28   -36 30.49558
29   -34 60.75026
30   -32 47.24667
31   -30 63.74822
32   -28 40.29159
33   -26 44.54154
34   -24 36.42701
35   -22 59.61671
36   -20 42.81636
37   -18 51.81676
38   -16 48.81125
39   -14 49.83641
40   -12 40.67852
41   -10 46.81491
42    -8 61.98377
43    -6 28.85338
44    -4 49.04988
45    -2 35.43850
46     0 49.97232
47     2 45.14729
48     4 64.80487
49     6 41.86113
50     8 51.54523
51    10 48.55555
52    12 46.43194
53    14 41.37419
54    16 50.84800
55    18 59.88207
56    20 47.82404
57    22 61.02776
58    24 62.07988
59    26 51.07930
60    28 49.46572
61    30 30.42886
62    32 37.51703
63    34 51.80884
64    36 39.18938
65    38 53.45915
66    40 55.07158
67    42 54.23841
68    44 56.96546
69    46 67.98550
70    48 38.61506
71    50 52.59336
72    52 55.34652
73    54 61.33864
74    56 30.45584
75    58 42.34978
76    60 34.72779
77    62 46.72686
78    64 52.57788
79    66 52.81603
80    68 57.31276
81    70 49.83944
82    72 46.19704
83    74 55.14985
84    76 26.77623
85    78 56.74318
86    80 52.33034
87    82 51.57052
88    84 41.60713
89    86 66.26505
90    88 40.13103
91  89.5 62.81118

table8
   lat_bin  value8
1   -89 53.15885
2   -86 46.90052
3   -82 58.59391
4   -78 58.82940
5   -74 47.70582
6   -70 55.54542
7   -66 58.24865
8   -62 43.64434
9   -58 43.01514
10  -54 51.31282
11  -50 52.00405
12  -46 31.41890
13  -42 29.52800
14  -38 48.03036
15  -34 32.38335
16  -30 54.32492
17  -26 45.79151
18  -22 42.55999
19  -18 42.13147
20  -14 51.33367
21  -10 38.70702
22   -6 43.86864
23   -2 42.88326
24    2 38.91993
25    6 62.63207
26   10 50.99667
27   14 42.10944
28   18 74.06162
29   22 33.90856
30   26 55.22473
31   30 47.97072
32   34 56.15491
33   38 31.80902
34   42 59.22560
35   46 37.32012
36   50 61.66584
37   54 35.03234
38   58 50.40256
39   62 32.45835
40   66 63.98039
41   70 51.47580
42   74 52.49533
43   78 50.93706
44   82 68.29959
45   86 57.30890
46   89 59.53932

I want to use factor to plot the 8 lines because I am also going to plot a boxplot on the same plot later.

To do this, I set the levels of all 8 data tables the same and then tried to plot them together (the number of lines that I plot doesn't matter because the same problem always there). And the code is

allLevels<-levels(factor(c(table1$x, table2$x, talbe3$x, table4$x, table5$x, table6$x, table7$x, table8$x)))

table1$x<-factor(table1$x,levels=(allLevels))
table2$x<-factor(table2$x,levels=(allLevels))
table3$x<-factor(table3$x,levels=(allLevels))
table4$x<-factor(table4$x,levels=(allLevels))

table5$x<-factor(table5$x,levels=(allLevels))
table6$x<-factor(table6$x,levels=(allLevels))
table7$x<-factor(table7$x,levels=(allLevels))
table8$x<-factor(table8$x,levels=(allLevels))

ggplot()+geom_line(aes(x=x, y=y, colour="1"), data = 
table1,group=1)+geom_line(aes(x=x, y=y, colour="2"), data = 
table2,group=1)+geom_line(aes(x=x, y=y, colour="3"), data = 
table3,group=1)+geom_line(aes(x=x, y=y, colour="4"), data = 
table4,group=1)+geom_line(aes(x=x, y=y, colour="5"), data = 
table5,group=1)

The result plot, the x axis is a total mess.

result plot

Thank you very much, and tell me if you need any more information!!

Follow up pic. pic

x t
  • 11
  • 2

2 Answers2

0

I don't think you should make x a factor, it looks like a continuous variable to me. Instead, you should create a separate variable called something like Source to identify each table, then combine all the tables and plot with the combined data. For example:

table1 = data.frame(
    x = sample(-100:100, 50),
    y = rnorm(50)
)
table2 = data.frame(
    x = sample(-100:100, 50),
    y = rnorm(50)
)

table1$Source = "tab1"
table2$Source = "tab2"

combined = rbind(table1, table2)

ggplot(combined, aes(x, y, colour = Source)) +
    geom_line()
Marius
  • 58,213
  • 16
  • 107
  • 105
0

A general rule: if you find yourself adding multiple geoms of the same type in ggplot, you're doing it wrong. In addition aes() is used to map to a variable in the data frame (column name), not a string.

I think something like this should work for your data: use dplyr::bind_rows() to combine the data frames and add an identifier which can be used to map to color.

library(dplyr)
library(ggplot2)

bind_rows(table1, table2, table3, table4,
          table5, table6, table7, table8,
          .id = "dataset") %>%
mutate(dataset = paste0("table", dataset)) %>%
ggplot(aes(x, y)) +
geom_line(aes(color = dataset))
neilfws
  • 32,751
  • 5
  • 50
  • 63
  • Thank you for your kind reply! One question is that the first 4 tables have the same x name but different y name, and the last 4 tables all have different x names and y names, and I think in that way i have to write multiple geom_lines? I just use the same x and y for all tables for simplicity in this question. – x t Oct 23 '19 at 18:35
  • I’d use dplyr::rename to make the column names consistent. – neilfws Oct 23 '19 at 19:49
  • Hey I used the code and the result figure is 4 lines that have lat_bin as x just squeezed in the middle. (T—T) – x t Oct 23 '19 at 23:42
  • `lat_bin` doesn't appear in your question so we don't know what that is. I think it will help if you edit the question to [make it reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including a complete set of example data. – neilfws Oct 23 '19 at 23:44
  • Sorry something was not clear in the question, i made some changes to it and also paste the figure that I generate using your method. – x t Oct 24 '19 at 00:18