手动绘制线型ggplot

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

我想根据一个国家(分析对象)或一组国家的天气手动绘制线型以进行比较。出于演示原因,我还将国家代码中的观察结果重命名为国家名称。到目前为止,最后一部分已经起作用,但我似乎不知道它是否导致了问题。

我写了这段代码:

data_pop_growth_rate_output_worker %>% filter(ref_area == "CHN" |
                                               ref_area == "PRK" |
                                               ref_area == "VNM" |
                                               ref_area == "X05") %>% 
  mutate(ref_area = recode(ref_area,
                          CHN = "China",
                          PRK = "Coreia",
                          VNM = "Vietnã",
                          X05 = "Mundo: alta renda")) %>% 
 
  
    ggplot()+
  geom_line(aes(x = time,
                y = obs_value,
                colour = ref_area))+
  scale_color_manual(values = c("China" = "#FE6100",
                    "Coreia" =  "#648FFF",
                    "Vietnã" = "#FFB000",
                    "Mundo: alta renda" = "#785EF0"
                    ))+
  scale_linetype_manual(values = c("China" = "solid",
                    "Coreia" =  "solid",
                    "Vietnã" = "solid",
                    "Mundo: alta renda" = "dotted"
                    ))

结果是:

enter image description here

我手动缩放线型似乎不起作用。世界(“mundo”)区域保持稳定。

尝试为一个特定观察手动绘制虚线类型。

r ggplot2 scale geom
1个回答
0
投票

没有数据不确定,但您需要添加

aes(linetype=ref_area)
(或任何包含您国家/地区名称的列)。也许

    ggplot()+
  geom_line(aes(x = time,
                y = obs_value,
                colour = ref_area,
                linetype = ref_area)) +                  ## add this
  scale_color_manual(values = c("China" = "#FE6100",
                    "Coreia" =  "#648FFF",
                    "Vietnã" = "#FFB000",
                    "Mundo: alta renda" = "#785EF0"
                    ))+
  scale_linetype_manual(values = c("China" = "solid",
                    "Coreia" =  "solid",
                    "Vietnã" = "solid",
                    "Mundo: alta renda" = "dotted"
                    ))
© www.soinside.com 2019 - 2024. All rights reserved.