2013-05-10 26 views
5

ファセットラベルにイタリック体で使用されている単語がいくつか表示されています。私はラベルのための新しい行を作成するために、次のコードを使用します。ファセットラベルの新しい線とイタリック体をggplot2と組み合わせる

levels(length_subject$CONSTRUCTION) <- 
c("THAT \n Extraposed", "THAT \n Post-predicate", "TO \n Extraposed \n for-subject", "TO \n Post-predicate \n for-subject", "THAT \n Extraposed \n that-omission", "THAT \n Post-predicate \n that-omission") 

しかし、私は言葉をしたいし、イタリック体で表示されます「の」「こと」。私は何かを試しました

"TO \n Extraposed \n (italics(for))-subject" 

ビットは動作しません。

これは、プロットがどのように見えるかです:

次のコードを用いて製造さ

enter image description here

:ここ

ggplot(length_subject, aes(x = SUBJECT)) + 
    geom_histogram(binwidth=.6, colour="black", fill="grey") + 
    ylab("Frequency") + 
    xlab("Subject length") + 
    scale_x_discrete(breaks=c(2,4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30)) + # 
    facet_grid(SUBJECT_TYPE~CONSTRUCTION, scales="free_x", space="free") + 
    theme(strip.text.x = element_text(size = 8)) 

は、データの減少変種です:

structure(list(ID = structure(1:86, .Label = c("A05_122_01", 
"A05_253_01", "A05_277_07", "A05_400_01", "A05_99_01", "A06_1076_01", 
"A06_1261_01", "A06_1283_01", "A06_1283_02", "A06_1317_01", "A06_1326_01", 
"A06_1389_01", "A06_1390_01", "A06_1437_01", "A06_1441_02", "A06_1441_03", 
"A06_1442_03", "A06_1456_01", "A06_1461_01", "A06_830_01", "A06_868_01", 
"A06_884_01", "A06_884_03", "A0K_1057_02", "A0K_1144_07", "A0K_1177_01", 
"A0K_1190_03", "A0K_1214_03", "A0K_1216_01", "A0K_950_02", "A0K_986_01", 
"A1A_102_02", "A1A_163_01", "A1A_199_01", "A1A_45_01", "A1A_97_01", 
"A1B_1008_02", "A1B_1013_01", "A1B_1028_02", "A1B_1042_01", "A1B_1064_01", 
"A1B_1126_03", "A1B_1152_01", "A1B_1174_01", "A1B_1271_01", "A1B_997_01", 
"A1J_487_01", "A1J_544_02", "A1J_555_03", "A1J_569_01", "A1J_601_01", 
"A1N_422_04", "A1N_70_02", "A1S_191_01", "A1S_329_01", "A1S_330_01", 
"A1S_465_04", "A1Y_248_01", "A1Y_278_02", "A1Y_292_01", "A1Y_466_01", 
"A1Y_521_01", "A1Y_612_01", "A1Y_634_01", "A26_139_03", "A26_142_01", 
"A26_148_01", "A26_289_01", "A26_345_02", "A26_439_01", "A26_441_02", 
"A26_463_01", "A28_171_01", "A28_244_01", "A28_245_01", "A28_30_01", 
"A28_341_01", "A28_42_01", "A28_494_03", "A2A_301_01", "A2A_396_01", 
"A2A_599_01", "A2A_637_01", "A2A_676_01", "A2E_22_01", "A2E_25_03" 
), class = "factor"), SUBJECT = c(3L, 2L, 6L, 2L, 2L, 1L, 1L, 
1L, 1L, 2L, 4L, 1L, 4L, 2L, 3L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 7L, 1L, 3L, 2L, 2L, 1L, 6L, 7L, 4L, 1L, 5L, 4L, 2L, 9L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 5L, 3L, 4L, 1L, 1L, 1L, 1L, 5L, 
2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 5L, 2L, 1L, 2L, 2L, 1L, 7L, 1L, 
4L, 1L, 2L, 1L, 1L, 3L, 1L, 13L, 2L, 1L, 1L, 1L, 3L, 1L, 1L), 
    CONSTRUCTION = structure(c(1L, 3L, 1L, 1L, 1L, 4L, 4L, 1L, 
    1L, 5L, 5L, 1L, 1L, 5L, 1L, 3L, 5L, 1L, 5L, 4L, 3L, 3L, 1L, 
    5L, 3L, 5L, 1L, 1L, 2L, 3L, 1L, 1L, 3L, 1L, 1L, 1L, 3L, 1L, 
    4L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 4L, 2L, 4L, 1L, 1L, 3L, 2L, 
    5L, 1L, 1L, 1L, 3L, 1L, 1L, 4L, 4L, 3L, 1L, 2L, 3L, 3L, 1L, 
    3L, 1L, 1L, 1L, 6L, 1L, 1L, 2L, 4L, 4L, 3L, 5L, 3L, 3L, 3L, 
    3L, 5L, 1L), .Label = c("THAT_EXT", "THAT_EXT_NT", "THAT_POST", 
    "THAT_POST_NT", "TO_EXT_FOR", "TO_POST_FOR"), class = "factor"), 
    SUBJECT_TYPE = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 
    1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 
    2L, 3L, 1L, 2L, 2L, 3L, 1L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
    1L, 1L, 1L, 2L, 2L, 3L, 2L, 2L, 2L, 3L, 1L, 1L, 2L, 1L, 1L, 
    2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 
    1L, 3L, 3L), .Label = c("NP", "PRO", "PROPER"), class = "factor")), .Names = c("ID", 
"SUBJECT", "CONSTRUCTION", "SUBJECT_TYPE"), class = "data.frame", row.names = c(NA, 
-86L)) 
+0

