1

Im looking for a solution to have grouped legends. Right now it seems that the first fill argument takes over the labeling of the legend.

Looking for: Solution to show both data sets separated inside the legend.

Problem: Mixed up legend and data corresponding data not standing together.

Note: The shown data are from two different origins:

  1. mean value in relation to the income groups
  2. values of the income groups by themselves

Sorry: The labeling is not english as my data set is in german. (wish it would be otherwise ^^')

enter image description here

spine_gender = 
  ggplot()+
  geom_bar(stat="identity", 
            aes(
              x=Altersgruppe,
              y=mean_shopping_time_after_age_country_gender,
              group=Nationalitaet,
              fill=Geschlecht
              ),
            df_spine,
            position = "dodge")+
             
  geom_point (stat="identity", 
              aes(
                x=Altersgruppe,
                y=mean_time_money,
                group=Nationalitaet,
                fill=Einkommensgruppe
                ),
              df_spine,
              position = "dodge",
              shape=21,
              size=4,
              stroke=NA,)+
  xlab("Nationalitaet")+
  ylab("MW Zeitaufwand")+
  facet_grid(Nationalitaet~.)+
  theme_light()+
  coord_flip()

Sample

Altersgruppe;Nationalitaet;Geschlecht;Einkommensgruppe;Anzahl_befragte_Personen;durchschnittlicher_Zeitaufwand_fuer_das_Einkaufen_von_Nahrunsgmitteln_pro_Tag_in_Minuten;durchschnittliche_monatliche_Ausgaben_fuer_Nahrungsmittel_in_Landeswaehrung;color;color_income;mean_time_money;mean_shopping_time_after_age_country_gender 20-35;D;W;niedrig;966;-18;312;pink;red;-17.3076923076923;-10.511280722101 20-35;CH;W;niedrig;726;-18;318;pink;red;-16.9811320754717;-10.5971533104517 20-35;A;W;niedrig;893;-16;265;pink;red;-18.1132075471698;-10.8699314067247 20-35;D;M;niedrig;789;6;187;lightblue;red;9.62566844919786;5.3076867262387 20-35;CH;M;niedrig;973;5;198;lightblue;red;7.57575757575758;4.50583820758843 20-35;A;M;niedrig;752;5;169;lightblue;red;8.87573964497041;5.50753416696345 36-50;D;W;niedrig;990;-20;384;pink;red;-15.625;-10.113089934094 36-50;CH;W;niedrig;717;-17;370;pink;red;-13.7837837837838;-8.47337425113925 36-50;A;W;niedrig;1010;-19;395;pink;red;-14.4303797468354;-9.10900477295668 36-50;D;M;niedrig;688;7;250;lightblue;red;8.4;5.13441893160424 36-50;CH;M;niedrig;966;7;251;lightblue;red;8.36653386454183;4.70848721443906 36-50;A;M;niedrig;824;7;263;lightblue;red;7.98479087452472;4.80990788672021 51-65;D;W;niedrig;693;-14;197;pink;red;-21.3197969543147;-12.564806686857 51-65;CH;W;niedrig;912;-13;203;pink;red;-19.2118226600985;-11.1161409755095 51-65;A;W;niedrig;680;-13;180;pink;red;-21.6666666666667;-12.7679639318909 51-65;D;M;niedrig;292;5;117;lightblue;red;12.8205128205128;6.67043008029075 51-65;CH;M;niedrig;608;5;126;lightblue;red;11.9047619047619;5.51711003606047 51-65;A;M;niedrig;720;5;112;lightblue;red;13.3928571428571;6.44301771203513 20-35;D;W;mittel;903;-13;460;pink;grey;-8.47826086956522;-10.511280722101 20-35;CH;W;mittel;654;-15;523;pink;grey;-8.60420650095602;-10.5971533104517 20-35;A;W;mittel;870;-13;439;pink;grey;-8.88382687927107;-10.8699314067247 20-35;D;M;mittel;894;5;347;lightblue;grey;4.32276657060519;5.3076867262387 20-35;CH;M;mittel;921;4;304;lightblue;grey;3.94736842105263;4.50583820758843 20-35;A;M;mittel;679;5;297;lightblue;grey;5.05050505050505;5.50753416696345 36-50;D;W;mittel;901;-16;539;pink;grey;-8.90538033395176;-10.113089934094 36-50;CH;W;mittel;795;-14;559;pink;grey;-7.5134168157424;-8.47337425113925 36-50;A;W;mittel;954;-14;532;pink;grey;-7.89473684210526;-9.10900477295668 36-50;D;M;mittel;637;5;372;lightblue;grey;4.03225806451613;5.13441893160424 36-50;CH;M;mittel;1119;5;450;lightblue;grey;3.33333333333333;4.70848721443906 36-50;A;M;mittel;774;5;387;lightblue;grey;3.87596899224806;4.80990788672021 51-65;D;W;mittel;823;-12;419;pink;grey;-8.59188544152745;-12.564806686857 51-65;CH;W;mittel;1030;-11;409;pink;grey;-8.06845965770171;-11.1161409755095 51-65;A;W;mittel;713;-12;410;pink;grey;-8.78048780487805;-12.7679639318909 51-65;D;M;mittel;377;4;293;lightblue;grey;4.09556313993174;6.67043008029075 51-65;CH;M;mittel;633;3;294;lightblue;grey;3.06122448979592;5.51711003606047 51-65;A;M;mittel;658;3;243;lightblue;grey;3.7037037037037;6.44301771203513 20-35;D;W;hoch;910;-11;621;pink;green;-5.31400966183575;-10.511280722101 20-35;CH;W;hoch;596;-11;659;pink;green;-5.00758725341426;-10.5971533104517 20-35;A;W;hoch;832;-11;638;pink;green;-5.17241379310345;-10.8699314067247 20-35;D;M;hoch;836;4;525;lightblue;green;2.28571428571429;5.3076867262387 20-35;CH;M;hoch;962;3;465;lightblue;green;1.93548387096774;4.50583820758843 20-35;A;M;hoch;739;4;480;lightblue;green;2.5;5.50753416696345 36-50;D;W;hoch;978;-15;797;pink;green;-5.64617314930991;-10.113089934094 36-50;CH;W;hoch;929;-15;866;pink;green;-5.19630484988453;-8.47337425113925 36-50;A;W;hoch;1107;-15;849;pink;green;-5.30035335689046;-9.10900477295668 36-50;D;M;hoch;697;5;514;lightblue;green;2.91828793774319;5.13441893160424 36-50;CH;M;hoch;1036;5;539;lightblue;green;2.78293135435993;4.70848721443906 36-50;A;M;hoch;848;5;582;lightblue;green;2.57731958762887;4.80990788672021 51-65;D;W;hoch;654;-10;362;pink;green;-8.28729281767956;-12.564806686857 51-65;CH;W;hoch;961;-9;403;pink;green;-6.69975186104218;-11.1161409755095 51-65;A;W;hoch;720;-11;397;pink;green;-8.31234256926952;-12.7679639318909 51-65;D;M;hoch;252;3;265;lightblue;green;3.39622641509434;6.67043008029075 51-65;CH;M;hoch;670;2;294;lightblue;green;2.04081632653061;5.51711003606047 51-65;A;M;hoch;763;2;267;lightblue;green;2.24719101123596;6.44301771203513

404rorre
  • 89
  • 7

1 Answers1

1

Try with ggnewscale package:

# install.packages("ggplot2")
# install.packages("data.table")
# install.packages("ggnewscale")

library(ggplot2)
library(data.table)
library(ggnewscale)

# data sample
my_data <-
  data.table(
    col_x = rep(letters[1:4], 5),
    col_y1 = rnorm(20, 3, 1),
    col_y2 = rnorm(20, 4, 1.5),
    col_grup = rep(LETTERS[1:2], 10),
    col_fill1 = rep(letters[3:6], 10),
    colfill2 = rep(letters[7:10], 10)
  )

ggplot(data = my_data) +
  geom_bar(
    stat = "identity",
    aes(
      x = col_x,
      y = col_y2,
      group = col_grup,
      fill = col_fill1
    ),
    position = "dodge"
  ) +
  scale_fill_manual("col_fill1 NAME", values = colorRampPalette(c("red", "green"))(4)) +
  new_scale_fill() + # Define scales before initiating a new one
  geom_point (
    stat = "identity",
    aes(
      x = col_x,
      y = col_y2,
      group = col_grup,
      fill = colfill2
    ),
    position = "dodge",
    shape = 21,
    size = 4,
    stroke = NA
  ) +
  scale_fill_manual("colfill2 NAME", values = colorRampPalette(c("blue", "yellow"))(4)) +
  xlab("col_x") +
  ylab("MW Zeitaufwand") +
  facet_grid(col_grup ~ .) +
  labs(shape = "Split legend") +
  theme_light() +
  coord_flip()

enter image description here

K. Peltzer
  • 326
  • 3
  • 7