ggplot编辑中轴刻度线的替代长度:主要和次要刻度线

问题描述 投票:1回答:2

我正在创建一个图表,显示使用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() 

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9JVTlKRi5qcGcifQ==” alt =“在此处输入图像描述”>

我实际上有大约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))

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9EZUJFUi5qcGcifQ==” alt =“在此处输入图像描述”>

r ggplot2 axis-labels factors
2个回答
3
投票

[好吧,在上面的空姐和this question的帮助下,找到了答案。

诀窍是在原始图中绘制次要刻度线,然后使用注解_自定义添加主要刻度线。

使用上方的数据集:

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

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9pajJrZi5qcGcifQ==” alt =“在此处输入图像描述”>


1
投票

这样的东西会标记其他所有刻度线:

 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])
© www.soinside.com 2019 - 2024. All rights reserved.