简体   繁体   中英

ggplot2 line plot dotted lines and arrows

I'm trying to create a lines plot. I would like to obtain a points joined by lines plot. I would like to have a different line type (dashed or not) based on the method (MET) and different colour line based on the variable (Var). I have observations in 4 times (MD): cd, 15, 30, 45.

I would like to add an arrow from left to right.

Here's my database:

> gra
   id MD        val  MET      Var
1   1 cd   0.000000 1-KM   LRT_DM
2   2 cd   0.000000  CIF   LRT_DM
3   3 15  14.740000 1-KM   LRT_DM
4   4 15  17.050000  CIF   LRT_DM
5   5 30  34.740000 1-KM   LRT_DM
6   6 30  35.230000  CIF   LRT_DM
7   7 45  55.790000 1-KM   LRT_DM
8   8 45  54.550000  CIF   LRT_DM
9   1 cd   0.000000 1-KM  LRT_HTN
10  2 cd   0.000000  CIF  LRT_HTN
11  3 15   6.321839 1-KM  LRT_HTN
12  4 15   6.024096  CIF  LRT_HTN
13  5 30  13.793103 1-KM  LRT_HTN
14  6 30  12.650602  CIF  LRT_HTN
15  7 45  36.781609 1-KM  LRT_HTN
16  8 45  37.951807  CIF  LRT_HTN
17  1 cd   0.000000 1-KM  LGIT_DM
18  2 cd   0.000000  CIF  LGIT_DM
19  3 15  16.270784 1-KM  LGIT_DM
20  4 15  16.206483  CIF  LGIT_DM
21  5 30  33.847981 1-KM  LGIT_DM
22  6 30  33.253301  CIF  LGIT_DM
23  7 45  50.831354 1-KM  LGIT_DM
24  8 45  50.060024  CIF  LGIT_DM
25  1 cd   0.000000 1-KM LGIT_HTN
26  2 cd   0.000000  CIF LGIT_HTN
27  3 15   5.714286 1-KM LGIT_HTN
28  4 15   5.714286  CIF LGIT_HTN
29  5 30  12.380952 1-KM LGIT_HTN
30  6 30  12.380952  CIF LGIT_HTN
31  7 45  36.190476 1-KM LGIT_HTN
32  8 45  36.190476  CIF LGIT_HTN
33  1 cd   0.000000 1-KM  LGITHTN
34  2 cd   0.000000  CIF  LGITHTN
35  3 15  15.217391 1-KM  LGITHTN
36  4 15  15.217391  CIF  LGITHTN
37  5 30  45.652174 1-KM  LGITHTN
38  6 30  45.652174  CIF  LGITHTN
39  7 45 117.391304 1-KM  LGITHTN
40  8 45 117.391304  CIF  LGITHTN
41  1 cd   0.000000 1-KM  LGUT_DM
42  2 cd   0.000000  CIF  LGUT_DM
43  3 15  16.470588 1-KM  LGUT_DM
44  4 15  16.867470  CIF  LGUT_DM
45  5 30  34.117647 1-KM  LGUT_DM
46  6 30  33.734940  CIF  LGUT_DM
47  7 45  51.764706 1-KM  LGUT_DM
48  8 45  50.602410  CIF  LGUT_DM
49  1 cd   0.000000 1-KM LGUT_HTN
50  2 cd   0.000000  CIF LGUT_HTN
51  3 15   5.714286 1-KM LGUT_HTN
52  4 15   5.714286  CIF LGUT_HTN
53  5 30  12.380952 1-KM LGUT_HTN
54  6 30  12.380952  CIF LGUT_HTN
55  7 45  36.190476 1-KM LGUT_HTN
56  8 45  36.190476  CIF LGUT_HTN

Here's my code:

library(ggplot2)
library(grid)
gra$MD<-factor(gra$MD, levels=c("cd", "15", "30", "45"))
gra$MET<-factor(gra$MET, levels=c("1-KM", "CIF"))
gra$Var<-factor(gra$Var, levels=c("LRT_DM", "LRT_HTN","LGIT_DM",    "LGIT_HTN", "LGITHTN",  "LGUT_DM"   ,"LGUT_HTN"))

ggplot(data=gra, aes(x = gra$MD, y = gra$val, colour = gra$Var)) +geom_line(arrow = arrow())+geom_point()

Here's what I obtain: 在此处输入图片说明

Why I don't have lines? How can I add the dashed/non dashed option? I've tried this:

ggplot(gra, aes(x = gra$MD, y = gra$val, colour = gra$Var, group= gra$MET)) + geom_line(aes(linetype=gra$MET))+geom_point()

but it doesn't work.

Thank you

Just map linetype to MET .

Your main problem is that you are trying to create a line plot for a discrete X scale. If you still want to retain "cd" as a label on the x axis, you can do something like this.

ggplot(gra, aes(x = as.integer(MD), y = val, colour = Var, linetype = MET)) +
  geom_line(arrow = arrow()) +
  geom_point() +
  scale_x_continuous(labels = levels(gra$MD))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM