簡體   English   中英

在ggplot中與y軸的固定距離處標記geom_bar圖

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

我想將條形文本標簽放置為從 y 軸以固定距離左對齊。 我用於生成情節的代碼如下,

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)

上述代碼生成的圖像如下。 文本跟隨條的尾端,但超出了繪圖區域。 它們由scale_y_continuous(expand = expansion(mult = c(.00, .6)))但它只是在一定程度上是合理的。 如果文本細節對條形圖同樣重要,那么它們的位置應該全部對齊為左對齊,並且它們應該從最小條形的尾端開始。 它們可以在較大尺寸的條上繼續重疊,但它們的左對齊對稱性對於提供所需的信息更為重要。

圖片

如果以最少的代碼行滿足上述要求,我將不勝感激。

上圖的數據如下。

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))

謝謝。

一種直接的方法是在geom_text aes內設置y = 5並將scales = "fixed"放在facet_wrap調用中:

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)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM