R ggplot编辑中轴记号的交替长度:主记号和次记号
我正在使用ggplot创建一个显示实验装置可用数据的绘图。我的问题是y轴变得太拥挤,所以我希望每隔一个记号标记更长,这样我就可以使用更大的字体作为轴标签 我的目标是绘制现场安装数量与测量时年龄的关系图,显示所有可用数据,并按第一次测量时的年龄排序。下面是一个使用伪数据的示例。请注意,y轴上装置的打印顺序基于首次测量时的年龄R ggplot编辑中轴记号的交替长度:主记号和次记号,r,ggplot2,axis-labels,factors,R,Ggplot2,Axis Labels,Factors,我正在使用ggplot创建一个显示实验装置可用数据的绘图。我的问题是y轴变得太拥挤,所以我希望每隔一个记号标记更长,这样我就可以使用更大的字体作为轴标签 我的目标是绘制现场安装数量与测量时年龄的关系图,显示所有可用数据,并按第一次测量时的年龄排序。下面是一个使用伪数据的示例。请注意,y轴上装置的打印顺序基于首次测量时的年龄 # create data frame of fake values set.seed(1) plots <- data.frame(installation=rep(
# 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()
#创建伪值的数据帧
种子(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])
lvls类似这样的东西会每隔一次标记一次:
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])
lvls好的,在jhoward的帮助下解决了这个问题
诀窍是在原始绘图中绘制次要记号,然后使用注释添加主要记号
使用上面的数据集:
# 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)
#基准图
base好的,在上面和下面的jhoward的帮助下解决了这个问题
诀窍是在原始绘图中绘制次要记号,然后使用注释添加主要记号
使用上面的数据集:
# 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)
#基准图
基地有帮助吗?有帮助吗?嗯…这是正确的。如果我能找到一种方法,为带有较长记号的赔率添加第二个y刻度,我会被设置。嗯……这是正确的。如果我能找到一种方法,为带有较长记号的赔率添加第二个y刻度,我会被设置。