Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shapes and Linetypes in ggplot

In the following df:

> seperate
   Tenors    Ratio  Pairs  Subgroup
1      1W  93.9426 audusd USD Pairs
2      1M  97.6231 audusd USD Pairs
3      3M 100.0376 audusd USD Pairs
4      6M 108.7951 audusd USD Pairs
5     12M 127.6704 audusd USD Pairs
6      1W  93.6322 eurusd USD Pairs
7      1M  93.5800 eurusd USD Pairs
8      3M  96.2518 eurusd USD Pairs
9      6M 101.1169 eurusd USD Pairs
10    12M 108.7339 eurusd USD Pairs
11     1W  92.8932 gbpusd USD Pairs
12     1M  89.6215 gbpusd USD Pairs
13     3M  83.8495 gbpusd USD Pairs
14     6M  86.3159 gbpusd USD Pairs
15    12M  86.9095 gbpusd USD Pairs
16     1W  94.4111 usdcad USD Pairs
17     1M  86.4612 usdcad USD Pairs
18     3M  88.4969 usdcad USD Pairs
19     6M  94.3754 usdcad USD Pairs
20    12M 103.1338 usdcad USD Pairs
21     1W  97.9665 usdchf USD Pairs
22     1M  97.1005 usdchf USD Pairs
23     3M  93.1135 usdchf USD Pairs
24     6M  90.2106 usdchf USD Pairs
25    12M  84.7482 usdchf USD Pairs
26     1W  85.7557 usdjpy USD Pairs
27     1M  89.6454 usdjpy USD Pairs
28     3M  90.6929 usdjpy USD Pairs
29     6M  90.7980 usdjpy USD Pairs
30    12M  95.8823 usdjpy USD Pairs
31     1W  92.2133 nzdusd USD Pairs
32     1M  95.3295 nzdusd USD Pairs
33     3M  99.8976 nzdusd USD Pairs
34     6M 107.4463 nzdusd USD Pairs
35    12M 124.3403 nzdusd USD Pairs
36     1W  88.2031 usddkk USD Pairs
37     1M  93.0318 usddkk USD Pairs
38     3M  91.5042 usddkk USD Pairs
39     6M  99.4707 usddkk USD Pairs
40    12M  97.4030 usddkk USD Pairs
41     1W  95.8640 usdnok USD Pairs
42     1M  88.9647 usdnok USD Pairs
43     3M  93.5782 usdnok USD Pairs
44     6M  99.4802 usdnok USD Pairs
45    12M 107.7916 usdnok USD Pairs
46     1W  91.5501 usdsek USD Pairs
47     1M  89.5148 usdsek USD Pairs
48     3M  92.1016 usdsek USD Pairs
49     6M  99.6835 usdsek USD Pairs
50    12M 112.8247 usdsek USD Pairs
51     1W  43.6337 usdhkd USD Pairs
52     1M  61.4948 usdhkd USD Pairs
53     3M  62.5292 usdhkd USD Pairs
54     6M  54.5213 usdhkd USD Pairs
55    12M  41.1970 usdhkd USD Pairs
56     1W  93.7892 usdzar USD Pairs
57     1M  92.0233 usdzar USD Pairs
58     3M  94.2433 usdzar USD Pairs
59     6M 105.9208 usdzar USD Pairs
60    12M 108.5231 usdzar EUR Pairs
61     1W  95.9848 euraud EUR Pairs
62     1M  96.6497 euraud EUR Pairs
63     3M  99.2588 euraud EUR Pairs
64     6M 103.1839 euraud EUR Pairs
65    12M 111.2710 euraud EUR Pairs
66     1W  90.3670 eurchf EUR Pairs
67     1M  92.9810 eurchf EUR Pairs
68     3M  86.7696 eurchf EUR Pairs
69     6M  92.4201 eurchf EUR Pairs
70    12M 107.0379 eurchf EUR Pairs
71     1W  92.5969 eurgbp EUR Pairs
72     1M  86.2361 eurgbp EUR Pairs
73     3M  81.5729 eurgbp EUR Pairs
74     6M  82.2716 eurgbp EUR Pairs
75    12M  90.0153 eurgbp EUR Pairs
76     1W  92.9831 eurjpy EUR Pairs
77     1M  91.2058 eurjpy EUR Pairs
78     3M  90.1872 eurjpy EUR Pairs
79     6M  90.9569 eurjpy EUR Pairs
80    12M  98.0120 eurjpy EUR Pairs
81     1W  87.7428 eurnok EUR Pairs
82     1M  84.9459 eurnok EUR Pairs
83     3M  86.7989 eurnok EUR Pairs
84     6M  87.0153 eurnok EUR Pairs
85    12M  98.4807 eurnok EUR Pairs
86     1W  97.1092 eurtry EUR Pairs
87     1M  93.0774 eurtry EUR Pairs
88     3M  96.7237 eurtry EUR Pairs
89     6M  91.3461 eurtry EUR Pairs
90    12M  75.9171 eurtry EUR Pairs

.. with the following plot:

ggplot(seperate, aes(x=Tenors,y =Ratio, colour=Pairs, group=Tenors)) +       
  geom_line(    data=seperate,aes(x=Tenors,y=Ratio,group=Pairs,linetype=Pairs))+
  geom_point(data=seperate,aes(x=Tenors,y=Ratio,group=Pairs,shape=Pairs))+
  facet_grid(Subgroup~.)+
  xlab("Tenor")+
  ylab("Average")+
  ylim(c(20,150))

... Now I am this far, but I would like to assign each 18 currency pair with its own linetype and shape, but I get this:

enter image description here

It seems as if there is only 12 linetypes and even fewer shapes is that true?

like image 903
Phillip Sanford Brigham Avatar asked Mar 14 '23 15:03

Phillip Sanford Brigham


1 Answers

Such a large number of linetypes and shapes will probably be confusing to look at, but here's how to access more linetypes and point markers:

Linetypes can be specified by sequences of 2, 4, 6, or 8 hexadecimal digits (1 through 9 and then A through F to represent 10 through 15 (0 is not allowed in linetype codes)). The pattern for these is length of dash then length of gap. So "24" would be a dash of length 2 followed by a gap of length 4. "42C6" would be dash 4 gap 2 dash 12 gap 6, and so on. Below I create 24 different linetype patterns for illustration, but you can of course tailor your patterns as needed.

linetypes = c(apply(expand.grid(c(2,4), c(1,2,4,8,"A")), 1, paste, collapse=""), 
              apply(expand.grid(c(2,4,8), c(2,4), c(5,"F"), 2), 1, paste, collapse=""),
              "4284B4F4", "228F61A4")

R has 26 built-in symbols (see ?pch), 24 of which are used in the plot below. You can access additional symbols using Unicode, as shown in this SO answer.

Here's some fake data to plot. We'll use linetypes as the grouping variable so that the legend will display the linetype code for each of the linetypes.

dat = data.frame(x=rep(1:2, 24), y=rep(1:24, each=2), 
                 group=factor(rep(linetypes, each=2), levels=linetypes))

ggplot(dat, aes(x,y, group=group, linetype=group, shape=group)) + 
  geom_line() +
  geom_point(size=3, colour="blue", fill="red") +
  scale_shape_manual(values=c(0:23)) +
  scale_linetype_manual(values=linetypes) +
  guides(shape=guide_legend(reverse=TRUE),
         linetype=guide_legend(reverse=TRUE)) +
  labs(shape="", linetype="")

enter image description here

These linetypes and point markers are not specific to ggplot2 and can be accessed in base graphics and lattice as well.

like image 162
eipi10 Avatar answered Mar 16 '23 15:03

eipi10