Mario
Mario

Reputation: 2621

ggplot: Add second legend to plot with two dataframes

I have two dataframes that I can plot in a line chart with this command:

ggplot(herkunft.nach.sprachreg, aes(x=year, y=anteil, group=regio, colour = regio)) +
  geom_line(size=0.75) +
  #stat_smooth(se = FALSE) +
  geom_line(data = herkunft.nach.jahren, aes(group = regio),colour='black',  linetype = 2) +
  scale_colour_manual(name="Sprachregionen", values = c("#238b45","#d94801", "#2171b5", "#969696", "#000000"),
                      labels=c("deutschsprachig", "französischsprachig", "italienischsprachig", "rumantsch", "Schweiz"))

Result:

ggplot

However, I cannot add the dashed line from the second dataframe (herkunft.nach.jahren) to the legend. Any idea how I could achieve this?

this is my data:

herkunft.nach.jahren

 year    anteil regio
 1: 1850  2.992441    CH
 2: 1860  4.579501    CH
 3: 1870  5.702563    CH
 4: 1880  6.895583    CH
 5: 1888  7.875963    CH
 6: 1900 11.562798    CH
 7: 1910 14.708120    CH
 8: 1920 10.369849    CH
 9: 1930  8.742846    CH
10: 1941  5.240783    CH
11: 1950  6.053988    CH
12: 1960 10.765977    CH
13: 1970 17.226689    CH
14: 1980 14.844046    CH
15: 1990 18.118835    CH
16: 2000 20.520677    CH
17: 2010 23.134607    CH

herkunft.nach.sprachreg

year regio    anteil
 1: 1850     D  1.847823
 2: 1860     D  2.792707
 3: 1870     D  3.500240
 4: 1880     D  5.676991
 5: 1888     D  6.344455
 6: 1900     D 10.154804
 7: 1910     D 13.464852
 8: 1920     D  9.207319
 9: 1930     D  7.666982
10: 1941     D  4.217004
11: 1950     D  4.975853
12: 1960     D  9.767691
13: 1970     D 15.207087
14: 1980     D 12.710072
15: 1990     D 15.566806
16: 2000     D 18.297933
17: 2010     D 20.745188
18: 1850     F  5.805881
19: 1860     F  9.509214
20: 1870     F 11.649263
21: 1880     F  8.825456
22: 1888     F 11.007228
23: 1900     F 13.807910
24: 1910     F 15.943369
25: 1920     F 12.035999
26: 1930     F 10.049997
27: 1941     F  6.340864
28: 1950     F  7.683946
29: 1960     F 12.819156
30: 1970     F 22.102168
31: 1980     F 19.987130
32: 1990     F 24.892615
33: 2000     F 26.586658
34: 2010     F 29.691197
35: 1850     I  6.483036
36: 1860     I  5.955920
37: 1870     I  7.447771
38: 1880     I 15.543436
39: 1888     I 14.524391
40: 1900     I 21.761545
41: 1910     I 27.763855
42: 1920     I 21.001271
43: 1930     I 20.381279
44: 1941     I 17.579106
45: 1950     I 16.923411
46: 1960     I 18.243269
47: 1970     I 26.921148
48: 1980     I 24.233811
49: 1990     I 25.000170
50: 2000     I 25.055036
51: 2010     I 26.029678
52: 1850     R  1.694507
53: 1860     R  2.349471
54: 1870     R  2.954100
55: 1880     R  4.152596
56: 1888     R  4.977768
57: 1900     R  5.817789
58: 1910     R 13.538229
59: 1920     R  6.262052
60: 1930     R  5.764689
61: 1941     R  3.854601
62: 1950     R  4.293110
63: 1960     R  9.171356
64: 1970     R  7.249696
65: 1980     R  5.185905
66: 1990     R  5.811382
67: 2000     R  7.980575
68: 2010     R 14.850335

Upvotes: 0

Views: 1834

Answers (1)

aosmith
aosmith

Reputation: 36076

If you don't want to combine your two datasets together, move color for your second geom_line inside aes.

If you want the last line to be dashed in the legend, you can use override.aes in guide_legend.

ggplot(herkunft.nach.sprachreg, aes(x=year, y=anteil, group=regio, colour = regio)) +
    geom_line(size=0.75) +
    #stat_smooth(se = FALSE) +
    geom_line(data = herkunft.nach.jahren, aes(group = regio, colour='Schweiz'),  linetype = 2) +
    scale_colour_manual(name="Sprachregionen", values = c("#238b45","#d94801", "#2171b5", "#969696", "#000000"),
                        labels=c("deutschsprachig", "französischsprachig", "italienischsprachig", "rumantsch", "Schweiz"),
                        guide = guide_legend(override.aes = list(linetype = c(1, 1, 1, 1, 2))))

Upvotes: 1

Related Questions