ggplot编辑中轴交替标记的长度:主要和次要标记 [英] Alternating length of axis tick marks in ggplot edit: major and minor tick marks

查看:453
本文介绍了ggplot编辑中轴交替标记的长度:主要和次要标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

 #create data frame假数值
set.seed(1)
图< - data.frame(installation = rep(sample(seq(1,100,1),10),each = 10),
age = as.vector(replicate(10,sample(seq(1,50,1),10))))

#将安装设置为因子,在第一次测量时按年龄排序$ b $ (安装),总结,最新=最小(年龄))
odr < - odr [订单(odr $最新),]
绘制$ installation< - 因子(绘制$ installation,levels = rev(as.numeric(as.character(odr $ installation))))
rm(odr)

#绘制可用数据
ggplot(plot,aes(installation,age))+
geom_point()+
coord_flip()


<我实际上有大约60个安装和每个标签,所以我吨变得拥挤。通过错开每隔一个y轴勾出一点点时间,我可以使用更大的字体作为标签。这是我希望能够得到答案的问题。



我试着单独绘制偶数和奇数因素,然后让我摆弄每个轴标记,但订单被搞砸了,我不知道为什么。如果有一种方法可以获得轴线滴答效应,我可以通过另一种方式来结束这种方法。

  #将数据框分成奇数和偶数因素
赔率< - 图[as.numeric(图$安装)%% 2!= 0,]
平均值< - 图[as.numeric (绘制$安装)%% 2 == 0,]

#尝试并单独绘制赔率和平均值
ggplot(赔率,aes(安装,年龄))+
geom_point ()+
coord_flip()+
geom_point(data = evens,aes(installation,age))



感谢!

解决方案

好的,通过上面的jhoward和这个问题

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



使用上面的数据集:

 #base plot 
base < - ggplot(plot,aes(age,installation))+
geom_point()+
scale_y_discrete(breaks = levels(绘图$ 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))

#在每个其他方面添加刻度标记
for(i in 1:length(plots $ installation)){
if(as。
ase = base + annotation_custom(grob = linesGrob(gp = gpar(col =dark gray)),
ymin = as.numeric(绘图$ installation [i]),
ymax = as.numeric(绘图$ installation [i]),
xmin = -1.5,
xmax = 0)
}
}

#将标签添加到每个其他分面水平
中(i in 1:length(绘制$安装)){
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(图表$ installation [i]),
ymax = as.numeric(图表$安装
xmin = -2.5,
xmax = -2.5)
}
}

#创建绘图
gt < - ggplot_gtable(ggplot_build(base))
gt $ layout $ clip [gt $ layout $ name ==panel]< - off
grid.draw(gt)


I am creating a plot showing available data for experimental installations using ggplot. My problem is that the y-axis becomes too crowded, so I would like to have every other tick mark be longer, allowing me to use a larger font for the axis labels.

My goal is to plot the field installation number versus age at measurement, showing all of the available data, and sorted by the age at first measurement. Here is an example using pseudo-data. Note that the plotting order of the installations on the y-axis is based on the age at first measurement.

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

I've actually got about 60 installations and a label for each, so it gets crowded. By staggering every other y-axis tick out a little longer I can use a larger font for the labels. This is the question I am hoping to get answered.

I tried plotting the even and odd factors separately, which would then allow me to fiddle with the axis marks for each, but the ordering got screwed and I'm not sure why. If there is a way to get the axis tick effect I'm after another way I'm not married to this approach.

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

Thanks!

解决方案

Ok, got this figured out with help from jhoward above and this question.

The trick is to plot the minor tick marks in the original plot, then add the major tick marks using annotation_custom.

Using the dataset from above:

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

这篇关于ggplot编辑中轴交替标记的长度:主要和次要标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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