ggplot axis resets with new plot

by Melanie Walling   Last Updated January 11, 2018 22:26 PM

Below is an example code. The first commands display the data that I am plotting with ggplot in R so others can reproduce. Using this data I can create simple plot, using commands stored as plot3, which appears as it should. I then add a line using geom_line(##) function, and display it as plot6 and the plot does not come out. The x-axis gets out of wack even though both datasets have the same x-axis scale (e.g. a distance between 10 and 850 km). I can't explain what is going on. Can anyone please help out.

#CREATE A LEGEND FOR PLOTS BY REGION
mylegend <-scale_fill_manual(name="Regions",
                               labels=c("Alaska","Cascades","Japan",'NewZealand','Taiwan',
                                        'SouthAmerica','CentralAmerica&Mexico'), 
                               values=c("red","orange","brown","green",'blue','purple','magenta'))
mylegend1 <- scale_color_manual(name="Models", labels=c("Brookhaven Model"), 
                                values="black") # this command mannually adds legend for line color

#CREATE A THEME FOR PLOTS - CONTROLS THE GRID LINES AND AXIS AND BACKGROUND
mygrid          <-theme(panel.grid=element_line(colour = "grey50")) #add a grid line[enter image description here][1]
myaxistext      <-theme(axis.text = element_text(colour = "blue",face="bold",size=12)) #axis test
mytext          <-theme(text=element_text(face="bold",size=12)) #changes the axis label and title
myaspectr       <-theme(aspect.ratio=1) #aspect ratio
myplotmargin    <-theme(plot.margin=unit(c(t=.3,l=.3,r=0,b=.3), "in"))#margin
mylegendpostion <-theme(legend.position = "bottom",legend.box.margin=margin(b=1,t=1,l=0,r=0))
     mytheme         <-        mygrid+myaxistext+mytext+myplotmargin+mylegendpostion#+myaspectr+
    pd <- position_dodge(0.1)
    #CREATE PLOTLABELS
    mylabs <- labs(y='Duration (sec)', x='Magnitude')


> myplotdata
    mag  region distance     mean      med      1Qu      3Qu      min      msx
2     7 Region1       10 2.016697 1.988609 1.834191 2.165062 1.285342 3.160286
6     7 Region2       10 1.959938 1.977103 1.822116 2.128624 1.162944 2.787603
10    7 Region3       10 1.940165 1.968510 1.819797 2.075489 1.324712 2.449772
14    7 Region4       10 1.908564 1.919609 1.760254 2.047184 1.041988 2.901011
18    7 Region5       10 1.931521 1.916763 1.797597 2.047337 1.365274 2.542901
22    7 Region6       10 2.285839 2.222084 2.062419 2.487097 1.664761 3.342688
26    7 Region7       10 1.909078 1.917471 1.769166 2.053873 1.082554 2.655798
30    7 Region1       50 2.176460 2.157480 2.025303 2.296825 1.582986 3.216349
34    7 Region2       50 2.194883 2.216898 2.069309 2.336612 1.574835 2.950615
38    7 Region3       50 2.280443 2.293653 2.196550 2.372977 1.873549 2.655728
42    7 Region4       50 2.068062 2.071190 1.948547 2.185840 1.399745 2.952116
46    7 Region5       50 2.110193 2.099534 2.000199 2.204063 1.637354 2.638823
50    7 Region6       50 2.415144 2.357504 2.220218 2.581127 1.894707 3.378637
54    7 Region7       50 2.166025 2.171308 2.044001 2.284603 1.533602 2.773564
58    7 Region1       75 2.263835 2.246427 2.118692 2.371404 1.699186 3.249857
62    7 Region2       75 2.316356 2.345206 2.192581 2.462389 1.679257 3.040467
66    7 Region3       75 2.445180 2.455960 2.373397 2.524133 2.107820 2.765777
70    7 Region4       75 2.155428 2.155899 2.044058 2.260493 1.528039 2.982779
74    7 Region5       75 2.207165 2.199993 2.108331 2.293485 1.776141 2.694414
78    7 Region6       75 2.487297 2.433156 2.313151 2.635789 1.998524 3.410526
82    7 Region7       75 2.296794 2.299738 2.187376 2.410348 1.730531 2.840695
86    7 Region1      100 2.343735 2.330113 2.212508 2.441796 1.798377 3.282278
90    7 Region2      100 2.424027 2.442952 2.299641 2.578748 1.773440 3.122907
94    7 Region3      100 2.586244 2.595503 2.522051 2.653573 2.297492 2.864906
98    7 Region4      100 2.235382 2.230812 2.129472 2.331097 1.641730 3.012530
102   7 Region5      100 2.295367 2.288557 2.205949 2.373748 1.897993 2.747077
106   7 Region6      100 2.554092 2.503243 2.394455 2.692950 2.092569 3.443684
110   7 Region7      100 2.411762 2.424008 2.300908 2.514077 1.864591 2.903601
114   7 Region1      150 2.485753 2.475452 2.373630 2.569508 1.954188 3.344128
118   7 Region2      150 2.608782 2.627275 2.465588 2.767389 1.938759 3.269831
122   7 Region3      150 2.819385 2.825099 2.766769 2.875305 2.594286 3.043642
126   7 Region4      150 2.377608 2.374991 2.282036 2.464388 1.836418 3.069502
130   7 Region5      150 2.451040 2.445586 2.377435 2.517995 2.104552 2.844748
134   7 Region6      150 2.674598 2.629356 2.536608 2.783818 2.257674 3.506872
138   7 Region7      150 2.607296 2.615498 2.502462 2.709090 2.007160 3.068017
142   7 Region1      250 2.718940 2.710955 2.622939 2.793003 2.207711 3.457414
146   7 Region2      250 2.897797 2.912320 2.749391 3.052746 2.204772 3.511429
150   7 Region3      250 3.166551 3.169290 3.130003 3.204048 3.008781 3.332305
154   7 Region4      250 2.611329 2.607924 2.529074 2.692600 2.139347 3.174544
158   7 Region5      250 2.703723 2.695465 2.649336 2.758491 2.421728 3.015423
162   7 Region6      250 2.876930 2.849925 2.763394 2.969700 2.490448 3.622401
166   7 Region7      250 2.909620 2.922999 2.797785 3.027463 2.242821 3.419183
170   7 Region1      350 2.906804 2.902440 2.819372 2.982911 2.409784 3.559162
174   7 Region2      350 3.120830 3.133391 2.970231 3.281828 2.414685 3.705866
178   7 Region3      350 3.423489 3.424481 3.394398 3.452267 3.300948 3.556036
182   7 Region4      350 2.799702 2.800957 2.716422 2.878614 2.371528 3.297404
186   7 Region5      350 2.904873 2.900204 2.852848 2.953770 2.662159 3.161172
190   7 Region6      350 3.043512 3.026072 2.944629 3.142796 2.661909 3.725954
194   7 Region7      350 3.140611 3.153496 3.022415 3.258778 2.433402 3.678568
198   7 Region1      450 3.064357 3.063950 2.982717 3.147694 2.577809 3.651507
202   7 Region2      450 3.302693 3.313688 3.144941 3.468673 2.588088 3.868582
206   7 Region3      450 3.627624 3.628858 3.604238 3.651863 3.526773 3.738750
210   7 Region4      450 2.957692 2.964477 2.879650 3.033752 2.559829 3.410188
214   7 Region5      450 3.072079 3.067619 3.025654 3.115476 2.855842 3.288355
218   7 Region6      450 3.185394 3.173829 3.086160 3.285850 2.799014 3.819784
222   7 Region7      450 3.327787 3.341211 3.202191 3.448051 2.593410 3.884341
226   7 Region1      550 3.200135 3.202007 3.123185 3.285205 2.718515 3.736040
230   7 Region2      550 3.456324 3.468426 3.297103 3.625050 2.735820 4.008490
234   7 Region3      550 3.797027 3.797876 3.776796 3.819452 3.710878 3.895905
238   7 Region4      550 3.093838 3.103620 3.013845 3.170663 2.718230 3.511531
242   7 Region5      550 3.215197 3.213384 3.171830 3.259840 3.013999 3.401172
246   7 Region6      550 3.309105 3.310214 3.214938 3.418151 2.869816 3.905559
250   7 Region7      550 3.485224 3.502892 3.361743 3.606609 2.731312 4.054912
254   7 Region1      650 3.319484 3.324045 3.243246 3.406255 2.823198 3.813982
258   7 Region2      650 3.589359 3.598545 3.425653 3.756988 2.850067 4.149431
262   7 Region3      650 3.941824 3.942028 3.923825 3.962227 3.865281 4.033308
266   7 Region4      650 3.213496 3.225536 3.137987 3.289778 2.821338 3.603543
270   7 Region5      650 3.340319 3.339975 3.298961 3.384242 3.148111 3.509656
274   7 Region6      650 3.418853 3.420577 3.322123 3.530470 2.933986 3.984555
278   7 Region7      650 3.621122 3.640195 3.498949 3.748188 2.852482 4.200584
282   7 Region1      750 3.425982 3.432116 3.348167 3.515505 2.917954 3.886285
286   7 Region2      750 3.706692 3.715142 3.542034 3.877996 2.950528 4.273367
290   7 Region3      750 4.068270 4.068688 4.051516 4.086576 3.995029 4.154092
294   7 Region4      750 3.320255 3.334515 3.246780 3.397037 2.893000 3.687797
298   7 Region5      750 3.451479 3.452336 3.409832 3.493020 3.266345 3.614264
302   7 Region6      750 3.517519 3.524448 3.422858 3.632800 2.994285 4.057765
306   7 Region7      750 3.740688 3.757589 3.618023 3.868630 2.960543 4.327709
310   7 Region1      850 3.522146 3.530860 3.442817 3.609617 3.004503 3.953711
314   7 Region2      850 3.811652 3.818817 3.645706 3.985685 3.041812 4.383623
318   7 Region3      850 4.180500 4.180745 4.163853 4.196783 4.109859 4.261846
322   7 Region4      850 3.416643 3.429025 3.342382 3.496590 2.959868 3.765723
326   7 Region5      850 3.551487 3.552523 3.511386 3.590999 3.372066 3.708957
330   7 Region6      850 3.607164 3.620115 3.509087 3.730432 3.051154 4.125978
334   7 Region7      850 3.847439 3.866501 3.722760 3.978420 3.058058 4.440481
> BHmodelRup
   Disttest BHmodelRupMag6 BHmodelRupMag7 BHmodelRupMag8
1        10       2.536659       6.039898       14.38126
2        50       5.078361       8.581599       16.92296
3        75       6.666924      10.170163       18.51153
4       100       8.255488      11.758726       20.10009
5       150      11.432615      14.935853       23.27722
6       250      17.786869      21.290107       29.63147
7       350      24.141123      27.644361       35.98573
8       450      30.495377      33.998615       42.33998
9       550      36.849631      40.352869       48.69423
10      650      43.203885      46.707123       55.04849
11      750      49.558139      53.061377       61.40274
12      850      55.912393      59.415631       67.75700
> plot3 <- ggplot(myplotdata, aes(x=distance,y=exp(mean), factor=region, group=region,alpha=.01))+
+     geom_line(position=pd)+geom_point(position=pd)+
+     geom_ribbon(aes(ymin=exp(myplotdata$'1Qu'), ymax=exp(myplotdata$`3Qu`), fill=region), position=pd)+
+     mylegend+mylabs+scale_y_log10(limits=c(5,80))+mytheme+labs(title="IntraSlab Duration Vs Distance for Mag 7 ")
> 
> plot3
> plot6 <- ggplot(myplotdata, aes(x=distance,y=exp(mean), factor=region, group=region,alpha=.01))+
+     geom_line(position=pd)+geom_point(position=pd)+
+     geom_ribbon(aes(ymin=exp(myplotdata$'1Qu'), ymax=exp(myplotdata$`3Qu`), fill=region), position=pd)+
+     geom_line(data=BHmodelRup,inherit.aes = FALSE, aes(x=Disttest,y=BHmodelRupMag7, color="Brookhaven Model"),size=2, linetype='dashed')+ #add Brookhaven model, note: color in aes must match 'scale_color_manual' 'label' (see mylegend1)
+     mylegend+mylegend1+scale_alpha(guide = 'none')+
+     mylabs+scale_y_log10(limits=c(5,80))+mytheme+labs(title="IntraSlab Duration Vs Distance for Mag 7 ")

Tags : r ggplot2 axes


Related Questions



Plot multiple columns with different scales

Updated June 27, 2017 07:26 AM

Duplicating y axes

Updated July 19, 2018 23:26 PM

Chart.js -getting decimals in logarithmic axis

Updated February 06, 2018 21:26 PM