简体   繁体   中英

Labelling geom_bar plot at a fixed distance from y-axis in ggplot

I want to place bar text labels as left aligned from y-axis at a fixed distance. My code for producing the plot is as follows,

ggplot(np4, aes(x = Ord, y = Value/1000, fill = Item)) +
  geom_bar(stat = "identity") +
  scale_y_continuous(expand = expansion(mult = c(.00, .6)), labels = comma) +
  coord_flip() +
  facet_wrap(~Year, scales = "free", drop = T, nrow = 2) +
  labs(title = "Nepal's Export Commodities and Destinations, Mln USD",
       caption = "Source: faostat") +
  theme(legend.position = "none",
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        plot.caption = element_text(face = "italic")) +
  geom_text(aes(label=paste0(Item,"-", Partner)), angle = 0,
            vjust=.3, hjust = -.1, size=3)

The image produced from the above code is as follows. The text are following the trailing end of the bar, but they are going beyond the plot area. They are managed by scale_y_continuous(expand = expansion(mult = c(.00, .6))) but it is only rational upto a point. If text details are equally important to bars then their placement should be all aligned as left-algined and they should begin from the trailing end of the samllest bar. They can continue overlap on the larger sized bars but their left-aligned symmetry is more important for delivering the required information.

图片

I would appreciate if the above requirement is satisfied with a minimal code lines.

Data for the above plot is as follows.

