I am creating plots with ggplot2 and for some reason the function is acting really weirdly.
I have a dataframe df, and I want to visualize several columns.
Any dataframe seems to work fine. I have generated this dummy dataframe.
df <- data.frame(Date = seq.Date(as.Date.character("2019-01-01"), by = 1, length.out = 10),
Value = rnorm(10),
Foo = rnorm(10))
So what I do is
library(ggplot2)
gg <- ggplot(df, aes(x = Date)) + geom_line(aes(y = Value, color = "Value", linetype = "Value"))
gg <- gg + geom_line(aes(y = Foo, color = "SomeWord", linetype = "SomeWord"))
gg <- gg + scale_color_manual(name="Legend",
breaks=c("Value", "SomeWord"), values=c("steelblue", "firebrick")) +
scale_linetype_manual(name="Legend",
breaks=c("Value", "SomeWord"), values=c("solid", "twodash"))
gg
Normally, ggplot2 would now correctly assign color steelblue and linetype solid to the column Value, while assigning firebrick and twodash to the Foo column, which I assigned the name SomeWord to. However, depending on what I choose for the name, ggplot assigns the colours and linetypes in the wrong way. For example, using "Test1" as a name seems to work just fine, but "Einschritt" causes ggplot2 to throw my entire ruleset out of the window.
I have tried googling this, but have not found any clue on why ggplot seems not to accept some names, while others are just fine. I would also like to use Hyphens in the color and linetype reference name which I assume might be a problem.
Edit: As an example, I have just tried replicating this on my dummy data frame. Using the code posted above, when I use the following names, the linetype and colour are matched falsely:
- "Value" for column "Value", anything for column Foo.
- "Ein-Schritt-Prognose" for column "Value", anything for column Foo.
- "SomeWord" for column "Value", anything for column Foo.
However, when I switch to something like:
- "ABD" for column Value, anything for column Foo.
then they are matched correctly.





