Как установить длину тиков точно в 1 дюйм? - программирование
Подтвердить что ты не робот

Как установить длину тиков точно в 1 дюйм?

 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()

Мы можем получить график шириной 6 дюймов, высотой 26 дюймов, но единица измерения галочки по оси x или оси y не равна 1 дюйму; см. приложение g1.png:

enter image description here

Причина в том, что R не будет использовать все пространство для установки оси x и оси y, R не имеет места; см. приложение g2.png:

enter image description here

Я могу png( "~/myplot.png" , width = 610 , height = 2700 ,units="px",res=100), но как я могу сделать физическую длину блока тика только 1 дюйм?

4b9b3361

Ответ 1

Чтобы указать точные настройки, которые вы хотите, лучше всего выводить в PDF. Экранный экран может измениться при изменении размера и т.д. Вам нужно установить различные графические параметры, чтобы позаботиться о полях. В графике base есть хорошая схема графика и соответствующие имена par для настройки здесь. Этот код должен дать вам именно то, что вы просили:

Изменить

Похоже, что устройство PDF сохраняет границу одной линии снаружи, несмотря ни на что. По умолчанию высота линии ~ 0,2 дюйма. Поэтому мы увеличиваем нашу ширину и высоту на 0,5 дюйма, и мы получаем интервал ровно на один дюйм.

Вот измерения на моем компьютере! enter image description here

## 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()

И результаты:

enter image description here