structure(list(Year = c(1999, 1999, 1999, 1999, 1999, 1999, 1999, 
1999, 1999, 1999, 1999, 1999, 1999, 1999, 1999, 2003, 2003, 2003, 
2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 
2003, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 2009, 
2009, 2009, 2009, 2009, 2009, 2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2010, 2011, 2011, 
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 
2011, 2011, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2013, 2013, 2013, 2013, 2013, 
2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2014, 
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 
2014, 2014, 2014, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 
2015, 2015, 2015, 2015, 2015, 2015, 2015, 2016, 2016, 2016, 2016, 
2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 
2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 
2017, 2017, 2017, 2017, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018), Partner = c("Bangladesh", 
"Bangladesh", "China, mainland", "India", "India", "India", "India", 
"India", "India", "India", "India", "India", "India", "India", 
"USA", "India", "India", "India", "Portugal", "India", "India", 
"India", "India", "India", "India", "India", "Unspecified Area", 
"Bangladesh", "India", "India", "Bangladesh", "India", "India", 
"India", "India", "Bangladesh", "India", "India", "India", "Turkey", 
"UAE", "India", "India", "India", "India", "Bangladesh", "India", 
"India", "India", "India", "India", "India", "India", "India", 
"India", "India", "India", "India", "USA", "India", "India", 
"Bangladesh", "India", "India", "India", "India", "India", "India", 
"India", "India", "India", "India", "India", "Thailand", "India", 
"India", "Bangladesh", "India", "India", "India", "India", "India", 
"India", "China, mainland", "India", "Thailand", "India", "Afghanistan", 
"India", "India", "India", "India", "India", "India", "Bangladesh", 
"India", "India", "India", "Afghanistan", "China, mainland", 
"India", "India", "India", "Afghanistan", "India", "India", "India", 
"India", "India", "Bangladesh", "Afghanistan", "India", "China, mainland", 
"India", "India", "India", "India", "India", "India", "India", 
"India", "India", "India", "India", "India", "Afghanistan", "Bangladesh", 
"India", "India", "India", "India", "India", "India", "India", 
"USA", "India", "India", "India", "Bangladesh", "India", "India", 
"India", "India", "India", "India", "USA", "India", "Vietnam", 
"Malaysia", "India", "India", "India", "India", "India", "India", 
"Bangladesh", "USA", "India", "India", "Vietnam", "India", "India", 
"India", "China, mainland", "Malaysia", "India", "India", "India", 
"India", "USA", "India", "India", "India", "India", "India", 
"India", "USA", "France", "USA", "India"), Item = c("Lentils", 
"Rice, milled", "Flour, wheat", "Cake, mustard", "Lentils", "Nutmeg, mace and cardamoms", 
"Food prep nes", "Juice, orange, concentrated", "Ginger", "Flour, wheat", 
"Crude materials", "Macaroni", "Food wastes", "Oil, rice bran", 
"Oilseeds nes", "Lentils", "Nutmeg, mace and cardamoms", "Food prep nes", 
"Sugar refined", "Tea", "Oil, vegetable origin nes", "Cake, rapeseed", 
"Food wastes", "Macaroni", "Crude materials", "Ginger", "Fat nes, prepared", 
"Lentils", "Oil, coconut (copra)", "Juice, orange, concentrated", 
"Lentils", "Nutmeg, mace and cardamoms", "Crude materials", "Juice, fruit nes", 
"Tea", "Wheat", "Food prep nes", "Juice, orange, single strength", 
"Ginger", "Lentils", "Lentils", "Macaroni", "Nuts nes", "Food wastes", 
"Juice, apple, single strength", "Lentils", "Crude materials", 
"Nutmeg, mace and cardamoms", "Tea", "Beverages, non alcoholic", 
"Nuts nes", "Ginger", "Food prep nes", "Juice, fruit nes", "Juice, orange, single strength", 
"Macaroni", "Cake, rapeseed", "Oil, vegetable origin nes", "Lentils", 
"Juice, apple, single strength", "Nutmeg, mace and cardamoms", 
"Lentils", "Crude materials", "Tea", "Juice, fruit nes", "Nuts nes", 
"Juice, orange, single strength", "Cake, rapeseed", "Macaroni", 
"Ginger", "Juice, apple, single strength", "Juice, pineapple", 
"Oil, vegetable origin nes", "Meat, cattle, boneless (beef & veal)", 
"Food prep nes", "Nutmeg, mace and cardamoms", "Lentils", "Juice, fruit nes", 
"Tea", "Crude materials", "Ginger", "Juice, orange, single strength", 
"Cake, rapeseed", "Crude materials", "Juice, apple, single strength", 
"Meat, cattle, boneless (beef & veal)", "Macaroni", "Food prep nes", 
"Juice, pineapple", "Buffaloes", "Nutmeg, mace and cardamoms", 
"Juice, fruit nes", "Tea", "Areca nuts", "Lentils", "Crude materials", 
"Juice, orange, single strength", "Ginger", "Tobacco products nes", 
"Crude materials", "Cake, rapeseed", "Juice, apple, single strength", 
"Macaroni", "Food prep nes", "Juice, pineapple", "Nuts nes", 
"Nutmeg, mace and cardamoms", "Juice, fruit nes", "Tea", "Lentils", 
"Tobacco products nes", "Crude materials", "Crude materials", 
"Juice, orange, single strength", "Cake, rapeseed", "Macaroni", 
"Juice, apple, single strength", "Food wastes", "Ginger", "Juice, pineapple", 
"Nutmeg, mace and cardamoms", "Juice, fruit nes", "Nuts nes", 
"Tea", "Crude materials", "Tobacco products nes", "Lentils", 
"Cake, rapeseed", "Juice, orange, single strength", "Food wastes", 
"Ginger", "Juice, apple, single strength", "Macaroni", "Juice, pineapple", 
"Pet food", "Nutmeg, mace and cardamoms", "Juice, fruit nes", 
"Tea", "Lentils", "Cake, rapeseed", "Crude materials", "Macaroni", 
"Juice, orange, single strength", "Ginger", "Juice, apple, single strength", 
"Pet food", "Juice, pineapple", "Meat, cattle, boneless (beef & veal)", 
"Tobacco products nes", "Oil, soybean", "Nutmeg, mace and cardamoms", 
"Juice, fruit nes", "Tea", "Cake, rapeseed", "Crude materials", 
"Lentils", "Pet food", "Juice, orange, single strength", "Macaroni", 
"Meat, cattle, boneless (beef & veal)", "Ginger", "Juice, pineapple", 
"Juice, apple, single strength", "Sugar confectionery", "Tobacco products nes", 
"Beverages, non alcoholic", "Nutmeg, mace and cardamoms", "Oil, palm", 
"Crude materials", "Pet food", "Food prep nes", "Oil, soybean", 
"Feed, compound nes", "Ginger", "Cake, soybeans", "Spices nes", 
"Oil, essential nes", "Oil, essential nes", "Food prep nes", 
"Oil, essential nes"), Value = c(11649, 5283, 3988, 3961, 3788, 
3592, 2454, 2372, 2289, 2203, 1650, 1516, 1381, 1333, 1296, 9346, 
5816, 5230, 5071, 3963, 3723, 3597, 3264, 3004, 2916, 2845, 2732, 
2645, 2422, 1889, 44045, 16949, 15539, 15404, 15119, 15059, 9961, 
6512, 5208, 4716, 4583, 4320, 4285, 3382, 3314, 42515, 25401, 
15801, 15169, 11555, 6549, 6157, 5937, 5517, 4348, 4003, 3481, 
2941, 2399, 2250, 30460, 23839, 20133, 16490, 16047, 11100, 8780, 
7463, 6444, 4809, 4110, 3473, 2705, 1841, 1780, 38868, 31320, 
17720, 15734, 12839, 10745, 8013, 6426, 6324, 4158, 3530, 3374, 
2969, 2887, 2699, 43224, 23404, 17065, 16966, 14896, 9645, 9294, 
7997, 7589, 6549, 5556, 4619, 4227, 3688, 3588, 36906, 32832, 
24910, 17843, 17767, 15799, 11098, 9488, 8291, 6243, 5807, 5021, 
4076, 3827, 3418, 42866, 22956, 17090, 15966, 10380, 9116, 6567, 
5807, 5049, 4686, 4411, 4221, 4114, 2482, 2237, 36324, 29671, 
23268, 12133, 11531, 9581, 5997, 5594, 5144, 4123, 3702, 2762, 
2669, 1775, 1362, 43440, 31226, 24649, 15143, 14893, 8555, 6221, 
6187, 5539, 4528, 3913, 3620, 3082, 2141, 1665, 38863, 31839, 
14575, 12746, 8417, 5903, 4552, 3845, 2721, 1311, 1208, 1141, 
797, 538, 523), Ord = c(15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 
7L, 6L, 5L, 4L, 3L, 2L, 1L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 
8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 15L, 14L, 13L, 12L, 11L, 10L, 
9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 15L, 14L, 13L, 12L, 11L, 
10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 15L, 14L, 13L, 12L, 
11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 15L, 14L, 13L, 
12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 15L, 14L, 
13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 15L, 
14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 
15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 
1L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 
2L, 1L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 
3L, 2L, 1L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 
4L, 3L, 2L, 1L)), row.names = c(NA, -180L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(Year = c(1999, 
2003, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
), .rows = structure(list(1:15, 16:30, 31:45, 46:60, 61:75, 76:90, 
    91:105, 106:120, 121:135, 136:150, 151:165, 166:180), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE))

Thanks.

A straightforward way to do this would be to set y = 5 inside aes in geom_text and put scales = "fixed" inside the facet_wrap call:

ggplot(np4, aes(x = Ord, y = Value/1000, fill = Item)) +
  geom_bar(stat = "identity") +
  scale_y_continuous(expand = expansion(mult = c(.00, .6)), labels = comma) +
  coord_flip() +
  facet_wrap(~Year, drop = TRUE, nrow = 2) +
  labs(title = "Nepal's Export Commodities and Destinations, Mln USD",
       caption = "Source: faostat") +
  theme(legend.position = "none",
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        plot.caption = element_text(face = "italic")) +
  geom_text(aes(y = 5, label = paste0(Item,"-", Partner)), angle = 0,
            vjust= 0.3, hjust = 0, size=3)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM