2014-02-08 98 views
6

我試圖將表達式設置爲ggplot2中facet環境中的x軸文本,並且標籤長度不等。例如:如何將表達式設置爲ggplot2中facets的軸文本?

dat <- structure(list(Species = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), Individual = structure(c(1L, 
2L, 3L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("1", "2", "3", "4", 
"expression(bar(\"x\"))"), class = "factor"), mean = c(45, 32, 
100, 59, 65, 110, 87, 93, 88.75), min = c(34, 20, 89, 47.66666667, 
54, 100, 67, 85, 76.5), max = c(54, 42, 110, 68.66666667, 76, 
120, 99, 105, 100)), .Names = c("Species", "Individual", "mean", 
"min", "max"), class = "data.frame", row.names = c(NA, -9L)) 

require(ggplot2) 

enter image description here

This answer介紹如何做纔不至於方面。我管理設置labels,但我找不到如何爲單獨的每個方面設置標籤:

ggplot(dat, aes(x = Individual, y = mean, ymin = min, ymax = max, color = Species)) + 
    geom_pointrange() + 
    facet_wrap(~Species, scales = "free") + 
    scale_x_discrete(labels = c("1", "2", "3", expression(bar("x")))) + 
    theme_grey(base_size = 18) 

enter image description here

長載體或指定labels列表似乎不工作:

ggplot(dat, aes(x = Individual, y = mean, ymin = min, ymax = max, color = Species)) + 
    geom_pointrange() + 
    facet_wrap(~Species, scales = "free") + 
    scale_x_discrete(labels = c("1", "2", "3", expression(bar("x")), "1", "2", "3", "4", expression(bar("x")))) + 
    theme_grey(base_size = 18) 

enter image description here

ggplot(dat, aes(x = Individual, y = mean, ymin = min, ymax = max, color = Species)) + 
    geom_pointrange() + 
    facet_wrap(~Species, scales = "free") + 
    scale_x_discrete(labels = list(c("1", "2", "3", expression(bar("x")), c("1", "2", "3", "4", expression(bar("x")))))) + 
    theme_grey(base_size = 18) 

enter image description here

有沒有辦法做到這一點ggplot2

+0

在這個例子中,他們是不同的物種和表達(bar(「x」))是個人的平均值(B方面的1:4)。 – Mikko

回答

5

您可以添加斷裂參數scale_x_discrete:

ggplot(dat, aes(x=Individual, y = mean,ymin = min, ymax = max, color=Species)) + 
geom_pointrange() + facet_wrap(~Species, scales = "free_x") + 
scale_x_discrete(breaks=levels(factor(dat$Individual)), 
       labels = c("1", "2", "3", "4", expression(bar("x")))) + 
theme_grey(base_size = 18) 

enter image description here

+0

+1好的解決方案。 –

2

作爲一種變通方法,您可以使用scales = "free_y"

ggplot(dat, aes(x = Individual, y = mean, 
       ymin = min, ymax = max, color = Species)) + 
    geom_pointrange() + facet_wrap(~Species, scales = "free_y") + 
    scale_x_discrete(labels = c("1", "2", "3", "4", expression(bar("x")))) + 
    theme_grey(base_size = 18) 

enter image description here

相關問題