Alex Rivera | Logout

Changing the line type in the ggplot legend

Asked 2013-02-14T13:08:09.277
25

I have a df:

head(hej3)

  Year          variable  N   Sum      Mean        sd   Variance Median Min  Max
1 1901     Delägare.män. 85 18089 212.81176 365.39168 133511.083  110.0   5 2771
2 1901 Delägare.kvinnor. 48  3509  73.10417  97.84209   9573.074   34.5   1  414
3 1902     Delägare.män. 92 19783 215.03261 363.63529 132230.625  111.5   2 2827
4 1902 Delägare.kvinnor. 53  3872  73.05660  94.12827   8860.131   40.0   1  408
5 1903     Delägare.män. 99 21123 213.36364 366.58411 134383.907  109.0   2 2994
6 1903 Delägare.kvinnor. 55  4201  76.38182  93.85006   8807.833   40.0   1  390
hej3 <- data.frame(
  Year = rep(
    c(
      1901L, 1902L, 1903L, 1904L, 1905L, 1906L, 1907L, 1908L, 1909L,
      1910L, 1911L, 1912L, 1915L, 1921L, 1924L, 1927L, 1930L
    ),
    each = 2L
  ),
  variable = factor(
    rep(c("Delägare.män.", "Delägare.kvinnor."), 17),
    levels = c("Delägare.män.", "Delägare.kvinnor.")
  ),
  N = c(
    85, 48, 92, 53, 99, 55, 112, 63, 116, 69, 126, 76, 130, 78, 130, 80, 129, 83,
    125, 81, 118, 77, 97, 72, 50, 41, 42, 34, 42, 33, 26, 20, 29, 29
  ),
  Sum = c(
    18089, 3509, 19783, 3872, 21123, 4201, 23686, 5087, 26751, 5652, 28198, 6493,
    31638, 6934, 32611, 7194, 36133, 7025, 30138, 6884, 26935, 6900, 20333, 8021,
    18929, 5619, 11157, 4332, 10778, 4437, 7974, 3416, 9270, 3793
  ),
  Mean = c(
    212.811764705882, 73.1041666666667, 215.032608695652, 73.0566037735849,
    213.363636363636, 76.3818181818182, 211.482142857143, 80.7460317460317,
    230.612068965517, 81.9130434782609, 223.793650793651, 85.4342105263158,
    243.369230769231, 88.8974358974359, 250.853846153846, 89.925,
    280.100775193798, 84.6385542168675, 241.104, 84.9876543209877,
    228.262711864407, 89.6103896103896, 209.618556701031, 111.402777777778,
    378.58, 137.048780487805, 265.642857142857, 127.411764705882,
    256.619047619048, 134.454545454545, 306.692307692308, 170.8, 3
Edit
Report

1 Answer

5
  1. Here you are want to change the linetype conditional on a variable. So, we create a new data set:

    R> library(reshape2)
    R> dd = melt(hej3, colnames(hej3)[c(1:2, 4, 6:10)])
    R> dd = dd[dd$variable=="Delägare.män.",c(1, 9:10)]
    R> head(dd, 4)
       Year variable value
    1  1901        N    85
    3  1902        N    92
    5  1903        N    99
    7  1904        N   112
    

    then we remove your two geom_line functions calls and replace with:

    + geom_line(data=dd, aes(x=Year, y = value, linetype=variable), 
              size = 1.5,  alpha = 1) + 
    

    where the line type changes according to your variable.

  2. To zoom into a region of you plot (help page)

    + coord_cartesian(ylim = c(-300, 600))
    

enter image description here


Full ggplot2 code for reference:

ggplot(subset(hej3,variable=="Delägare.män."), aes(x = Year)) + 
geom_line(data=dd, aes(x=Year, y = value, linetype=variable), 
          size = 1.5,  alpha = 1) + 
geom_ribbon(aes(ymax = Max, 
                ymin = Min, fill = "Delägare Män Max/Min"), linetype = 3, 
            alpha = 0.4) + 
geom_ribbon(aes(ymax = Mean+sd, ymin = Mean-sd, fill = "Mean +- sd"), 
            colour = "grey50", linetype = 3, alpha = 0.8)+
scale_color_manual("Variabler", breaks = c("Antal Kassor","Medelvärde"),  
                   values = c("Antal Kassor" = "black", "Medelvärde" = "#6E6E6E")) + 
scale_fill_manual(" Ribbons", breaks = c("Delägare Män Max/Min", "Mean +- sd"), 
                  values = c(`Delägare Män Max/Min` = "grey50", `Mean +- sd` = "#4E4E4E")) + 
theme(legend.direction = "horizontal", legend.position = "bottom", legend.key = element_blank(), 
      legend.background = element_rect(fill = "white", colour = "gray30")) + 
guides(fi
answered 2013-02-14T13:35:28.663

Your Answer