Search code examples
rggplot2linewidth

R ggplot2 - format linewidth and linetype on a specific serie when using group and color


I had this diagram based on df (78 obs. of 3 variables), see head of df below:

 head(df_konj)
            Indikator     Period value
1 Barometerindikatorn 2022-01-01 110.9
2 Barometerindikatorn 2022-02-01 114.0
3 Barometerindikatorn 2022-03-01 112.2
4 Barometerindikatorn 2022-04-01 110.3
5 Barometerindikatorn 2022-05-01 110.8
6 Barometerindikatorn 2022-06-01 106.1

[![enter image description here][1]][1]

But wanted the serie "Barometerindikatorn" to be thicker.

Old code:

ggplot(df_konj, aes(x = Period, y = value, group=Indikator, color=Indikator)) +
  geom_point(size=2) + 
  geom_line(linewidth=1.1, aes(linetype=Indikator)) +
  labs(x = "Månad", y = "Värde",
       title="Konjunkturbarometern - månadsbarometern, flera indikatorer",
       subtitle="Sverige",
       caption="Källa: Konjunkturinstitutet",
       fill="År")

New code:

ggplot(df_konj_exlB, aes(x = Period, y = value, group=Indikator, color=Indikator)) +
  geom_point(size=2) + 
  geom_line(linewidth=1.1, aes(linetype=Indikator)) +
  geom_line(data = filter(df_konj, Indikator == "Barometerindikatorn"), linewidth = 2)
  labs(x = "Månad", y = "Värde",
       title="Konjunkturbarometern - månadsbarometern, flera indikatorer",
       subtitle="Sverige",
       caption="Källa: Konjunkturinstitutet",
       fill="År")

Not what I wanted: [![enter image description here][2]][2]

The serie I wanted got thicker, but issue 1. the legend changed to 2 and it divided it up - which I don't want. 2. I also don't want the serie (observations of) "Bygg & anläggning" to get solid - none of the rest of the series should be solid

Desired state

Everything like figure 1 - except serie (observations) of "Barometerindikatorn" in col. Indikator should be the only one which is both solid and thicker. How can I achieve this?

I'm beginner to R so my understanding of ggplot, and why I even need to use group e.g. is not good at all.

Thanks in advance!

EDIT3 - correct code w result I wanted:

ggplot(df_konj, aes(x = Period, y = value, group=Indikator, color=Indikator, 
                    linetype=Indikator, linewidth = Indikator)) +
  geom_point(size=2) + 
  geom_line() +
  scale_discrete_manual("linewidth", values = c(2, rep(1, 5))) +
  labs(x = "Månad", y = "Värde",
       title="Konjunkturbarometern - månadsbarometern, flera indikatorer",
       subtitle="Sverige",
       caption="Källa: Konjunkturinstitutet",
       fill="År")+
  theme(legend.title = element_text(face="bold")) +
  theme_bw() 

Solution

  • You need to map linewidth to a manual scale and set it there, rather than including a separate geom_line call.

    There doesn't seem to be a scale_linewidth_manual in the latest version of ggplot, so instead we can do:

    ggplot(df_konj, aes(Period, value, color = Indikator)) +
      geom_point(size = 2) + 
      geom_line(aes(linetype = Indikator, linewidth = Indikator)) +
      scale_discrete_manual('linewidth', values = c(2, 1, 1, 1, 1, 1)) +
      labs(x = "Månad", y = "Värde",
           title = "Konjunkturbarometern - månadsbarometern, flera indikatorer",
           subtitle = "Sverige",
           caption = "Källa: Konjunkturinstitutet",
           fill = "År") +
      theme_bw()
    

    enter image description here