简体   繁体   English

在特定的y轴值上绘制更长的刻度线

[英]Plotting longer tick marks at particular y-axis values

I am plotting rate vs. year by size. 我正在按比例绘制速率与年份。 This is my data: 这是我的数据:

size    year    rate    se  lci uci count   pop
<=1 1983    0.53    0.05    0.43    0.64    110 21,998,396
<=1 1984    0.53    0.05    0.44    0.65    114 22,197,735
<=1 1985    0.63    0.06    0.52    0.74    135 22,423,982
<=1 1986    0.62    0.06    0.51    0.74    132 22,644,373
<=1 1987    0.6 0.05    0.5 0.71    131 22,872,669
<=1 1988    0.58    0.05    0.48    0.69    130 23,111,066
<=1 1989    0.63    0.05    0.53    0.74    140 23,349,445
<=1 1990    0.82    0.06    0.7 0.95    185 23,657,474
<=1 1991    0.79    0.06    0.68    0.92    186 23,998,620
<=1 1992    0.94    0.06    0.82    1.07    227 24,368,037
<=1 1993    0.88    0.06    0.77    1.01    214 24,713,120
<=1 1994    1.03    0.07    0.91    1.17    253 25,022,087
<=1 1995    1.17    0.07    1.04    1.32    293 25,339,972
<=1 1996    1.22    0.07    1.09    1.37    310 25,652,964
<=1 1997    1.32    0.07    1.18    1.46    337 25,969,420
<=1 1998    1.47    0.08    1.32    1.62    383 26,263,552
<=1 1999    1.71    0.08    1.56    1.88    453 26,520,657
<=1 2000    1.69    0.08    1.53    1.85    451 26,787,544
<=1 2001    2.11    0.09    1.94    2.29    573 27,018,187
<=1 2002    2.36    0.09    2.18    2.55    643 27,165,850
<=1 2003    2.61    0.1 2.43    2.81    720 27,298,693
<=1 2004    2.91    0.1 2.71    3.12    813 27,458,357
<=1 2005    3.48    0.11    3.27    3.71    984 27,662,860
<=1 2006    3.49    0.11    3.28    3.71    1,010   27,954,662
<=1 2007    4.26    0.12    4.02    4.5 1,234   28,212,877
<=1 2008    4.44    0.12    4.2 4.69    1,303   28,496,587
<=1 2009    4.92    0.13    4.67    5.18    1,465   28,777,105
<=1 2010    4.87    0.13    4.62    5.13    1,462   29,014,912
<=1 2011    5.09    0.13    4.84    5.36    1,570   29,276,092
<=1 2012    5.23    0.13    4.97    5.5 1,612   29,546,129
<=1 2013    5.39    0.13    5.13    5.66    1,684   29,806,864
(1,2]   1983    0.87    0.07    0.75    1.01    190 21,998,396
(1,2]   1984    0.92    0.07    0.8 1.06    202 22,197,735
(1,2]   1985    1.06    0.07    0.93    1.21    235 22,423,982
(1,2]   1986    1   0.07    0.87    1.15    224 22,644,373
(1,2]   1987    1.04    0.07    0.91    1.19    234 22,872,669
(1,2]   1988    1.08    0.07    0.95    1.23    245 23,111,066
(1,2]   1989    1.18    0.07    1.04    1.33    280 23,349,445
(1,2]   1990    1.13    0.07    0.99    1.27    260 23,657,474
(1,2]   1991    1.22    0.07    1.08    1.37    291 23,998,620
(1,2]   1992    1.16    0.07    1.03    1.31    282 24,368,037
(1,2]   1993    1.12    0.07    0.99    1.27    274 24,713,120
(1,2]   1994    1.17    0.07    1.04    1.32    291 25,022,087
(1,2]   1995    1.33    0.07    1.19    1.48    342 25,339,972
(1,2]   1996    1.29    0.07    1.15    1.44    334 25,652,964
(1,2]   1997    1.47    0.08    1.32    1.62    381 25,969,420
(1,2]   1998    1.57    0.08    1.43    1.73    418 26,263,552
(1,2]   1999    1.41    0.07    1.27    1.56    375 26,520,657
(1,2]   2000    1.68    0.08    1.53    1.84    453 26,787,544
(1,2]   2001    1.75    0.08    1.6 1.92    478 27,018,187
(1,2]   2002    2.12    0.09    1.96    2.31    580 27,165,850
(1,2]   2003    2.18    0.09    2.01    2.36    598 27,298,693
(1,2]   2004    2.31    0.09    2.13    2.49    636 27,458,357
(1,2]   2005    2.5 0.09    2.32    2.69    703 27,662,860
(1,2]   2006    2.78    0.1 2.59    2.99    789 27,954,662
(1,2]   2007    3.02    0.1 2.82    3.23    869 28,212,877
(1,2]   2008    3.18    0.11    2.97    3.39    919 28,496,587
(1,2]   2009    3.64    0.11    3.42    3.86    1,064   28,777,105
(1,2]   2010    3.43    0.11    3.22    3.65    1,022   29,014,912
(1,2]   2011    3.87    0.12    3.64    4.1 1,147   29,276,092
(1,2]   2012    3.78    0.11    3.56    4   1,157   29,546,129
(1,2]   2013    3.92    0.12    3.7 4.15    1,204   29,806,864
(2,4]   1983    0.8 0.06    0.68    0.93    177 21,998,396
(2,4]   1984    0.81    0.06    0.69    0.94    179 22,197,735
(2,4]   1985    0.87    0.06    0.75    1   198 22,423,982
(2,4]   1986    0.99    0.07    0.86    1.14    216 22,644,373
(2,4]   1987    0.87    0.06    0.75    1.01    195 22,872,669
(2,4]   1988    0.78    0.06    0.67    0.91    184 23,111,066
(2,4]   1989    0.98    0.07    0.86    1.12    226 23,349,445
(2,4]   1990    1.02    0.07    0.89    1.16    236 23,657,474
(2,4]   1991    1.04    0.07    0.91    1.18    250 23,998,620
(2,4]   1992    1.04    0.07    0.92    1.18    256 24,368,037
(2,4]   1993    1.04    0.07    0.92    1.18    259 24,713,120
(2,4]   1994    1   0.06    0.88    1.14    251 25,022,087
(2,4]   1995    1.02    0.06    0.9 1.15    259 25,339,972
(2,4]   1996    1.22    0.07    1.09    1.37    316 25,652,964
(2,4]   1997    1.2 0.07    1.07    1.35    313 25,969,420
(2,4]   1998    1.26    0.07    1.13    1.41    335 26,263,552
(2,4]   1999    1.34    0.07    1.21    1.49    356 26,520,657
(2,4]   2000    1.38    0.07    1.24    1.53    372 26,787,544
(2,4]   2001    1.49    0.07    1.35    1.65    406 27,018,187
(2,4]   2002    1.75    0.08    1.59    1.91    479 27,165,850
(2,4]   2003    1.82    0.08    1.67    1.99    500 27,298,693
(2,4]   2004    1.95    0.08    1.79    2.13    542 27,458,357
(2,4]   2005    1.8 0.08    1.64    1.96    500 27,662,860
(2,4]   2006    1.95    0.08    1.79    2.12    557 27,954,662
(2,4]   2007    2.07    0.09    1.91    2.25    593 28,212,877
(2,4]   2008    2.31    0.09    2.13    2.49    668 28,496,587
(2,4]   2009    2.58    0.1 2.4 2.78    750 28,777,105
(2,4]   2010    2.52    0.09    2.34    2.71    741 29,014,912
(2,4]   2011    2.62    0.09    2.44    2.82    785 29,276,092
(2,4]   2012    2.83    0.1 2.65    3.03    857 29,546,129
(2,4]   2013    2.7 0.1 2.51    2.89    816 29,806,864
>4  1983    0.22    0.03    0.16    0.3 48  21,998,396
>4  1984    0.24    0.03    0.17    0.31    51  22,197,735
>4  1985    0.18    0.03    0.12    0.24    37  22,423,982
>4  1986    0.25    0.04    0.18    0.33    51  22,644,373
>4  1987    0.24    0.03    0.18    0.32    56  22,872,669
>4  1988    0.17    0.03    0.12    0.23    38  23,111,066
>4  1989    0.24    0.03    0.18    0.32    54  23,349,445
>4  1990    0.19    0.03    0.14    0.25    44  23,657,474
>4  1991    0.22    0.03    0.16    0.28    51  23,998,620
>4  1992    0.23    0.03    0.17    0.3 55  24,368,037
>4  1993    0.25    0.03    0.19    0.32    60  24,713,120
>4  1994    0.34    0.04    0.27    0.42    84  25,022,087
>4  1995    0.38    0.04    0.31    0.47    95  25,339,972
>4  1996    0.38    0.04    0.31    0.47    98  25,652,964
>4  1997    0.35    0.04    0.28    0.43    90  25,969,420
>4  1998    0.36    0.04    0.29    0.44    92  26,263,552
>4  1999    0.44    0.04    0.36    0.53    115 26,520,657
>4  2000    0.4 0.04    0.33    0.49    107 26,787,544
>4  2001    0.51    0.04    0.42    0.6 135 27,018,187
>4  2002    0.58    0.05    0.5 0.68    158 27,165,850
>4  2003    0.53    0.04    0.45    0.63    145 27,298,693
>4  2004    0.55    0.04    0.47    0.65    153 27,458,357
>4  2005    0.73    0.05    0.63    0.84    201 27,662,860
>4  2006    0.69    0.05    0.6 0.8 196 27,954,662
>4  2007    0.67    0.05    0.58    0.78    191 28,212,877
>4  2008    0.72    0.05    0.63    0.83    210 28,496,587
>4  2009    0.79    0.05    0.69    0.9 230 28,777,105
>4  2010    0.95    0.06    0.84    1.07    283 29,014,912
>4  2011    0.88    0.05    0.78    1   267 29,276,092
>4  2012    0.93    0.06    0.83    1.05    283 29,546,129
>4  2013    0.96    0.06    0.85    1.08    295 29,806,864
Unknown 1983    0.74    0.06    0.63    0.88    155 21,998,396
Unknown 1984    0.91    0.07    0.79    1.06    195 22,197,735
Unknown 1985    0.85    0.07    0.72    0.98    178 22,423,982
Unknown 1986    1.02    0.07    0.88    1.16    218 22,644,373
Unknown 1987    0.81    0.06    0.7 0.95    178 22,872,669
Unknown 1988    0.88    0.06    0.76    1.01    196 23,111,066
Unknown 1989    0.82    0.06    0.71    0.95    187 23,349,445
Unknown 1990    1.05    0.07    0.92    1.19    238 23,657,474
Unknown 1991    0.88    0.06    0.76    1.01    205 23,998,620
Unknown 1992    1.07    0.07    0.94    1.21    256 24,368,037
Unknown 1993    0.97    0.06    0.85    1.11    236 24,713,120
Unknown 1994    1.2 0.07    1.06    1.34    294 25,022,087
Unknown 1995    1.08    0.07    0.96    1.22    268 25,339,972
Unknown 1996    1.01    0.06    0.89    1.14    255 25,652,964
Unknown 1997    1.07    0.06    0.95    1.21    274 25,969,420
Unknown 1998    0.94    0.06    0.82    1.06    244 26,263,552
Unknown 1999    0.98    0.06    0.86    1.11    258 26,520,657
Unknown 2000    1.16    0.07    1.04    1.3 308 26,787,544
Unknown 2001    1.02    0.06    0.9 1.15    275 27,018,187
Unknown 2002    0.98    0.06    0.86    1.1 266 27,165,850
Unknown 2003    0.96    0.06    0.85    1.08    264 27,298,693
Unknown 2004    0.74    0.05    0.65    0.85    206 27,458,357
Unknown 2005    0.66    0.05    0.57    0.76    184 27,662,860
Unknown 2006    0.66    0.05    0.57    0.76    185 27,954,662
Unknown 2007    0.64    0.05    0.55    0.74    186 28,212,877
Unknown 2008    0.62    0.05    0.54    0.72    183 28,496,587
Unknown 2009    0.61    0.05    0.53    0.71    185 28,777,105
Unknown 2010    0.52    0.04    0.44    0.61    155 29,014,912
Unknown 2011    0.55    0.04    0.46    0.64    167 29,276,092
Unknown 2012    0.55    0.04    0.47    0.64    173 29,546,129
Unknown 2013    0.47    0.04    0.4 0.55    149 29,806,864

