R语言 ggplot2:如何在刻度线上结束y轴?

v1uwarro  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(122)

我正在用不同的轴范围绘制几个不同的图,所以这个问题不仅仅适用于我在这里展示的代码。我试图修改刻度线的中断和间隔,但对于一些图,y轴总是在最后一个中断之后继续。但对于一些图,它只是像this一样工作正常。
下面是我当前的绘图和代码。我想在y轴上以60结束:


的数据

# Set nucleotide sequence for x-axis labels
my_labs = c("C", "T", "A", "C", "A", "T", "A", "A", "A", "T", "A", "C", "A", "C", "A", "T", "G", "T", "C", "T", "C", "T", "G", "C", "T", "C", "G", "T", "T", "C", "G", "G", "G", "G", "G", "C", "C", "G", "G", "T", "A", "T", "G", "C", "T", "A", "C", "A", "C", "G", "G", "A", "A", "C", "G", "T", "G", "A", "G", "A", "G", "A", "C", "C", "C", "C", "T", "C", "G", "G", "A", "A", "C", "T", "G", "G", "C", "A", "T", "A", "G", "A", "C", "T", "T", "G", "T", "G", "T", "A", "T", "A", "A", "A", "A", "G", "A", "A", "T")

# Set color of each nucleotide
my_cols = c("Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Red", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Black", "Black", "Black", "Black", "Black", "Black", "Black", "Black")

ggplot(data = miRNA2) + 
  geom_line(mapping = aes(x = Position, y = CPM), colour="red") +
  scale_y_continuous(breaks = seq(0, 60, 10)) +
  ylab("Counts per million") +
  scale_x_continuous(breaks=1:99, labels=my_labs, expand = c(0, 0)) +
  theme(axis.text.x = element_text(color = my_cols, family = "Courier", size = 6),
        panel.grid.minor.x=element_blank(), panel.grid.major.x=element_blank(), panel.grid.minor.y=element_blank(), panel.background = element_blank(),
        axis.line = element_line(colour = "black")) +
xlab("Supercontig_1.420:40270-40368") +
  ggtitle("Sar-Mir-Nov-2") +
  theme(plot.title = element_text(hjust = 0.5))

字符串

xj3cbfub

xj3cbfub1#

只需将limits = c(0,60)expand = c(0,0)添加到scale_y_continuous

scale_y_continuous(breaks = seq(0, 60, 10), 
                   limits = c(0,60), 
                   expand = c(0,0))

字符串

btxsgosb

btxsgosb2#

你是否想设置ylim max,使最后一个断点四舍五入到最接近的10,以及所有的断点都以10为增量?
你可以通过抓取gg对象ggplot_build(gg)$layout$panel_ranges[[1]]$y.range的y范围来获得ggplot的当前ylim,并将ylim max的ceiling取到最接近的10,同样地设置你的breaks。这样你就可以根据图动态地设置限制和breaks。

gg <- ggplot(data = miRNA2) + 
  geom_line(mapping = aes(x = Position, y = CPM), colour="red") +
  scale_y_continuous(breaks = seq(0, 60, 10)) +
  ylab("Counts per million") +
  scale_x_continuous(breaks=1:99, labels=my_labs, expand = c(0, 0)) +
  theme(axis.text.x = element_text(color = my_cols, family = "Courier", size = 6), 
  panel.grid.minor.x=element_blank(), panel.grid.major.x=element_blank(),
  panel.grid.minor.y=element_blank(), panel.background = element_blank(),
  axis.line = element_line(colour = "black")) +
  xlab("Supercontig_1.420:40270-40368") +
  ggtitle("Sar-Mir-Nov-2") +
  theme(plot.title = element_text(hjust = 0.5))

# grab y-range of current gg object
ggplot_build(gg)$layout$panel_ranges[[1]]$y.range
gg_ylim_max <- ggplot_build(gg)$layout$panel_ranges[[1]]$y.range[2]

# decide if you want to ceiling to nearest 10's or 5's
library(plyr)
round_any(gg_ylim_max, 10, f = ceiling) # ceiling 10's
round_any(gg_ylim_max, 5, f = ceiling) # ceiling 5's

字符串
请尝试这个,我没有你的数据,所以我不能告诉它是否为你工作。

gg_ylim_max_round <- round_any(gg_ylim_max, 10, f = ceiling)
gg + scale_y_continuous(breaks = seq(0, gg_ylim_max_round, 10), limits = c(c(0, gg_ylim_max_round)))


为了限制的冗余而编辑,代码现在应该是正确的。

mgdq6dx1

mgdq6dx13#

这将自动设置中断和限制,并确保中断的步长为10乘以1、2或5的某个幂(而不是3或2.5像有时发生的ggplot的自动休息),它绘制的情节线超过面板边界,而不是反之亦然(因此,如果有一条线与y轴的端点具有相同的值,那么这条线的一半不会被切断):

xy=data.frame(x=0:56,y=sample(-3:68,57,replace=T))

candidates=c(sapply(c(1,2,5),\(x)x*10^c(-20:20)))
xstep=candidates[which.min(abs(candidates-max(xy$x)/7))]
xstart=xstep*floor(min(xy$x)/xstep)
xend=xstep*ceiling(max(xy$x)/xstep)
xbreak=seq(xstart,xend,xstep)
ystep=candidates[which.min(abs(candidates-max(xy$y)/5))]
ystart=ystep*floor(min(xy$y)/ystep)
yend=ystep*ceiling(max(xy$y)/ystep)
ybreak=seq(ystart,yend,ystep)

ggplot(xy,aes(x,y))+
geom_hline(yintercept=c(ystart,yend),color="gray50",linewidth=.3,lineend="square")+
geom_vline(xintercept=c(xstart,xend),color="gray50",linewidth=.3,lineend="square")+
geom_line(linewidth=.4)+
labs(x=NULL,y=NULL)+
coord_cartesian(clip="off")+ # don't clip off lines that fall on plot boundaries
scale_x_continuous(limits=c(xstart,xend),breaks=xbreak,expand=c(0,0))+
scale_y_continuous(limits=c(ystart,yend),breaks=ybreak,expand=c(0,0))+
theme(
  axis.text=element_text(size=8,color="black"),
  axis.ticks=element_line(linewidth=.3,color="gray50"),
  axis.ticks.length=unit(.2,"lines"),
  panel.background=element_rect(fill="white"),
  panel.grid=element_blank(),
  plot.margin=margin(.7,.7,.3,.3,"lines")
)

ggsave("1.png",width=5,height=3)

字符串


的数据

相关问题