2017-10-24 16 views
1

ない列にfacet_wrapのラベルをベースにする方法 、ラッピングコラム例えば

library(ggplot2) 
df <- data.frame(
    Id = c(1, 1, 2, 3, 3, 3), 
    x = c(1, 2, 2, 1, 3, 4), 
    y = c(1, 2, 1.5, 4, 5, 4.5), 
    Label = c("mean: 1.5", "mean: 1.5", "mean: 1.5", "mean: 4.5", "mean: 4.5", "mean: 4.5") 
) 
ggplot(df, aes(x=x, y=y))+facet_wrap(~Id)+geom_point() 

enter image description here

私は、「ラベル」列のラベルをベースにしたいと思います。この場合、IDは1と2が同じラベルを持つため、私は単にggplot(df, aes(x=x, y=y))+facet_wrap(~Label)+geom_point()を実行することはできません。

私はggplot(df, aes(x=x, y=y))+facet_wrap(~Id, labeller = labeller(setNames(df$Id, df$Label)))+geom_point()を試してみました。

+0

、ID 1と2が同じラベルを持っていません。 – Lyngbakr

+0

@Lyngbakr oops。一定。ありがとう – Ben

答えて

1

はここでダムハックのようなものだ:

dumb_label <- function(df){ 
    list(c("mean: 1.5", "mean: 1.5", "mean: 4.5")) 
} 
ggplot(df, aes(x=x, y=y))+facet_wrap(~Id,labeller = dumb_label)+geom_point() 

これはおそらくより良いアプローチです:あなたがポストされたデータフレームで

ggplot(df, aes(x=x, y=y)) + 
    facet_wrap(~Id,labeller = labeller(Id = c('1' = "mean: 1.5",'2' = "mean: 1.5",'3' = "mean: 4.5"))) + 
    geom_point()