在ggplot中,在距y轴固定距离处标记geom_bar图

问题描述

我想将条形文字标签从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)))管理,但从某种意义上讲只是合理的。如果文本详细信息对小节同样重要,则它们的位置应全部与左对齐对齐,并且应从最小小节的尾端开始。它们可以继续在较大尺寸的条上重叠,但是它们的左对齐对称性对于传递所需的信息更为重要。

image

如果以最少的代码行满足上述要求,我将不胜感激。

以上图表的数据如下。

structure(list(Year = c(1999,1999,2003,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2018),Partner = c("Bangladesh","Bangladesh","China,mainland","India","USA","Portugal","Unspecified Area","Turkey","UAE","Thailand","Afghanistan","Vietnam","Malaysia","France","India"),Item = c("Lentils","Rice,milled","Flour,wheat","Cake,mustard","Lentils","Nutmeg,mace and cardamoms","Food prep nes","Juice,orange,concentrated","Ginger","Crude materials","Macaroni","Food wastes","Oil,rice bran","Oilseeds nes","Sugar refined","Tea",vegetable origin nes",rapeseed","Fat nes,prepared",coconut (copra)",fruit nes","Wheat",single strength","Nuts nes",apple,"Beverages,non alcoholic",pineapple","Meat,cattle,boneless (beef & veal)","buffaloes","Areca nuts","Tobacco products nes","Pet food",soybean","Sugar confectionery",palm","Feed,compound nes",soybeans","Spices nes",essential nes",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,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,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,1L)),row.names = c(NA,-180L),class = c("grouped_df","tbl_df","tbl","data.frame"),groups = structure(list(Year = c(1999,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"))),-12L),class = c("tbl_df",.drop = TRUE))

谢谢。

解决方法

一种简单的方法是在y = 5的{​​{1}}内设置aes并将geom_text放在scales = "fixed"调用内:

facet_wrap

enter image description here