x<-seq(-3,3,by=0.1)
y=0.5*x^3
## Set png size to be slightly bigger than plot to take account of one line margin
png( "~/myplot.png" , width = 600 , height = 2600 ,units="px",res=100)
## Set graphical parameters to control the size of the plot area and the margins
## Set size of plot area to 6 inches wide by 15 inches tall
par( pin = c(6,26) )
## Remove margins around plot area
par( mai = c(0,0,0,0) )
## Remove outer margins around plot area
par( omi = c(0,0,0,0) )
## Set y-axis take use entire width of plot area (6 inches) and to have 7 tick marks (-3,-2,-1,0,1,2,3)
par( xaxp = c(-3,3,7) )
## Set x-axis take use entire height of plot area (26 inches) and to have 27 tick marks (-13,-12,...,0,...11,12,13)
par( yaxp = c(-13,13,27) )
## Create the plot
plot( x , y , type = "l" , frame.plot = FALSE , axes = FALSE )
axis( 1 , pos = 0 , at = seq( -3 , 3 , by = 1 ) , labels = seq( -3 , 3 , by = 1 ) )
axis( 2 , pos = 0 , at = seq( -13 , 13 , by = 1 ) , labels = seq( -13 , 13 , by = 1 ) )
text(0.5,5,expression(f(x)==frac(1,2)*x^3) )
## Turn PNG device off
dev.off()
We can get the graph which is 6 inches width, 26 inches height, but the tick unit in the x-axis or y-axis is not 1 inch; please see the attachment g1.png:
The reason is R will not use the entire space to set the x-axis and y-axis, R keep little space; please see the attachment g2.png:
I can png( "~/myplot.png" , width = 610 , height = 2700 ,units="px",res=100)
, but how can I make the physical length of tick unit just to be 1 inch?
Right-click either a Segments or Gridline component in the tree view and click Add > Ticks. The Ticks component is added to the tree view.
To specify the exact settings you want it is best to output to PDF. The onscreen plot can change when it is re-sized etc. You need to set various graphical parameters to take care of the margins. There is a good outline of the plotting area in base
graphics and the appropriate par
names to adjust here. This code should give you exactly what you asked for:
It looks like the PDF device keeps a border of one line around the outside, no matter what. Be default line height ~ 0.2 inches. Therefore we increase our width and height by 0.5 inches and we get spacing of exactly one inch.
Here is the measurements on my computer!!
## Set pdf size to be slightly bigger than plot to take account of one line margin
pdf( "~/myplot.pdf" , width = 6.5 , height = 15.5 )
## Set graphical parameters to control the size of the plot area and the margins
## Set size of plot area to 6 inches wide by 15 inches tall
par( pin = c(6,15) )
## Remove margins around plot area
par( mai = c(0,0,0,0) )
## Remove outer margins around plot area
par( omi = c(0,0,0,0) )
## Set y-axis take use entire width of plot area (6 inches) and to have 7 tick marks (-3,-2,-1,0,1,2,3)
par( yaxp = c(0,1,7) )
## Set x-axis take use entire height of plot area (15 inches) and to have 27 tick marks (-13,-12,...,0,...11,12,13)
par( xaxp = c(0,1,27) )
## Create the plot
plot( x , y , type = "l" , frame.plot = FALSE , axes = FALSE )
axis( 1 , pos = 0 , at = seq( -3 , 3 , by = 1 ) , labels = seq( -3 , 3 , by = 1 ) )
axis( 2 , pos = 0 , at = seq( -13 , 13 , by = 1 ) , labels = seq( -13 , 13 , by = 1 ) )
text(0.5,5,expression(f(x)==frac(1,2)*x^3) )
## Turn PDF device off
dev.off()
And the results:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With