2017-02-19 35 views
1

這可能是一件很明顯,我的思念,但考慮到以下GGPLOT2直方圖:顯示標籤在各條

ggplot(diamonds, aes(carat)) + 
    geom_histogram(color="white") + 
    theme_light() 

enter image description here

我想要的標籤(例如1)柱狀圖的開始在酒吧的開始或結束時,不在中點。

回答

1

它可以自定義每個區間的邊界,通過geom_histogrambreaks參數。如果您想要在紙盒的開始或結束標籤,那麼您只需確保標籤的breaks具有紙盒寬度的倍數(或切割中的步驟):

cut_breaks = seq(0, 5, 0.2) 
lab_breaks = seq(0, 5, 1)   # make sure 1 here is a multiple of 0.2 

ggplot(diamonds, aes(carat)) + 
    geom_histogram(color="white", breaks = cut_breaks) + 
    scale_x_continuous(breaks = lab_breaks) + 
    theme_light() 

enter image description here


下面是一組不同的休息

cut_breaks = seq(0, 5, 0.2) 
lab_breaks = seq(0, 5, 0.6)     # 0.6 is a multiple of 0.2 

ggplot(diamonds, aes(carat)) + 
    geom_histogram(color="white", breaks = cut_breaks) + 
    scale_x_continuous(breaks = lab_breaks) + 
    theme_light() 

enter image description here

1

如果您添加一條帶有以下放置說明的文本行,它會將它們放到您想要它們的位置,但您需要使用要打印的標籤填充該零件。

ggplot(diamonds, aes(carat, label =)) + 
    geom_histogram(color="white") + 
    theme_light()+ 
    geom_text(aes(y = y + 0.05), vjust = 0) 
1

你可以嘗試position_nudge(和其輸入的參數發揮得到期望的結果):

ggplot(diamonds, aes(carat)) + 
    geom_histogram(color="white",position=position_nudge(x=0.25)) + 
    theme_light() 

enter image description here