考えを)追加することを忘れないでください:facet_grid' ''内ラベラー= label_parsed'を試してみてください。しかし、それがあなたの新しい行に何をするのかはわかりません。 – joran

+0

@joran私は、次のエラーメッセージが表示されます:エラーの解析(テキスト= x)::3:5:予期しない ' - ' 2:Extraposed 3:for- ^ – user1504579

+0

@joran投稿を更新します再現可能な例を含む。 – user1504579

答えて

7

斜体を取得するには、で記述されている書式設定が必要です(そして、それが式として解析されるために)。ただし、plotmath構文には改行操作はありません。あなたはatopと同様の何かを得ることができます。あなたの特定の例では、あなたがfacet_gridコールにlabeller=label_parsedを追加し、その後

levels(length_subject$CONSTRUCTION) <- 
    c("atop(textstyle('THAT'),textstyle('Extraposed'))", 
    "atop(textstyle('THAT'),textstyle('Post-predicate'))", 
    "atop(atop(textstyle('TO'),textstyle('Extraposed')),italic('for')*textstyle('-subject'))", 
    "atop(atop(textstyle('TO'),textstyle('Post-predicate')),italic('for')*textstyle('-subject'))", 
    "atop(atop(textstyle('THAT'),textstyle('Extraposed')),italic('that')*textstyle('-omission'))", 
    "atop(atop(textstyle('THAT'),textstyle('Post-predicate')),italic('that')*textstyle('-omission'))") 

とにラベルを設定することができます

ggplot(length_subject, aes(x = SUBJECT)) + 
    geom_histogram(binwidth=.6, colour="black", fill="grey") + 
    ylab("Frequency") + 
    xlab("Subject length") + 
    scale_x_discrete(breaks=c(2,4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30)) + # 
    facet_grid(SUBJECT_TYPE~CONSTRUCTION, scales="free_x", space="free", 
       labeller=label_parsed) + 
    theme(strip.text.x = element_text(size = 8)) 

(間隔の間にそれは完璧ではない

enter image description here

います線は同じではなく、視差はそこにある線が増えるにつれて悪化します)、それが私の唯一の方法です2つの組み合わせ(プロットマス式の改行)が見つかりました。

+0

ありがとうございます。しかし、間隔を修正する方法を見つけることはいいだろう。 – user1504579

4

編集(2016)

新しいファセットラベリングシステムでは、この解決策はもう動作しません。 element_blankから継承してカスタムgrobを作る方法はnow explicitly disabledです。私は、ggplot2、by designで行うことができないことを受け入れ、将来的にいつでも壊れる可能性のある回避策であまりにも多くのエネルギーを無駄にしないことを教訓と考えています。


オリジナル答え

あなたはテーマ設定に配置するために、適切なカスタムelementを作成しようとすることができます。テーマのデザインは、それは非常に簡単にはありません、残念ながら、

require(ggplot2) 
require(gridExtra) # tableGrob 

element_grob.element_custom <- function(element, label="", ...) { 

    mytheme <- ttheme_minimal(core = list(fg_params = list(parse=TRUE))) 
    disect <- strsplit(label, "\\n")[[1]] 

    g1 <- tableGrob(as.matrix(disect), theme=mytheme) 
    # wrapping into a gTree only because grobHeight.gtable would be too tight 
    # cf. absolute.units() squashing textGrobs 
    gTree(children=gList(g1), height=sum(g1$heights), 
     cl = "custom_strip") 
} 

# gTrees don't know their size and ggplot would squash it, so give it room 
grobHeight.custom_strip = heightDetails.custom_axis = function(x, ...) 
    x$height 
# silly wrapper to fool ggplot2's inheritance check... 
facet_custom <- function(...){ 
    structure(
    list(...), # this ... information is not used, btw 
    class = c("element_custom","element_blank", "element") # inheritance test workaround 
) 

} 


title <- c("First~line \n italic('wait, a second')", 
      "this~is~boring", 
      "integral(f(x)*dx, a, b)") 

iris2 <- iris 
iris2$Species <- factor(iris$Species, labels=title) 
ggplot(iris2, aes(Sepal.Length, Sepal.Width)) + 
    geom_line() + facet_grid(.~Species) + 
    theme(strip.text.x = facet_custom()) 

enter image description here

0

あなたのいくつかは、間隔を修正する方法を探していたように、私は解決策を発見しました。 は3行(この4を作る)か、いずれかの次の行から最後の行の前atop(scriptscriptstyle("")の行を追加し、その後

関連する問題