I am using this code below: 我在下面使用此代码:

library(tidyverse)
library(grid)
library(scales) 

y_breaks <- c(seq(0.01, 0.1, 0.01), seq(0.2, 1, 0.1), seq(2, 10, 1), 20)
y_labels <- y_breaks
y_labels[c(F, rep(T, 8), F, rep(T, 8), F, rep(T, 8), F, F)] <- ''

x_breaks <- seq(1970, 2015, 5)
x_labels <- x_breaks
x_labels[c(F, T)] <- ''

p <- ggplot(data, aes(year, rate)) + geom_point(aes(col = size)) +   
     scale_y_continuous(breaks = y_breaks,
                        labels = y_labels,
                        limits = c(0.01, 20),
                        trans = 'log10',
                        expand = c(0, 0)) +
     scale_x_continuous(name = "Year of Diagnosis", 
                        breaks = x_breaks, 
                        labels = x_labels,
                        limits = c(1970, 2015),
                        expand = c(0, 0)) +
     theme(panel.background = element_blank(),
           axis.line = element_line(colour = "black"),
           panel.grid.minor = element_blank(),
           panel.grid.major = element_blank(),
           )

p

Below the figure I obtain: 在我得到的图下面: 在此处输入图片说明

The y-axis is shown in log10 scale, the y-axis labels were added only to rate values of 0.01, 0.1, 1, 10, and 20. I wish to make the tick marks corresponding to these labels longer than the other y-axis tick marks. y轴以log10刻度显示,仅将y轴标签添加到比率值0.01、0.1、1、10和20。我希望使与这些标签相对应的刻度线比其他y-长轴刻度线。

