2016-05-27 6 views
2

質問はこれに関連しています:Line graph customization (add circles, colors)しかし、私は新しい仕事を得たので、私は新しい質問を作成しました。ggplotのfacet_wrapで回線を接続してください

私のデータフレームは、私がリンクに投稿した質問と同じです。 @beetroot

value <- c(9, 4, 10, 7, 10, 
      10, 10, 4, 10, 
      4, 10, 2, 5, 5, 4) 

names <- c("a","b", 
      "c","d","e", 
      "f", "g","h", 
      "i","j","k","l", 
      "m","n","p") 

df <- data.frame(value, names) 
df$names <- as.character(df$names) 
df$part <- rep(c("part3", "part2", "part1"), each = 5) 

library(dplyr) 
library(tidyr) 

df2 <- df %>% 
    group_by(part, names) %>% 
    expand(value = min(df$value):max(df$value)) 

p <- ggplot() + 
    geom_point(data = df2, aes(x = value, y = names), 
      shape = 1) + 
    geom_point(data = df, aes(y = names, x = value, group = 1), 
      colour = I("red"), shape = 21, lwd = 3, fill = "red") + 
    geom_line(data = df, aes(y = names, x = value, group = 1), 
      group = I(1),color = I("red")) + 
    theme_bw() + 
    facet_wrap(~part, ncol = 1, scales = "free_y") 

p + theme(strip.background = element_rect(fill="dodgerblue3"), 
      strip.text.x = element_text(colour = "white"))+xlab("") +ylab("") 


    df <- data.frame(value, names) 
    df$names <- as.character(df$names) 

によって私に与えられた以下のコードと(私自身の変更のほとんどない)で、私はこの出力を得る:

enter image description here

をしかし、今、私は(通過ラインを接続したいですPART1、PART2とPART3)私の出力は次のようになりますように:

enter image description here

私は線の黒い色を使用しました。私はこの部分を線で接続したいと考えています。

+3

わからない場合はこれと同じ原理で、この可能性のある作品http://stackoverflow.com/questions/31690007/ggplot-drawing -lines-between-points-across-facets – theArun

+7

そのような異なるファセットにデータを接続する場合は、ファセットを使用しないでください。代わりに、長方形とテキストでプロットに注釈を付けることができます。 – Roland

答えて

0

私は完全に満足していませんが、私は解決策を見つけました。私はバウンディングボックスを計算しました。

まず私は私のコードは次のようになりfacet_wrap(~part, ncol = 1, scales = "free_y")を削除:

p <- ggplot() + 
    geom_point(data = df2, aes(x = value, y = names), 
      shape = 1) + 
    geom_point(data = df, aes(y = names, x = value, group = 1), 
      colour = I("red"), shape = 21, lwd = 3, fill = "red") + 
    geom_line(data = df, aes(y = names, x = value, group = 1), 
      group = I(1),color = I("red")) + 
    theme_bw() 

その後トリックがデータフレームを作成し、直接テキストの幅と高さを追加しました:

# PART 1 
TextFrame <- data.frame(X = 6, Y = 15.5, LAB = "PART 1") 
TextFrame <- transform(TextFrame, 
         w = strwidth(LAB, 'inches') + 8, 
         h = strheight(LAB, 'inches') + 0.3 
) 


# PART 2 
TextFrame.1 <- data.frame(X = 6, Y = 10.5, LAB = "PART 2") 
TextFrame.1 <- transform(TextFrame.1, 
         w = strwidth(LAB, 'inches') + 8, 
         h = strheight(LAB, 'inches') + 0.3 
) 
# PART 3 
TextFrame.2 <- data.frame(X = 6, Y = 4.5, LAB = "PART 3") 
TextFrame.2 <- transform(TextFrame.2, 
         w = strwidth(LAB, 'inches') + 8, 
         h = strheight(LAB, 'inches') + 0.3 
) 

それから私は」私は後に錯覚を作成するためにgeom_rectgeom_textを使用しました。

p + geom_rect(data = TextFrame, aes(xmin = X - w/2, xmax = X + w/2, 
            ymin = Y - h/2, ymax = Y + h/2), fill = "dodgerblue3") + 
    geom_text(data = TextFrame,aes(x = X, y = Y, label = LAB), size = 5) + 

    geom_rect(data = TextFrame.1, aes(xmin = X - w/2, xmax = X + w/2, 
            ymin = Y - h/2, ymax = Y + h/2), fill = "dodgerblue3") + 
    geom_text(data = TextFrame.1,aes(x = X, y = Y, label = LAB), size = 5) + 

    geom_rect(data = TextFrame.2, aes(xmin = X - w/2, xmax = X + w/2, 
            ymin = Y - h/2, ymax = Y + h/2), fill = "dodgerblue3") + 
    geom_text(data = TextFrame.2,aes(x = X, y = Y, label = LAB), size = 5) 

そして出力は次のようになります。

enter image description here

関連する問題