【问题标题】:Labelling geom_bar plot at a fixed distance from y-axis in ggplot在ggplot中在距y轴固定距离处标记geom_bar图
【发布时间】:2020-10-05 08:17:52
【问题描述】:

我想将条形文本标签放置为从 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))

谢谢。

【问题讨论】:

  • 您可以使用 dplyr:: group_by 和 mutate(mn = min(value)) 找到每年的最小值,然后将 mn 作为 x 美学添加到 geom_text。还建议缩短一些名称
  • 谢谢,缩短名称一直在我的脑海中。我不知道如何在geom_text 中使用aes。一个班轮将得到极大的认可。

标签: r ggplot2 geom-text


【解决方案1】:

执行此操作的简单方法是将y = 5 设置在aes 中的geom_text 中,并将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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    • 2015-11-02
    相关资源
    最近更新 更多