在 Facet_Wrap 中编辑单个图

问题描述 投票:0回答:1

我在下面附上了一张图表。我正在尝试更改每个facet_wrapped 图的单独线条。

例如,我希望 REGION 11 和 REGION 9 为红线 - 而其余图表应为海军蓝线。

国家线在所有图表中都应保持黑色

当我编写代码时 - 国家线路如我所愿保持黑色,但所有地区线路保持红色。我不知道如何只将 2 条线(区域 11 和区域 9)设为红色,而其余的设为海军蓝色。

谢谢你

# HERE IS MY DATA ###################################################################

example2 = structure(list(Segment = c("Segment1", "Segment1", "Segment1", 
"Segment1", "Segment1", "Segment1", "Segment1", "Segment1", "Segment1", 
"Segment1", "Segment1", "Segment1", "Segment1", "Segment1", "Segment1", 
"Segment1", "Segment1", "Segment1", "Segment1", "Segment1", "Segment1", 
"Segment1", "Segment1", "Segment1", "Segment1", "Segment1", "Segment1", 
"Segment1", "Segment1", "Segment1", "Segment1", "Segment1", "Segment1", 
"Segment1", "Segment1", "Segment1", "Segment1", "Segment1", "Segment1", 
"Segment1", "Segment1", "Segment1", "Segment1", "Segment1", "Segment1", 
"Segment1", "Segment1", "Segment1"), Area = c("Region 1", "Region 2", 
"Region 4", "Region 3", "Region 5", "Region 6", "Region 7", "Region 8", 
"Region 9", "Region 10", "Region 11", "Region 1", "Region 2", 
"Region 4", "Region 3", "Region 5", "Region 6", "Region 7", "Region 8", 
"Region 9", "Region 10", "Region 11", "Region 1", "Region 2", 
"Region 4", "Region 3", "Region 5", "Region 6", "Region 7", "Region 8", 
"Region 9", "Region 10", "Region 11", "Region 1", "Region 2", 
"Region 4", "Region 3", "Region 5", "Region 6", "Region 7", "Region 8", 
"Region 9", "Region 10", "Region 11", "National", "National", 
"National", "National"), Measure.Names = structure(c(2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 3L, 2L, 1L), levels = c("R12M", 
"R6M", "R3M", "R1M"), class = "factor"), Measure.Values = c(0.02, 
0.04, -0.02, 0.07, -0.02, 0.02, 0.04, -0.01, 0.02, 0.03, -0.05, 
0.05, 0.05, -0.03, 0.11, 0.03, 0.06, 0.08, -0.01, -0.01, 0.01, 
-0.07, 0.07, 0.02, 0, 0.05, 0.05, 0.09, 0.13, 0.03, -0.02, 0.04, 
-0.1, 0.04, 0.05, -0.01, 0.09, 0, 0.01, 0.07, 0.01, 0, 0.07, 
-0.01, 0.04, 0.03, 0.02, 0.03), labels = c("2 %", "4 %", "-2 %", 
"7 %", "-2 %", "2 %", "4 %", "-1 %", "2 %", "3 %", "-5 %", "5 %", 
"5 %", "-3 %", "11 %", "3 %", "6 %", "8 %", "-1 %", "-1 %", "1 %", 
"-7 %", "7 %", "2 %", "0 %", "5 %", "5 %", "9 %", "13 %", "3 %", 
"-2 %", "4 %", "-10 %", "4 %", "5 %", "-1 %", "9 %", "0 %", "1 %", 
"7 %", "1 %", "0 %", "7 %", "-1 %", "4 %", "3 %", "2 %", "3 %"
), Area2 = c("Region 1", "Region 2", "Region 4", "Region 3", 
"Region 5", "Region 6", "Region 7", "Region 8", "Region 9", "Region 10", 
"Region 11", "Region 1", "Region 2", "Region 4", "Region 3", 
"Region 5", "Region 6", "Region 7", "Region 8", "Region 9", "Region 10", 
"Region 11", "Region 1", "Region 2", "Region 4", "Region 3", 
"Region 5", "Region 6", "Region 7", "Region 8", "Region 9", "Region 10", 
"Region 11", "Region 1", "Region 2", "Region 4", "Region 3", 
"Region 5", "Region 6", "Region 7", "Region 8", "Region 9", "Region 10", 
"Region 11", "National", "National", "National", "National"), 
    Area3 = c("Region 1", "Region 2", "Region 4", "Region 3", 
    "Region 5", "Region 6", "Region 7", "Region 8", "Region 9", 
    "Region 10", "Region 11", "Region 1", "Region 2", "Region 4", 
    "Region 3", "Region 5", "Region 6", "Region 7", "Region 8", 
    "Region 9", "Region 10", "Region 11", "Region 1", "Region 2", 
    "Region 4", "Region 3", "Region 5", "Region 6", "Region 7", 
    "Region 8", "Region 9", "Region 10", "Region 11", "Region 1", 
    "Region 2", "Region 4", "Region 3", "Region 5", "Region 6", 
    "Region 7", "Region 8", "Region 9", "Region 10", "Region 11", 
    "National", "National", "National", "National")), row.names = c(NA, 
-48L), class = "data.frame")


## HERE IS MY VISUALIZATION CODE ################################

ggplot(example2, aes(Measure.Names, 
                    Measure.Values, 
                    group = Area2)) + 
  scale_y_continuous(labels = scales::percent_format(accuracy = 1)) +  
  
  geom_line(data = example2[,3:6],
            aes(Measure.Names, 
                Measure.Values, 
                group = Area2), 
            color = "light grey")+
  
  geom_line(data = example2[,3:6] %>% filter (Area2 == "Total Core Trauma"),
            aes(Measure.Names, 
                Measure.Values, 
                group = Area2), 
            color = "black")+
  geom_line(color = "red")+
  geom_text(aes(y = Measure.Values, label= labels), hjust= 0.5, vjust = -1, size= 2.5, color= "black") +
  facet_wrap(~Area3,3, scales = "free") + 
  theme_bw()+
  theme(axis.title = element_blank()) + 
  theme(strip.background =element_rect(fill="navy"))+
  theme(strip.text = element_text(colour = 'white', face = "bold")) + 
  theme( 
    axis.text.y=element_blank(), 
    axis.ticks.y=element_blank()) + 
  theme(text = element_text(size = 10)) + 
  geom_line(data = example2[45:48,3:6],
            aes(Measure.Names, 
                Measure.Values, 
                group = Area2), 
            color = "black")+
  theme(panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank())+
  theme(panel.grid.major.x = element_line(
    size = 0.2,
    linetype = 2)) 

r ggplot2 facet-wrap
1个回答
0
投票

尝试将颜色映射到条件。不要忘记确保变量值“red”使用scale_color_identity()映射到-color-red:

  geom_line(aes(color = ifelse(Area3 == "Region 9" | Area3 == "Region 11", "red", "blue")))+
  scale_color_identity()
© www.soinside.com 2019 - 2024. All rights reserved.