0

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() 
Christian
  • 117
  • 1
  • 3
  • 10
  • 3
    Don't add a second `geom_line`. Instead use `linewidth = Indikator` inside `aes`. To make just the first line thicker, add `+ scale_linewidth_manual(values = c(2, rep(1.1, 5)))` This should also result in a single legend. You haven't provided enough data to demonstrate, so if this doesn't work for you please edit your question to include the output of `dput(df_konj)` – Allan Cameron Feb 17 '23 at 12:32
  • Thanks! But adding that code resulted in: *"could not find function "scale_linewidth_manual" "* some googling suggested: " scale_discrete_manual("linewidth", values = c(2, rep(1.1, 5)))" which worked. BUT, legend is now 1 legend, but it only show color, not the linetype in legend. Moving also linetype from aes within geom_line to ggplot aes did do something, for some of the series (but not all)? I attach new picture in original post including code (as EDIT1). – Christian Feb 17 '23 at 12:52
  • Apperently i left the old row with geom_line anyways?! I got it to work now, everything i ggplot aes, as per your instruction and with the (new+) scale_discrete_manual("linewidth", values = c(2, rep(1.1, 5))). I'll remove my edits. – Christian Feb 17 '23 at 13:19

1 Answers1

1

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

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Yes we figured this one out, mostly you though - thanks! :) – Christian Feb 17 '23 at 13:28
  • Allan if I have a follow-up question on the same topic - i wanna do the same on a different df, but the order is wrong in how ggplot plot them, although the order in df looks to be correct, should I create a new question? – Christian Feb 17 '23 at 13:36
  • 1
    @Christian that question gets asked a lot, so don't make another question. Character vectors are converted to factors before plotting in ggplot, so if you want your variables in a particular order, you can explicitly convert them to a factor, and select the ordering yourself. To get them in the order they appear you can do `df$variable <- factor(df$variable, unique(df$variable))` – Allan Cameron Feb 17 '23 at 13:58
  • Great, I used factor w/o additional arg - didn't understand what that would do. Afterwards function relevel: df$Indikator <- relevel(df$Indikator, "Totala näringslivet") - this resulted in what I wanted. :) – Christian Feb 17 '23 at 15:10