在ggplot()中,是否有类似于`scale_color_manual()的东西,但是对于线宽和线型,可以处理12行?

kcrjzv8t  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(162)

我的问题:我需要制作许多线条图。每个折线图将包含12条线。每一行对应一个位置。代码将被重复使用,以制作15-20个具有标准化格式的此类图。因此,我尝试编写一些可重用的代码。我不想为每个变量编写定制代码。
以下是我的数据:

tmp <- structure(list(Year = c(1971, 1976, 1981, 1986, 1991, 1996, 2001, 
2006, 2011, 2016, 2021, 1971, 1976, 1981, 1986, 1991, 1996, 2001, 
2006, 2011, 2016, 2021, 1971, 1976, 1981, 1986, 1991, 1996, 2001, 
2006, 2011, 2016, 2021, 1971, 1976, 1981, 1986, 1991, 1996, 2001, 
2006, 2011, 2016, 2021, 1971, 1976, 1981, 1986, 1991, 1996, 2001, 
2006, 2011, 2016, 2021, 1971, 1976, 1981, 1986, 1991, 1996, 2001, 
2006, 2011, 2016, 2021, 1971, 1976, 1981, 1986, 1991, 1996, 2001, 
2006, 2011, 2016, 2021, 1971, 1976, 1981, 1986, 1991, 1996, 2001, 
2006, 2011, 2016, 2021, 1971, 1976, 1981, 1986, 1991, 1996, 2001, 
2006, 2011, 2016, 2021, 1971, 1976, 1981, 1986, 1991, 1996, 2001, 
2006, 2011, 2016, 2021, 1971, 1976, 1981, 1986, 1991, 1996, 2001, 
2006, 2011, 2016, 2021, 1971, 1976, 1981, 1986, 1991, 1996, 2001, 
2006, 2011, 2016, 2021), variable = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 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, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L), .Label = c("FORT ERIE", "GRIMSBY", 
"LINCOLN", "NIAGARA FALLS", "NIAGARA-ON-THE-LAKE", "PELHAM", 
"PORT COLBORNE", "ST. CATHARINES", "THOROLD", "WAINFLEET", "WELLAND", 
"WEST LINCOLN"), class = "factor"), value = c(23113L, 24030L, 
24096L, 23253L, 26006L, 27183L, 28143L, 29925L, 29960L, 30710L, 
32901L, 15770L, 15565L, 15797L, 16956L, 18520L, 19585L, 21297L, 
23937L, 25325L, 27314L, 28883L, 14247L, 14460L, 14196L, 14391L, 
17149L, 18801L, 20612L, 21722L, 22487L, 23787L, 25719L, 67163L, 
69420L, 70960L, 72107L, 75399L, 76917L, 78815L, 82184L, 82997L, 
88071L, 94415L, 12552L, 12485L, 12186L, 12494L, 12945L, 13238L, 
13839L, 14587L, 15400L, 17511L, 19090L, 9997L, 10070L, 11104L, 
12137L, 13328L, 14343L, 15272L, 16155L, 16598L, 17110L, 18192L, 
21420L, 20535L, 19225L, 18281L, 18766L, 18451L, 18450L, 18599L, 
18424L, 18306L, 20033L, 109722L, 123350L, 124018L, 123455L, 129300L, 
130926L, 129170L, 131989L, 131400L, 133113L, 136803L, 15065L, 
14945L, 15412L, 16131L, 17542L, 17883L, 18048L, 18224L, 17931L, 
18801L, 23816L, 5486L, 6065L, 6000L, 5955L, 6203L, 6253L, 6258L, 
6601L, 6356L, 6372L, 6887L, 44397L, 45050L, 45448L, 45054L, 47914L, 
48411L, 48402L, 50331L, 50631L, 52293L, 55750L, 8396L, 9460L, 
9846L, 9918L, 10864L, 11513L, 12268L, 13167L, 13837L, 14500L, 
15454L)), row.names = c(NA, -132L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x00000230e29a1ef0>)

到目前为止,我的代码是这样的:

ggplot(data = tmp, aes(x = Year, y = value, color = variable, range(0,max(tmp$value)))) +
    geom_point() +
    geom_line() +
    labs(title = "Total population in Nigara's 12 CSDs, 1971-2021",
         subtitle = "Upward trend overall",
         x = "Census Year",
         y = "Total population",
         color = "") +
    scale_x_continuous(breaks = unique_years) +
    scale_y_continuous(breaks = scales::breaks_width(10000), labels = scales::comma) +
    theme_classic() +
    theme(legend.position = "bottom") +
    theme(plot.title.position = 'plot',
          plot.title = element_text(hjust = 0.5))

使用长格式数据(显然我不能在这里添加;让我知道如果我错过了什么)。..
...制作这张图:

我的问题是:有没有一种方法可以使用scale_color_manual()之类的东西来改变线宽和线型,但只针对线条?
最理想的情况是,每次我绘制图表时,每个位置(e。例如,Grimsby)将始终具有相同种类和颜色的线,并且我只需要在一个位置而不是在每个ggplot()中更改该代码。当我将geom_line(aes(linetype = variable))添加到上面的代码中时,它产生了丑陋的结果,所以我认为这不会起作用。

2guxujil

2guxujil1#

Andrew Gustar指出,是的,有scale_linewidth_manual(),在ggplot documentation中定义。
最后,在创建了两个变量来保存颜色和线型信息之后,我使用以下代码来使用directlabels::direct.label()scale_color_manual()scale_linetype_discrete()。这些问题是:

colVec <- ('#31a354', '#08519c', '#08519c', 'Black', '#08519c', 'Black', '#31a354', '#08519c', 'Black', '#31a354', 'Black', 'Black')

ltyVec <- rep(c("solid","dotted", "dashed"),c(4,4,4))

在定义了这两个变量之后,我运行了这个:

library(ggplot2)
library(directlabels)
base_size = 12

p <- ggplot(data = tmp, aes(x = Year, y = value, xmax = 2046, color = variable, linetype = variable, range(0,max(tmp$value)))) +
        geom_line(linewidth = 0.4) +
        geom_point(shape = 20) +
        labs(title = "Total population in Niagara's 12 CSDs, 1971-2021",
             subtitle = "",
             caption = "Source: Statistics Canada, Census of Population, various years",
             x = "Census Year",
             y = "Total population",
             color = "") +
        scale_x_continuous(breaks = unique_years) +
        scale_y_continuous(breaks = scales::breaks_width(10000), labels = scales::comma) +
        scale_linetype_discrete(name = "CSD") +
        scale_color_manual(name = "CSD", values = colVec) +
        theme_classic(base_size = 12,
                      base_family = "Trebuchet MS", # might be unneeded
                      base_line_size = base_size/22,
                      base_rect_size = base_size/22) +
        theme(text=element_text(family="Trebuchet MS"), # needed
              legend.position = "bottom",
              axis.text.x = element_text(angle = 30, face = "bold"),
              axis.text.y = element_text(face = "bold"),
              plot.title.position = 'plot',
              plot.title = element_text(hjust = 0.5, face = "bold",
                                        size = 18)) +
        annotate("rect", xmin = 1971, xmax = 2021, ymin = 4000, ymax = 35000, alpha = .18) +
        annotate("text", x = 2006, y = 35000, label = "See Figure 2 for details", size = base_size/4)
direct.label(p, list(cex = 1,dl.trans(x=x+0.1), "last.bumpup"))

这段代码生成了这个线图:

这是对我最初发布的线图的改进。

相关问题