【问题标题】:Connect line through facet_wrap in ggplot通过 ggplot 中的 facet_wrap 连接线
【发布时间】:2016-05-27 12:11:25
【问题描述】:

问题与此有关:Line graph customization (add circles, colors),但由于我有一个新任务,所以我创建了一个新问题。

所以我的数据框再次与我在链接中发布的问题相同。使用下面的代码和@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)

我得到这个输出:

但现在我想通过(PART1、PART2 和 PART3)连接线,这样我的 输出 看起来像:

我使用了黑色的线条,这样我想将这些部分与线条连接起来会更明显。

【问题讨论】:

  • 不确定这是否可能与stackoverflow.com/questions/31690007/…的原理相同
  • 如果你想像这样连接不同构面的数据,你不应该使用构面。相反,您可以使用矩形和文本来注释您的绘图。

标签: r ggplot2


【解决方案1】:

虽然我并不完全满意,但我找到了解决方案。我计算了边界框。

首先我删除了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)

输出是:

【讨论】:

    猜你喜欢
    • 2018-05-14
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 2018-06-23
    • 2020-05-11
    • 2021-05-22
    • 2017-02-26
    相关资源
    最近更新 更多