2017-02-27 108 views
0

我用不同的轴范围做了几个不同的图,所以这个问题不适用于我在这里展示的代码。我尝试修改刻度线的间隔和间隔,但对于某些图,y轴始终在最后一次中断后继续。但对于一些它只是工作得很好,如thisggplot2:如何在刻度线上结束y轴?

这是我目前的情节和代码。我想结束y轴带勾号上60:

enter image description here

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

中断和限制是独立的,尝试'scale_y_continuous(breaks = seq(0,60,10),limits = c(0,60))'。下次尝试添加一个最小且可重现的示例。 – Axeman

回答

1

只需添加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)) 
2

你要设置ylim最大值,以便显示的最后一个中间值四舍五入到最接近的10?以及所有的休息以10为增量?

您可以通过抓住gg对象的y范围ggplot_build(gg)$layout$panel_ranges[[1]]$y.range获取ggplot的当前ylim,并将ylim max的上限设置为最接近的10,并同样设置您的休息时间。这样,您可以根据情节动态设置限制和中断。

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

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