0
$\begingroup$

I have an issue with the "ggplot2"-package, where I cannot get the linetypes in the legend. enter image description here The legnd only shows the color of each line, but not the specific type.

My code looks like:

ggplot(data = vStdDevHours, aes(x=Hours)) +
  geom_line(aes(y = Yearly_DA, colour = "Yearly_DA")) +
  geom_line(aes(y = Winter_DA, colour = "Winter_DA")) +
  geom_line(aes(y = Spring_DA, colour = "Spring_DA")) +
  geom_line(aes(y = Summer_DA, colour = "Summer_DA")) +
  geom_line(aes(y = Fall_DA, colour = "Fall_DA")) +
  geom_line(aes(y = Yearly_RT, colour = "Yearly_RT", linetype = "Yearly_RT")) +
  geom_line(aes(y = Winter_RT, colour = "Winter_RT", linetype = "Winter_RT")) +
  geom_line(aes(y = Spring_RT, colour = "Spring_RT", linetype = "Spring_RT")) +
  geom_line(aes(y = Summer_RT, colour = "Summer_RT", linetype = "Summer_RT")) +
  geom_line(aes(y = Fall_RT, colour = "Fall_RT", linetype = "Fall_RT")) +
  scale_y_continuous(breaks=seq(0, 130, 10), limits=c(0, 130)) +
  scale_x_continuous(breaks = seq(1, 24, 1)) +
  ggtitle("Yearly and Seaonal Standard Deviation of Real-Time Prices") +
  ylab("Price") +
  xlab("Hours") +
  scale_linetype_manual(name = "Std. Dev.", values = c("Winter_DA" = "solid", "Winter_RT" = "dashed", "Fall_DA" = "solid", "Fall_RT" = "dashed", "Summer_DA" = "solid", 
                                                       "Summer_RT" = "dashed", "Spring_DA" = "solid", "Spring_RT" = "dashed", "Yearly_DA" = "solid", "Yearly_RT" = "dashed"))  +
  scale_colour_manual(name ="Std. Dev.", values = c("Winter_DA" = "darkred", "Winter_RT" = "darkred", "Fall_DA" = "steelblue", "Fall_RT" = "steelblue", 
                                                      "Summer_DA" = "forestgreen", "Summer_RT" = "forestgreen", "Spring_DA" = "purple", "Spring_RT" = "purple", 
                                                      "Yearly_DA" = "black", "Yearly_RT" = "black")) + 
  theme_bw() + theme(plot.title = element_text(hjust = 0.5))

I know that it is related to scale_linetype_manual, but all the workarounds I have tried have been unsuccessful.

Hopefully some ggplot wizard can help me, thanks in advance!

$\endgroup$

1 Answer 1

0
$\begingroup$

Solved:

ggplot(data = vStdDevHours, aes(x=Hours)) +
  geom_line(aes(y = Yearly_DA, colour = "Yearly_DA", linetype = "Yearly_DA")) +
  geom_line(aes(y = Winter_DA, colour = "Winter_DA", linetype = "Winter_DA")) +
  geom_line(aes(y = Spring_DA, colour = "Spring_DA", linetype = "Spring_DA")) +
  geom_line(aes(y = Summer_DA, colour = "Summer_DA", linetype = "Summer_DA")) +
  geom_line(aes(y = Fall_DA, colour = "Fall_DA", linetype = "Fall_DA")) +
  geom_line(aes(y = Yearly_RT, colour = "Yearly_RT", linetype = "Yearly_RT")) +
  geom_line(aes(y = Winter_RT, colour = "Winter_RT", linetype = "Winter_RT")) +
  geom_line(aes(y = Spring_RT, colour = "Spring_RT", linetype = "Spring_RT")) +
  geom_line(aes(y = Summer_RT, colour = "Summer_RT", linetype = "Summer_RT")) +
  geom_line(aes(y = Fall_RT, colour = "Fall_RT", linetype = "Fall_RT")) +
  scale_y_continuous(breaks=seq(0, 130, 10), limits=c(0, 130)) +
  scale_x_continuous(breaks = seq(1, 24, 1)) +
  ggtitle("Yearly and Seaonal Standard Deviation of Real-Time Prices") +
  ylab("Price") +
  xlab("Hours") +
  scale_linetype_manual(name = "Std. Dev.", values = c("Winter_DA" = "solid", "Winter_RT" = "dashed", "Fall_DA" = "solid", "Fall_RT" = "dashed", "Summer_DA" = "solid", "Summer_RT" = "dashed",
                                                      "Spring_DA" = "solid", "Spring_RT" = "dashed", "Yearly_DA" = "solid", "Yearly_RT" = "dashed"))  +
  scale_colour_manual(name ="Std. Dev.", values = c("Winter_DA" = "darkred", "Winter_RT" = "darkred", "Fall_DA" = "steelblue", "Fall_RT" = "steelblue", 
                                                      "Summer_DA" = "forestgreen", "Summer_RT" = "forestgreen", "Spring_DA" = "purple", "Spring_RT" = "purple", 
                                                      "Yearly_DA" = "black", "Yearly_RT" = "black")) + 
  theme_bw() + theme(plot.title = element_text(hjust = 0.5))
$\endgroup$
2
  • $\begingroup$ Great that you solved your own problem! Could you please identify or highlight the change you made to your code, so if someone else has a similar problem they can try your solution. Thanks! $\endgroup$
    – Lynn
    Aug 10, 2022 at 9:22
  • $\begingroup$ I strongly recommend reshaping the data long with pivot_longer() so you then can call geom_line() only once $\endgroup$
    – Andrea M
    Jul 20, 2023 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.