I noted Sandy Muspratt's answer from ggplot2: Have shorter tick marks for tick marks without labels . 我注意到ggplot2的桑迪· 穆斯普拉特( Sandy Muspratt)的回答:没有标签的刻度线的刻度线较短 I tried to mimick Sandy's method using the code below: 我尝试使用下面的代码来模仿Sandy的方法:

# Edit the plot:
# Change the lengths of the major tick marks

g = ggplotGrob(p)

# Get the x axis
yaxis <- g$grobs[[which(g$layout$name == "axis-l")]]  

# Get the tick marks and tick mark labels   
ticks <- yaxis$children[[2]]

# Get the tick marks
marks = ticks$grobs[[1]]

# Edit the y positions of the end points of the tick marks
# The '6' and the '3' in the code below 
# are the lengths in pts of the major and minor tick marks respectively. 
marks$x = unit.c(unit.c(unit(1, "npc") - unit(6, "pt"), unit(1, "npc"),   
                        rep(unit.c(unit(1, "npc") - unit(3, "pt"), unit(1, "npc")), 8)))

# Put the tick marks back into the plot
ticks$grobs[[1]] = marks
yaxis$children[[2]] = ticks
g$grobs[[which(g$layout$name == "axis-l")]]  = yaxis

# Draw the plot
grid.newpage()
grid.draw(g)

Unfortunately, I still cannot make the desired ticks longer. 不幸的是,我仍然无法使所需的滴答声更长。 I wish to know how to get longer ticks at y-axis values of 0.01, 0.1, 1, 10, and 20. 我想知道如何在y轴值为0.01、0.1、1、10和20时获得更长的滴答声。

If you can refrain from having a label at y=20 there's an easy solution by adding annotation_logticks() . 如果您可以避免在y=20y=20标签,则可以通过添加annotation_logticks()轻松解决。 We can remove the trailing zeros from the labels with formatC . 我们可以使用formatC从标签中删除尾随零。

ggplot(data, aes(year, rate)) + 
  geom_point(aes(col = size)) +   
  scale_y_log10(name = "Rate", 
                labels = formatC,
                limits = c(0.01, 20),
                expand = c(0, 0)) +
  scale_x_continuous(name = "Year of Diagnosis", 
                     breaks = x_breaks, 
                     labels = x_labels,
                     limits = c(1970, 2015),
                     expand = c(0, 0)) +
  annotation_logticks(sides = "l") + 
  theme(panel.background = element_blank(),
        axis.line = element_line(colour = "black"),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank(),
  )

Yielding 屈服

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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