以对数刻度绘制y轴的较长刻度线,刻度线朝外 [英] Plotting longer ticks for y-axis at logarithmic scale with ticks pointing outward

查看:74
本文介绍了以对数刻度绘制y轴的较长刻度线,刻度线朝外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前有一篇文章,关于在特定的y轴值处绘制更长的刻度(

I have a previous post regarding ploting longer ticks at particular y-axis values (plotting longer ticks at particular y-axis values).

我的y轴是对数刻度.y值分别标记为0.01、0.1、1、10和20.我还希望与这些中断相对应的报价比其他报价更长. jaySF建议使用annotation_logticks,但是,刻度线出现在绘图区域内,我希望刻度线在外部.

My y-axis is at log-scale.y values were labeled at 0.01, 0.1, 1, 10, and 20. I also want ticks corresponding to these breaks longer than other ticks. jaySF suggested using annotation_logticks, however, the ticks appear inside the plotting area and I want the ticks to be outside.

Sandy Muspratt从> ggplot2:刻度线较短的答案没有标签的刻度线标记似乎有帮助,但是我尝试了类似的代码,但未成功.我之前的 post 中可以看到我遵循Sandy方法的代码.

Sandy Muspratt's answer from ggplot2: Have shorter tick marks for tick marks without labels seems to help, but I tried similar code and did not succeed. My code following Sandy's method can be seen in my previous post.

我想知道我应该如何遵循桑迪的方法,或者使用一些新方法来使0.01、0.1、1、10和20的刻度线变长,图中的其他内容都没有改变.

I wish to know either how should I follow Sandy's method, or use some new way, to make ticks at 0.01, 0.1, 1, 10, and 20 longer, which nothing else in the graph changes.

推荐答案

您的代码(来自

Your code (from here) was close. ticks contains two grobs: the tick marks and the tick mark labels. On the x-axis, the tick marks come first, followed by the labels; but on the y-axis, the the labels come first, followed by the tick marks. Thus to select the tick marks on the y-axis, you need: marks = ticks$grobs[[2]]

此外,长和短刻度线的顺序并不像您所跟随的示例中那样简单.特别是最后两个刻度线很长.在下面的代码中,我列出了所有刻度线的起点和终点的x坐标. (可能有一些方法可以使用ggplot_build信息来自动执行此步骤,但是如果您的绘图是一次性的,则手动设置x坐标同样容易.)

In addition, the sequence of long and short tick marks is not as simple as in the example you followed; in particular, the last two tick marks are long. In the code below, I list the x-coordinates of start and end points of all the tick marks. (There is probably some way to automate this step using the ggplot_build information, but if your plot is a one-off, it is just as easy to set the x-coordinates manually.)

此外,我将刻度线标签向左移动一点. (您的数据在帖子的最后.)

Also, I would move the tick mark labels a little to the left. (Your data is at the very end of the post.)

library(ggplot2)
library(scales)
library(grid)


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(),
           # Move the tick mark labels a little to the left
           axis.text.y = element_text(margin = margin(r = 5, unit = "pt")) 
           )

p

# Get the ggplot grob
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[[2]]

# Edit the x positions of the end points of the tick marks
# The '5.5' and the '2.75' in the code below 
# are the lengths in pts of the major and minor tick marks respectively. 
marks$x = unit.c(rep(
                     unit.c(unit(1, "npc") - unit(5.5, "pt"), unit(1, "npc"),   # 1st long tick mark
                     rep(unit.c(unit(1, "npc") - unit(2.75, "pt"), unit(1, "npc")), 8)), # 8 shorter tick marks
                 3), # repeat that sequence of tick marks 3 times
             rep(unit.c(unit(1, "npc") - unit(5.5, "pt"), unit(1, "npc")), 2)) # top 2 long tick marks

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

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







但是,知道我现在所知道的,我不推荐这种方法.我认为很难找到从列表中的列表序列到适当插槽的路径.我建议使用grid编辑工具进行此编辑.

However, knowing what I now know, I would not recommend this method. I think it is too difficult finding the path through the sequence of lists within lists to the appropriate slots. I would recommend using grid editing tools for this edit.

# Get the ggplot grob
g= ggplotGrob(p)

# Get a list of the grobs that make up the plot
grid.ls(grid.force(g))

寻找与左轴有关的grob及其子代.
这是相关的序列(尾随数字可能有所不同):

Look for a grob and its children that have something to do with the left axis.
This is the relevant sequence (the trailing numbers might differ):

axis-l.7-4-7-4
    axis.line.y.left..polyline.106
   轴
     轴.1-1-1-1
         GRID.text.102
     轴.1-2-1-2

axis-l.7-4-7-4
    axis.line.y.left..polyline.106
    axis
       axis.1-1-1-1
          GRID.text.102
       axis.1-2-1-2

axis.1-2-1-2是刻度线grob; GRID.text.102是刻度标记标签grob.

axis.1-2-1-2 is the tick marks grob; GRID.text.102 is the tick mark labels grob.

# Edit the x-coordinates of the tick marks
g1 = editGrob(grid.force(g), gPath("axis-l", "axis", "axis.1-2"), grep = TRUE,
     x = unit.c(rep(
                     unit.c(unit(1, "npc") - unit(5.5, "pt"), unit(1, "npc"),   # 1st long tick mark
                     rep(unit.c(unit(1, "npc") - unit(2.75, "pt"), unit(1, "npc")), 8)), # 8 shorter tick marks
                 3), # repeat that sequence of tick marks 3 times
             rep(unit.c(unit(1, "npc") - unit(5.5, "pt"), unit(1, "npc")), 2))) # top 2 long tick marks

# Draw the edited plot
grid.newpage()
grid.draw(g1)

您的数据

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

这篇关于以对数刻度绘制y轴的较长刻度线,刻度线朝外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