Two-categories legend

Hi,

what approach would you recommend to add two categories to a legend, something like this r - changing the legends in ggplot2 to have groups of similar labels - Stack Overflow

data_plot <- structure(list(country = c("Dánsko", "Irsko", "Švédsko", "Nizozemsko", 
                                        "Belgie", "Island", "Finsko", "Česká rep.", "Norsko", "Francie", 
                                        "Maďarsko", "Rakousko", "Slovinsko", "Spojené král.", "Lucembursko", 
                                        "USA", "Německo", "Španělsko", "Itálie", "Portugalsko", "Švýcarsko", 
                                        "Řecko", "Polsko", "Estonsko", "Srbsko", "Rumunsko", "Dánsko", 
                                        "Irsko", "Švédsko", "Nizozemsko", "Belgie", "Island", "Finsko", 
                                        "Česká rep.", "Norsko", "Francie", "Maďarsko", "Rakousko", 
                                        "Slovinsko", "Spojené král.", "Lucembursko", "USA", "Německo", 
                                        "Španělsko", "Itálie", "Portugalsko", "Švýcarsko", "Řecko", 
                                        "Polsko", "Estonsko", "Srbsko", "Rumunsko", "Dánsko", "Irsko", 
                                        "Švédsko", "Nizozemsko", "Belgie", "Island", "Finsko", "Česká rep.", 
                                        "Norsko", "Francie", "Maďarsko", "Rakousko", "Slovinsko", "Spojené král.", 
                                        "Lucembursko", "USA", "Německo", "Španělsko", "Itálie", "Portugalsko", 
                                        "Švýcarsko", "Řecko", "Polsko", "Estonsko", "Srbsko", "Rumunsko", 
                                        "Dánsko", "Irsko", "Švédsko", "Nizozemsko", "Belgie", "Island", 
                                        "Finsko", "Česká rep.", "Norsko", "Francie", "Maďarsko", "Rakousko", 
                                        "Slovinsko", "Spojené král.", "Lucembursko", "USA", "Německo", 
                                        "Španělsko", "Itálie", "Portugalsko", "Švýcarsko", "Řecko", 
                                        "Polsko", "Estonsko", "Srbsko", "Rumunsko"), `starob. důchody` = c(6.82175254821777, 
                                                                                                           3.4394383430481, 7.89058589935303, 7.11408996582031, 7.02075529098511, 
                                                                                                           4.10098266601562, 7.09368658065796, 9.49062824249268, 5.23411417007446, 
                                                                                                           5.91149187088013, 5.46756553649902, 7.10214281082153, 6.30662822723389, 
                                                                                                           5.64014196395874, 3.55563569068909, 1.7183450460434, 7.18503379821777, 
                                                                                                           4.84626531600952, 7.57450008392334, 6.29415607452393, 9.1420783996582, 
                                                                                                           7.285475730896, 5.61926937103271, 5.94276571273804, 3.83493041992188, 
                                                                                                           4.89866352081299, 6.82175254821777, 3.4394383430481, 7.89058589935303, 
                                                                                                           7.11408996582031, 7.02075529098511, 4.10098266601562, 7.09368658065796, 
                                                                                                           9.49062824249268, 5.23411417007446, 5.91149187088013, 5.46756553649902, 
                                                                                                           7.10214281082153, 6.30662822723389, 5.64014196395874, 3.55563569068909, 
                                                                                                           1.7183450460434, 7.18503379821777, 4.84626531600952, 7.57450008392334, 
                                                                                                           6.29415607452393, 9.1420783996582, 7.285475730896, 5.61926937103271, 
                                                                                                           5.94276571273804, 3.83493041992188, 4.89866352081299, 6.82175254821777, 
                                                                                                           3.4394383430481, 7.89058589935303, 7.11408996582031, 7.02075529098511, 
                                                                                                           4.10098266601562, 7.09368658065796, 9.49062824249268, 5.23411417007446, 
                                                                                                           5.91149187088013, 5.46756553649902, 7.10214281082153, 6.30662822723389, 
                                                                                                           5.64014196395874, 3.55563569068909, 1.7183450460434, 7.18503379821777, 
                                                                                                           4.84626531600952, 7.57450008392334, 6.29415607452393, 9.1420783996582, 
                                                                                                           7.285475730896, 5.61926937103271, 5.94276571273804, 3.83493041992188, 
                                                                                                           4.89866352081299, 6.82175254821777, 3.4394383430481, 7.89058589935303, 
                                                                                                           7.11408996582031, 7.02075529098511, 4.10098266601562, 7.09368658065796, 
                                                                                                           9.49062824249268, 5.23411417007446, 5.91149187088013, 5.46756553649902, 
                                                                                                           7.10214281082153, 6.30662822723389, 5.64014196395874, 3.55563569068909, 
                                                                                                           1.7183450460434, 7.18503379821777, 4.84626531600952, 7.57450008392334, 
                                                                                                           6.29415607452393, 9.1420783996582, 7.285475730896, 5.61926937103271, 
                                                                                                           5.94276571273804, 3.83493041992188, 4.89866352081299), total = c(16.3484301567078, 
                                                                                                                                                                            15.958060503006, 14.7512962818146, 14.6082615852356, 14.2426084280014, 
                                                                                                                                                                            14.0117658376694, 13.965563416481, 13.4162129163742, 13.160128891468, 
                                                                                                                                                                            12.7352687120438, 12.6339061260223, 12.5528424978256, 12.4850103855133, 
                                                                                                                                                                            11.658962726593, 11.463140130043, 11.1154057979584, 11.0065249204636, 
                                                                                                                                                                            10.4203115701675, 10.2063401341438, 10.0996255874634, 9.38751983642578, 
                                                                                                                                                                            8.99790626764297, 8.95461642742157, 8.57390266656876, 7.16727435588837, 
                                                                                                                                                                            7.03615635633469, 16.3484301567078, 15.958060503006, 14.7512962818146, 
                                                                                                                                                                            14.6082615852356, 14.2426084280014, 14.0117658376694, 13.965563416481, 
                                                                                                                                                                            13.4162129163742, 13.160128891468, 12.7352687120438, 12.6339061260223, 
                                                                                                                                                                            12.5528424978256, 12.4850103855133, 11.658962726593, 11.463140130043, 
                                                                                                                                                                            11.1154057979584, 11.0065249204636, 10.4203115701675, 10.2063401341438, 
                                                                                                                                                                            10.0996255874634, 9.38751983642578, 8.99790626764297, 8.95461642742157, 
                                                                                                                                                                            8.57390266656876, 7.16727435588837, 7.03615635633469, 16.3484301567078, 
                                                                                                                                                                            15.958060503006, 14.7512962818146, 14.6082615852356, 14.2426084280014, 
                                                                                                                                                                            14.0117658376694, 13.965563416481, 13.4162129163742, 13.160128891468, 
                                                                                                                                                                            12.7352687120438, 12.6339061260223, 12.5528424978256, 12.4850103855133, 
                                                                                                                                                                            11.658962726593, 11.463140130043, 11.1154057979584, 11.0065249204636, 
                                                                                                                                                                            10.4203115701675, 10.2063401341438, 10.0996255874634, 9.38751983642578, 
                                                                                                                                                                            8.99790626764297, 8.95461642742157, 8.57390266656876, 7.16727435588837, 
                                                                                                                                                                            7.03615635633469, 16.3484301567078, 15.958060503006, 14.7512962818146, 
                                                                                                                                                                            14.6082615852356, 14.2426084280014, 14.0117658376694, 13.965563416481, 
                                                                                                                                                                            13.4162129163742, 13.160128891468, 12.7352687120438, 12.6339061260223, 
                                                                                                                                                                            12.5528424978256, 12.4850103855133, 11.658962726593, 11.463140130043, 
                                                                                                                                                                            11.1154057979584, 11.0065249204636, 10.4203115701675, 10.2063401341438, 
                                                                                                                                                                            10.0996255874634, 9.38751983642578, 8.99790626764297, 8.95461642742157, 
                                                                                                                                                                            8.57390266656876, 7.16727435588837, 7.03615635633469), component = structure(c(4L, 
                                                                                                                                                                                                                                                           4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                                                                                                                                                                                                                                           4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                                                                                                                                                                                           2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                                                                                                                                                                                                                                           2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                                                                                                                                                                                                                                           3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 
                                                                                                                                                                                                                                                           1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                                                                                                                                           1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("zbylé veřejné výdaje", 
                                                                                                                                                                                                                                                                                                   "zdravotnictví", "rodinná a sociální podpora", "podpora v nezaměstnanosti a ZTP"
                                                                                                                                                                                                                                                           ), class = "factor"), value = c(4.35798788070679, 4.71157646179199, 
                                                                                                                                                                                                                                                                                           2.26604461669922, 1.73221254348755, 3.38601064682007, 1.72926318645477, 
                                                                                                                                                                                                                                                                                           2.91885852813721, 2.10806488990784, 3.15262889862061, 1.40192782878876, 
                                                                                                                                                                                                                                                                                           2.14112687110901, 2.28966403007507, 2.39490914344788, 1.12920069694519, 
                                                                                                                                                                                                                                                                                           1.2396776676178, 1.07840394973755, 1.53650867938995, 2.58500289916992, 
                                                                                                                                                                                                                                                                                           1.66916394233704, 1.99029207229614, 2.05276942253113, 0.903238475322723, 
                                                                                                                                                                                                                                                                                           1.9076966047287, 1.32383215427399, 0.9678955078125, 0.969247162342072, 
                                                                                                                                                                                                                                                                                           4.51233100891113, 4.56853675842285, 3.75543546676636, 4.37768888473511, 
                                                                                                                                                                                                                                                                                           4.36386442184448, 4.42223453521729, 3.61511492729187, 3.98593163490295, 
                                                                                                                                                                                                                                                                                           3.6630551815033, 4.19026851654053, 2.91883730888367, 3.8666090965271, 
                                                                                                                                                                                                                                                                                           3.57599258422852, 4.16485500335693, 3.42951822280884, 4.04126834869385, 
                                                                                                                                                                                                                                                                                           3.83790874481201, 3.4541323184967, 4.02951288223267, 3.4484486579895, 
                                                                                                                                                                                                                                                                                           1.38221955299377, 3.27976870536804, 2.43130707740784, 2.66560983657837, 
                                                                                                                                                                                                                                                                                           3.20122456550598, 2.2548828125, 1.67867708206177, 2.46929478645325, 
                                                                                                                                                                                                                                                                                           2.27491641044617, 2.26338958740234, 1.55140817165375, 1.28427267074585, 
                                                                                                                                                                                                                                                                                           1.68527257442474, 1.20439946651459, 0.865727961063385, 1.79421401023865, 
                                                                                                                                                                                                                                                                                           2.30768847465515, 1.94786894321442, 1.83087396621704, 2.37672567367554, 
                                                                                                                                                                                                                                                                                           1.73340117931366, 2.19822955131531, 2.13204526901245, 0.551501870155334, 
                                                                                                                                                                                                                                                                                           0.800752460956573, 1.11212873458862, 2.31267905235291, 0.858002543449402, 
                                                                                                                                                                                                                                                                                           1.1882700920105, 0.861432015895844, 0.59498655796051, 1.3331161737442, 
                                                                                                                                                                                                                                                                                           5.79943418502808, 4.20865249633789, 6.45489978790283, 6.2349705696106, 
                                                                                                                                                                                                                                                                                           4.94132518768311, 6.57599544525146, 5.7463173866272, 6.11781692504883, 
                                                                                                                                                                                                                                                                                           5.47871685028076, 5.34885835647583, 5.26625347137451, 4.44870042800903, 
                                                                                                                                                                                                                                                                                           4.68323469161987, 3.98818135261536, 5.06054306030273, 3.79750394821167, 
                                                                                                                                                                                                                                                                                           3.50006222724915, 3.82967448234558, 3.70691084861755, 3.54875612258911, 
                                                                                                                                                                                                                                                                                           3.63985180854797, 3.95689654350281, 3.42734265327454, 3.72302865982056, 
                                                                                                                                                                                                                                                                                           2.40316772460938, 2.47891020774841)), row.names = c(NA, -104L
                                                                                                                                                                                                                                                                                           ), class = c("tbl_df", "tbl", "data.frame"))

 
library(ggplot2)

ggplot(data_plot,aes(x = reorder(country, -total), y = value, fill = component)) +
  geom_col(show.legend = T) +
  scale_fill_manual(values = rev(c("#DD4540", "#ECB925","#4D2344", "#A6A6A6")),
                    guide = guide_legend(reverse = TRUE, nrow=2, byrow=TRUE)) +
  scale_y_continuous(label = scales::percent_format(accuracy = 1, scale = 1, prefix = "", suffix = " %",
                                                    big.mark = " ", decimal.mark = ","),
                     limits = c(0,20)) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 11),
        axis.text.y = element_text(size = 11, color = "#00254B"),
        panel.grid.major = element_blank(),
        axis.title = element_blank(),
        plot.title = element_text(color="#00254B", 
                                  margin = margin(t = 10,
                                                  r = 0,
                                                  b = 10,
                                                  l = 0)),
        legend.position =  "bottom",
        legend.title=element_blank(),
        legend.text = element_text(size = 10),
        plot.margin = margin(t = 10,
                             r = 0,
                             b = 00,
                             l = 10),
        legend.margin = margin(t = 0, 
                               r = 0, 
                               b = 0, 
                               l = 0)) 

Many thanks,

Jakub

Admittedly not a sophisticated solution like the link you shared, but if the goal is to add labels to the columns of your legend, one way to do it would be to add the following (adjusting the title spacing as needed). This also requires the removal of legend.title=element_blank() in the theme portion of your code.

scale_fill_manual(values = rev(c("#DD4540", "#ECB925","#4D2344", "#A6A6A6")),
                    guide = guide_legend(reverse = TRUE, nrow=2, byrow=TRUE,
                                         title = 'component\nGroup1                                               Group2',
                                         title.position = 'top')
                    )

Thank you! Would it also work if I wanted to have group titles on the left side?

Unfortunately, this approach would not suffice. If you want group titles on the left, you would need to follow the solution in the link you provided.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.