【问题标题】:Alternating length of axis tick marks in ggplot edit: major and minor tick marksggplot编辑中轴刻度线的交替长度:主要和次要刻度线
【发布时间】:2014-01-02 15:47:49
【问题描述】:

我正在创建一个图表,显示使用 ggplot 进行实验安装的可用数据。我的问题是 y 轴变得太拥挤了,所以我希望每隔一个刻度线都更长,这样我就可以为轴标签使用更大的字体。

我的目标是绘制现场安装数量与测量年龄的关系,显示所有可用数据,并按首次测量年龄排序。这是使用伪数据的示例。请注意,y 轴上的装置的绘制顺序是基于第一次测量的年龄。

# create data frame of fake values
set.seed(1)
plots <- data.frame(installation=rep(sample(seq(1,100,1), 10), each=10),
                    age=as.vector(replicate(10, sample(seq(1,50,1), 10))))

# set up installations as factor, sorted by age at first measurement
odr <- ddply(plots, .(installation), summarize, youngest = min(age))
odr <- odr[order(odr$youngest),]
plots$installation <- factor(plots$installation, levels=rev(as.numeric(as.character(odr$installation))))
rm(odr)

# plot the available data
ggplot(plots, aes(installation, age)) + 
  geom_point() +
  coord_flip() 

我实际上有大约 60 个安装和一个标签,所以它变得拥挤。通过将每个其他 y 轴错开一点,我可以为标签使用更大的字体。这是我希望得到解答的问题。

我尝试分别绘制偶数和奇数因子,这样我就可以调整每个因子的轴标记,但是顺序搞砸了,我不知道为什么。如果有一种方法可以获得轴刻度效果,我会追求另一种方法,我不会接受这种方法。

# break up the data frame into odd and even factors
odds <- plots[as.numeric(plots$installation) %% 2 != 0,]
evens <- plots[as.numeric(plots$installation) %% 2 == 0,]

# try and plot odds and evens seperately
ggplot(odds, aes(installation, age)) + 
  geom_point() +
  coord_flip() +
  geom_point(data = evens, aes(installation, age))

【问题讨论】:

标签: r ggplot2 axis-labels factors


【解决方案1】:

好的,在上面的 jhoward 和 this question 的帮助下解决了这个问题。

诀窍是在原始图中绘制次要刻度线,然后使用 annotation_custom 添加主要刻度线。

使用上面的数据集:

# base plot
base <- ggplot(plots, aes(age,installation)) +
  geom_point() +
  scale_y_discrete(breaks=levels(plots$installation)[c(2,4,6,8,10)]) +
  scale_x_continuous(expand=c(0,1)) +
  theme(axis.text=element_text(size=10),
        axis.title.y=element_text(vjust=0.1))

# add the tick marks at every other facet level
for (i in 1:length(plots$installation)) {
  if(as.numeric(plots$installation[i]) %% 2 != 0) {
    base = base + annotation_custom(grob = linesGrob(gp=gpar(col= "dark grey")),  
                              ymin = as.numeric(plots$installation[i]), 
                              ymax = as.numeric(plots$installation[i]), 
                              xmin = -1.5, 
                              xmax = 0)
  }
}

# add the labels at every other facet level
for (i in 1:length(plots$installation)) {
  if(as.numeric(plots$installation[i]) %% 2 != 0) {
    base = base + annotation_custom(grob = textGrob(label = plots$installation[i], 
                                                    gp=gpar(col= "dark grey", fontsize=10)),  
                                    ymin = as.numeric(plots$installation[i]), 
                                    ymax = as.numeric(plots$installation[i]), 
                                    xmin = -2.5, 
                                    xmax = -2.5)
  }
}

# create the plot
gt <- ggplot_gtable(ggplot_build(base))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)

【讨论】:

    【解决方案2】:

    这样的东西会标记每隔一个刻度:

     ggplot(plots, aes(age,installation))+
       geom_point()+
       scale_y_discrete(breaks=levels(plots$installation)[c(2,4,6,8,10)])
    

    这适用于一般情况:

    lvls <- levels(plots$installation)
    brks <- 2*(1:(length(lvls)/2)) 
    ggplot(plots, aes(age,installation))+
      geom_point()+
      scale_y_discrete(breaks=levels(plots$installation)[brks])
    

    【讨论】:

    • 嗯..这是在正确的轨道上。如果我能找到一种方法为带有更长刻度线的几率添加第二个 y 刻度,我会被设置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多