2015-05-28 27 views
6

Tôi có thể tìm các tùy chọn để căn chỉnh chú thích và nhãn trục trong ggplot, nhưng không phải cho nhãn đánh dấu.Nhãn đánh dấu liên kết bên trái trong R ggplot

Có thể để các nhãn đó không được căn phải vào ô biểu đồ đồ thị hay không, nhưng căn chỉnh trái với nhãn bắt đầu dài nhất hoặc khoảng cách đã đặt từ đường viền đồ thị tổng thể?

Ví dụ:

set.seed(1) 
library(ggplot2) 
axisLabels.x <- c("This is a longer label", 
       "Short label", "Short label","Short label","Short label", 
       "This is again a longer label") 
labels.wrap <- lapply(strwrap(axisLabels.x,50,simplify=F),paste,collapse="\n") # word wrap 
gg <- data.frame(x=LETTERS[1:6], y=sample(1:10,6)) 
ggplot(gg) + 
    geom_bar(aes(x,y, fill=x), stat="identity")+ 
    scale_x_discrete(labels=labels.wrap)+ 
    scale_fill_discrete(guide="none")+ 
    labs(x="",y="Response")+ 
    coord_flip() 

enter image description here


Wanted:

enter image description here

+0

'theme (axis.text.y = element_text (hjust = 0)) ' – user20650

+0

thể trùng lặp của [thay đổi kích thước phông chữ và hướng của văn bản trục trong ggplot2] (http://stackoverflow.com/questions/13297995/changing-font-size-and-direction-of-axes-text-in-ggplot2) – user20650

Trả lời

4

Vì nó cung cấp giải pháp, tôi làm @ bình luận user20650 của một câu trả lời.

set.seed(1) 
library(ggplot2) 
axisLabels.x <- c("This is a longer label", 
        "Short label", "Short label","Short label","Short label", 
        "This is again a longer label") 
labels.wrap <- lapply(strwrap(axisLabels.x,50,simplify=F),paste,collapse="\n") # word wrap 
gg <- data.frame(x=LETTERS[1:6], y=sample(1:10,6)) 
plot <- ggplot(gg) + 
    geom_bar(aes(x,y, fill=x), stat="identity")+ 
    scale_x_discrete(labels=labels.wrap)+ 
    scale_fill_discrete(guide="none")+ 
    labs(x="",y="Response")+ 
    coord_flip() 

Và ở đây chúng tôi đi

plot + theme(axis.text.y = element_text(hjust = 0)) 

left aligned y scale labels

Các vấn đề liên quan