2

The question relates to this: Line graph customization (add circles, colors), but since I got a new task, I created a new question.

So again my data frame is the same as in the question I've posted in a link. With code below and (little of my own modification) that was given to me by @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)

I get this output:

enter image description here

But now I would like to connect lines through (PART1, PART2 and PART3) so that my output would look like:

enter image description here

I used black color of a line just it will be more visible that I would like to connect this parts with lines.

Community
  • 1
  • 1
Miha
  • 2,559
  • 2
  • 19
  • 34
  • 3
    Not sure if this may possible work on the same principle as this http://stackoverflow.com/questions/31690007/ggplot-drawing-line-between-points-across-facets – ArunK May 27 '16 at 12:16
  • 7
    If you want to connect data in different facets like that, you shouldn't use facets. Instead you could annotate your plot with rectangles and text. – Roland May 27 '16 at 12:42

1 Answers1

1

Although I am not completely satisfied I've found solution. I computed the bounding box.

Firstly I removed facet_wrap(~part, ncol = 1, scales = "free_y") so my code looks like this:

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() 

Then the trick was to create data frame and add the width and height of text directly:

# 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
)

Then I've used geom_rectand geom_text to create the illusion I am after.

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)

And the output is:

enter image description here

Miha
  • 2,559
  • 2
  • 19
  • 34