2017-02-27 110 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))) 

編輯冗餘的限制,現在的代碼應該是正確的。