Revision | 66d8f97d0eb7dffd173df61cf2ea11e93828d0e1 (tree) |
---|---|
Zeit | 2008-04-28 19:36:04 |
Autor | iselllo |
Commiter | iselllo |
I added the code inset.R which shows how to create and inset plot using
R.
@@ -0,0 +1,187 @@ | ||
1 | +rm(list=ls()) | |
2 | + | |
3 | + | |
4 | +#x<-0:10; | |
5 | +time <- read.table("time_red.dat", header=FALSE) | |
6 | +time <- as.matrix(time) | |
7 | +#y<-x^4; | |
8 | +delta_r<- read.table("delta_r_sq_time.dat", header=FALSE) | |
9 | +delta_r <- as.matrix(delta_r) | |
10 | + | |
11 | + | |
12 | +#now a linear fit | |
13 | + | |
14 | +my_fit <- lm(delta_r[4:length(delta_r)]~ time[4:length(time)]) | |
15 | + | |
16 | +b <- as.real(summary(my_fit)$coefficients[ ,1][2]) | |
17 | + | |
18 | +print ("the slope is, ") | |
19 | + | |
20 | +print (b) | |
21 | + | |
22 | +a <- as.real(summary(my_fit)$coefficients[ ,1][1]) | |
23 | + | |
24 | +print ("the intercept is, ") | |
25 | +print (a) | |
26 | + | |
27 | +fit1 <- a+b*time[4:length(time)] | |
28 | + | |
29 | +#print ("my_fit is, ") | |
30 | +#summary(my_fit) | |
31 | + | |
32 | +time_short<- read.table("time_red_short.dat", header=FALSE) | |
33 | +time_short<- as.matrix(time_short) | |
34 | +#y<-x^4; | |
35 | +delta_r_short<- read.table("delta_r_sq_time_short.dat", header=FALSE) | |
36 | +delta_r_short<- as.matrix(delta_r_short) | |
37 | + | |
38 | + | |
39 | + | |
40 | +time_inset <- c(time_short[2:length(time_short)],time[5:15]) | |
41 | + | |
42 | +delta_r_inset <- c(delta_r_short[2:length(delta_r_short)], delta_r[5:15]) | |
43 | + | |
44 | + | |
45 | + | |
46 | + | |
47 | +my_fit_short<- lm(log10(delta_r_short[2:10])~ log10(time_short[2:10])) | |
48 | + | |
49 | + | |
50 | +b_short<- as.real(summary(my_fit_short)$coefficients[ ,1][2]) | |
51 | + | |
52 | +print ("the slope is, ") | |
53 | + | |
54 | +print (b_short) | |
55 | + | |
56 | +a_short<- as.real(summary(my_fit_short)$coefficients[ ,1][1]) | |
57 | + | |
58 | +print ("the intercept is, ") | |
59 | +print (a_short) | |
60 | + | |
61 | + | |
62 | +#fit_short <- a_short+b_short*log10(time_short[2:length(time_short)]) | |
63 | + | |
64 | + | |
65 | +fit_short <- a_short+b_short*log10(time_inset) | |
66 | + | |
67 | + | |
68 | + | |
69 | + | |
70 | +my_fit_log<- lm(log10(delta_r[4:length(delta_r)])~ log10(time[4:length(time)])) | |
71 | + | |
72 | +b <- as.real(summary(my_fit_log)$coefficients[ ,1][2]) | |
73 | + | |
74 | +print ("the slope is, ") | |
75 | + | |
76 | +print (b) | |
77 | + | |
78 | +a <- as.real(summary(my_fit_log)$coefficients[ ,1][1]) | |
79 | + | |
80 | +print ("the intercept is, ") | |
81 | +print (a) | |
82 | + | |
83 | +#fit_log<- a+b*log10(time_short[2:length(time_short)]) | |
84 | + | |
85 | +fit_log<- a+b*log10(time_inset) | |
86 | + | |
87 | + | |
88 | + | |
89 | +library(gridBase) | |
90 | +#X11(width=8,height=8) | |
91 | + | |
92 | +# produce outer ("main") plot | |
93 | +pdf("inset_plot.pdf") | |
94 | + par( mar = c(4.5,5, 2, 1) + 0.1) | |
95 | + | |
96 | + plot(time,delta_r,col="blue",lwd=1.5,lty=1,pch=1,xlim=range(c(0,400)), | |
97 | + ylim=range(c(0,25)), | |
98 | + xlab=expression("Time"),ylab=expression("<"* delta*""*r ^2 *">"),cex.axis=1.4,cex.lab=1.6) | |
99 | + lines(time[4:length(time)],fit1,col="black",lwd=2.) | |
100 | + | |
101 | +legend(350,10,cex=1.2, c(expression("Simulation"), | |
102 | + | |
103 | + expression("Linear fit"), | |
104 | + expression("Power-law fit")), | |
105 | +lwd=c(2,2,2), lty=c(-1,1,2),pch = c(1,-1,-1),col=c("blue","black","red"),box.lwd=0,box.lty=0, | |
106 | +,xjust = 1, yjust = 1) | |
107 | + | |
108 | + | |
109 | + | |
110 | +vp <- baseViewports() | |
111 | + pushViewport(vp$inner,vp$figure,vp$plot) | |
112 | + | |
113 | +# push viewport that will contain the inset | |
114 | + | |
115 | +# pushViewport(viewport(x=0.1,y=0.9,width=.5,height=.5,just=c("left","top"))) | |
116 | + | |
117 | + pushViewport(viewport(x=-0.11,y=1.06,width=.68,height=.65,just=c(0,1))) | |
118 | + | |
119 | + | |
120 | +# now either define viewport to contain the whole inset figure | |
121 | + | |
122 | +# par(fig=gridFIG(),new=T) # or gridPLT() # ...or just the plotting are (coordinate system) | |
123 | + | |
124 | + par(fig=gridPLT(),new=T) | |
125 | + | |
126 | +# par(plt=gridPLT(),new=T) | |
127 | + | |
128 | + | |
129 | +# draw frame around selected area (for illustration only) | |
130 | + | |
131 | +# grid.rect(gp=gpar(lwd=3,col="red")) | |
132 | + | |
133 | +# plot inset figure | |
134 | + | |
135 | +## plot(time_short,delta_r_short,"b",col="blue",xlab="",ylab="",ylim=range(c(0,max(delta_r_short+0.02))) | |
136 | +## ,yaxt="n",cex.axis=1.4,cex.lab=1.6) | |
137 | +## axis(side=2, at=c( 0, 0.075, 0.15), | |
138 | +## labels=expression(0, 0.075, 0.15),cex.axis=1.4,cex.lab=1.6) | |
139 | + | |
140 | +## plot(time_short[2:length(time_short)],delta_r_short[2:length(delta_r_short)], | |
141 | +## pch=1,col="blue",xlab="",ylab="" | |
142 | +## ,cex.axis=1.4,cex.lab=1.6,log="xy") | |
143 | + | |
144 | + plot(time_inset,delta_r_inset, | |
145 | + pch=1,col="blue",xlab="",ylab="" | |
146 | + ,cex.axis=1.4,cex.lab=1.6,log="xy",xaxt="n",yaxt="n") | |
147 | + axis(side=1, at=c( 0.1,1, 5,15), | |
148 | + labels=expression(0.1,1, 5,15),cex.axis=1.4,cex.lab=1.6) | |
149 | + | |
150 | + axis(side=2, at=c( 0.00002,0.01,0.5), | |
151 | + labels=expression(2%*%10^{-5},10^{-1},0.5),cex.axis=1.4,cex.lab=1.6) | |
152 | + | |
153 | + | |
154 | + | |
155 | +## axis(side=2, at=c( log10(0.01), log10(0.075), log10(0.15)), | |
156 | +## labels=expression(log10(0.01), log10(0.075), log10(0.15)),cex.axis=1.4,cex.lab=1.6) | |
157 | + | |
158 | + | |
159 | +## lines(time_short[2:length(time_short)],10**(fit_short), col="black", lwd=2.) | |
160 | + | |
161 | +## lines(time_short[2:length(time_short)],10**(fit_log), col="red", lwd=2.) | |
162 | + | |
163 | + lines(time_inset,10**(fit_short), col="red", lwd=2.,lty=2) | |
164 | + | |
165 | + lines(time_inset,10**(fit_log), col="black", lwd=2.) | |
166 | + | |
167 | + | |
168 | + | |
169 | + #axis(side=2, at=c( 0, 0.075, 0.15), | |
170 | + #labels=expression(0, 0.075, 0.15),cex.axis=1.4,cex.lab=1.6) | |
171 | + | |
172 | + | |
173 | + | |
174 | + | |
175 | + | |
176 | +# pop all viewports from stack | |
177 | +popViewport(4) | |
178 | + | |
179 | +dev.off() | |
180 | + | |
181 | + | |
182 | +pdf("log-log_plot") | |
183 | + plot(time_short[2:length(time_short)],delta_r_short[2:length(delta_r_short)],"b",col="blue" | |
184 | + ,cex.axis=1.4,cex.lab=1.6,log="xy") | |
185 | +dev.off() | |
186 | + | |
187 | +print ("So far so good") |