最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Add a dashed line to every facet in ggplot? - Stack Overflow

programmeradmin7浏览0评论

I have this data:

cmf <- data.frame(
  apresid = c("1.0", "2.1", "2.2", "3.1", "3.2", "3.3", "4.0", "5.1", "5.2", "5.3", "MRJ"),
  cmf12 = c(10.7, 7.1, 5.4, 11.3, 10.0, 9.3, 8.9, 10.7, 8.8, 11.4, 10.0),
  cmf13 = c(8.8, 4.9, 7.9, 10.3, 10.7, 9.3, 6.9, 10.4, 8.5, 8.5, 9.3),
  cmf14 = c(10.2, 4.4, 4.1, 10.3, 9.0, 9.7, 7.9, 11.6, 9.3, 10.1, 9.5),
  cmf15 = c(11.0, 6.1, 6.7, 8.7, 9.0, 11.2, 9.1, 9.2, 8.9, 12.8, 9.2),
  cmf16 = c(7.4, 6.2, 6.6, 9.4, 9.1, 8.9, 9.8, 10.8, 6.5, 8.6, 8.7),
  cmf17 = c(10.7, 7.2, 7.0, 9.2, 7.1, 11.5, 8.9, 10.6, 8.8, 7.3, 9.0),
  cmf18 = c(11.6, 5.8, 7.3, 10.1, 7.9, 12.4, 8.8, 11.2, 9.7, 8.9, 9.3),
  cmf19 = c(10.6, 8.9, 11.0, 11.5, 8.5, 10.4, 7.7, 12.5, 10.3, 13.8, 10.9),
  cmf20 = c(13.7, 8.2, 7.3, 12.7, 10.6, 10.6, 7.4, 11.2, 12.0, 12.7, 10.4),
  cmf21 = c(11.7, 8.7, 7.3, 11.7, 9.1, 10.0, 8.4, 11.2, 12.8, 13.5, 10.1),
  cmf22 = c(10.7, 4.0, 9.1, 10.0, 11.9, 10.0, 8.4, 10.3, 9.8, 9.7, 8.4),
  cmf23 = c(9.6, 7.0, 7.0, 9.8, 8.9, 7.9, 6.5, 10.3, 9.8, 9.8, 8.9),
  cmf24 = c(6.5, 4.3, 6.5, 8.7, 9.0, 9.4, 10.2, 8.0, 7.8, 9.2, 8.9),
  cmf25 = c(7.4, 10.6, 15.4, 14.7, 10.6, 10.6, 5.4, 8.0, 7.9, 8.7, 8.6)
)

cmf_long <- cmf %>%
  pivot_longer(cols = starts_with("cmf"), names_to = "ano", values_to = "cmf") %>%
  mutate(ano = as.numeric(gsub("cmf", "", ano)) + 2000) %>%
  mutate(ano = as.character(ano))

And I'm trying to plot a facet plot in ggplot, where each facet is "apresid". But I don't want to create a facet for "MRJ", I just want the MRJ ploted as a dashed line in each facet for every other apresid.

This is my plot:

ggplot(cmf_long, aes(x = ano, y = cmf, group = apresid)) +
  geom_line(color = "red", size = 2) +
  geom_line(data = cmf_long %>% filter(apresid == "MRJ"),
            aes(x = ano, y = cmf),
            linetype = "dashed", color = "black", size = 1) +
  facet_wrap(~apresid, scales = "fixed", ncol = 5) +
  ylim(0, 20) +
  labs(y = "Coeficiente de Mortalidade Fetal/1.000 NV", x = NULL) +
  theme_minimal(base_size = 20) +
  theme(
    strip.background = element_rect(fill = "darkred"),
    strip.text = element_text(color = "white", face = "bold"),
    axis.text.x = element_text(angle = 45, hjust = 1),
    panel.grid.major = element_line(color = "gray", linetype = "dashed", size = 0.5)
  )

I want something like this:

I have this data:

cmf <- data.frame(
  apresid = c("1.0", "2.1", "2.2", "3.1", "3.2", "3.3", "4.0", "5.1", "5.2", "5.3", "MRJ"),
  cmf12 = c(10.7, 7.1, 5.4, 11.3, 10.0, 9.3, 8.9, 10.7, 8.8, 11.4, 10.0),
  cmf13 = c(8.8, 4.9, 7.9, 10.3, 10.7, 9.3, 6.9, 10.4, 8.5, 8.5, 9.3),
  cmf14 = c(10.2, 4.4, 4.1, 10.3, 9.0, 9.7, 7.9, 11.6, 9.3, 10.1, 9.5),
  cmf15 = c(11.0, 6.1, 6.7, 8.7, 9.0, 11.2, 9.1, 9.2, 8.9, 12.8, 9.2),
  cmf16 = c(7.4, 6.2, 6.6, 9.4, 9.1, 8.9, 9.8, 10.8, 6.5, 8.6, 8.7),
  cmf17 = c(10.7, 7.2, 7.0, 9.2, 7.1, 11.5, 8.9, 10.6, 8.8, 7.3, 9.0),
  cmf18 = c(11.6, 5.8, 7.3, 10.1, 7.9, 12.4, 8.8, 11.2, 9.7, 8.9, 9.3),
  cmf19 = c(10.6, 8.9, 11.0, 11.5, 8.5, 10.4, 7.7, 12.5, 10.3, 13.8, 10.9),
  cmf20 = c(13.7, 8.2, 7.3, 12.7, 10.6, 10.6, 7.4, 11.2, 12.0, 12.7, 10.4),
  cmf21 = c(11.7, 8.7, 7.3, 11.7, 9.1, 10.0, 8.4, 11.2, 12.8, 13.5, 10.1),
  cmf22 = c(10.7, 4.0, 9.1, 10.0, 11.9, 10.0, 8.4, 10.3, 9.8, 9.7, 8.4),
  cmf23 = c(9.6, 7.0, 7.0, 9.8, 8.9, 7.9, 6.5, 10.3, 9.8, 9.8, 8.9),
  cmf24 = c(6.5, 4.3, 6.5, 8.7, 9.0, 9.4, 10.2, 8.0, 7.8, 9.2, 8.9),
  cmf25 = c(7.4, 10.6, 15.4, 14.7, 10.6, 10.6, 5.4, 8.0, 7.9, 8.7, 8.6)
)

cmf_long <- cmf %>%
  pivot_longer(cols = starts_with("cmf"), names_to = "ano", values_to = "cmf") %>%
  mutate(ano = as.numeric(gsub("cmf", "", ano)) + 2000) %>%
  mutate(ano = as.character(ano))

And I'm trying to plot a facet plot in ggplot, where each facet is "apresid". But I don't want to create a facet for "MRJ", I just want the MRJ ploted as a dashed line in each facet for every other apresid.

This is my plot:

ggplot(cmf_long, aes(x = ano, y = cmf, group = apresid)) +
  geom_line(color = "red", size = 2) +
  geom_line(data = cmf_long %>% filter(apresid == "MRJ"),
            aes(x = ano, y = cmf),
            linetype = "dashed", color = "black", size = 1) +
  facet_wrap(~apresid, scales = "fixed", ncol = 5) +
  ylim(0, 20) +
  labs(y = "Coeficiente de Mortalidade Fetal/1.000 NV", x = NULL) +
  theme_minimal(base_size = 20) +
  theme(
    strip.background = element_rect(fill = "darkred"),
    strip.text = element_text(color = "white", face = "bold"),
    axis.text.x = element_text(angle = 45, hjust = 1),
    panel.grid.major = element_line(color = "gray", linetype = "dashed", size = 0.5)
  )

I want something like this:

Share Improve this question edited Mar 20 at 20:23 stefan 127k6 gold badges38 silver badges76 bronze badges Recognized by R Language Collective asked Mar 20 at 18:02 Lana MeijinhosLana Meijinhos 1972 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Drop the MRJ category from your global data and drop the facetting variable from the filtered data used to draw the dashed line. Also note that we have to add a group=1 to the second geom_line.

ibrary(tidyverse)

ggplot(
  data = cmf_long %>% filter(apresid != "MRJ"),
  aes(x = ano, y = cmf, group = apresid)
) +
  geom_line(
    color = "red", size = 2
  ) +
  geom_line(
    data = cmf_long %>% filter(apresid == "MRJ") |> select(-apresid),
    aes(x = ano, y = cmf, group = 1),
    linetype = "dashed", color = "black", size = 1
  ) +
  facet_wrap(~apresid, scales = "fixed", ncol = 5) +
  ylim(0, 20) +
  labs(y = "Coeficiente de Mortalidade Fetal/1.000 NV", x = NULL) +
  theme_minimal(base_size = 20) +
  theme(
    strip.background = element_rect(fill = "darkred"),
    strip.text = element_text(color = "white", face = "bold"),
    axis.text.x = element_text(angle = 45, hjust = 1),
    panel.grid.major = element_line(color = "gray", linetype = "dashed", size = 0.5)
  )

发布评论

评论列表(0)

  1. 暂无评论