本文介绍了条形图上的geom_text位置中间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是可复制的代码:

<$ (ggmap)
库(ggplot2)
库(gganimate)
库(readxl)
库(动画)

mydf1 2000,2001,2001,2002,2002,2004,2005,2005,2006 ,2006年,2007年,
2007,2008,2008,2009,2010,2011,2012,2013,2014,2015,2015,
2016,2016,2016,2016),N = c(3L,图1L,1L,4L,1L,4L,1L,2L,
1L,5L,1L,2L,1L,1L,4L,1L,3L,1L,2L,1L,5L,2L,
4L,1L,1L,2L,1L,1L,3L,4L,1L),信道= c(86,24,24,
68,6,34,22,10,14,24 ,16,43,4,20,56,15,20,8,11,
24,44,9,23,65,20,12,5,43,5,4,24,53,14 ),
可靠性= c(0.466666666666667,
,0.2,0.8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1),type = c(OIL,LNG,
AGS,OI LN,OIL,LNG,OIL,LNG,OIL,LNG,
OIL,OIL,LNG,OIL ,LNG,OIL,LNG,OIL,LNG,
OIL,OIL,OIL,OIL,OIL LNG,LNG,OIL,
AGS,LNG,LNG *,OIL)),.Names = c(year,N,
通道,可靠性,类型),row.names = c(NA,-33L),class =data.frame)

p3 geom_bar(stat =identity)+ labs(x =year,y =y )+
geom_text(aes(label = channels),vjust = 1,color =white,size = 3)+ theme_minimal()+
ggtitle(Data here,statistics)
p3

问题在于标签的位置 - 它们不匹配相应的酒吧。请参阅图片以获取解释。




Need help with the aligning of the labels on the barplot.

Here is the reproducible code:

library(ggmap)
library(ggplot2)
library(gganimate)
library(readxl)
library(animation)

mydf1<-structure(list(year = c(1994, 1995, 1997, 1997, 1998, 1998, 2000,
2000, 2001, 2001, 2002, 2002, 2004, 2005, 2005, 2006, 2006, 2007,
2007, 2008, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2015,
2016, 2016, 2016, 2016), N = c(3L, 1L, 1L, 4L, 1L, 4L, 1L, 2L,
1L, 5L, 1L, 2L, 1L, 1L, 4L, 1L, 3L, 1L, 2L, 1L, 5L, 2L, 2L, 6L,
4L, 1L, 1L, 2L, 1L, 1L, 3L, 4L, 1L), channels = c(86, 24, 24,
68, 6, 34, 22, 10, 14, 24, 16, 43, 4, 20, 56, 15, 20, 8, 11,
24, 44, 9, 23, 65, 20, 12, 5, 43, 5, 4, 24, 53, 14),
reliability = c(0.466666666666667,
1, 0.2, 0.8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), type = c("OIL", "LNG",
"AGS", "OIL", "LNG", "OIL", "LNG", "OIL", "LNG", "OIL", "LNG",
"OIL", "OIL", "LNG", "OIL", "LNG", "OIL", "LNG", "OIL", "LNG",
"OIL", "OIL", "OIL", "OIL", "OIL", "OIL", "LNG", "LNG", "OIL",
"AGS", "LNG", "LNG*", "OIL")), .Names = c("year", "N",
"channels", "reliability", "type"), row.names = c(NA, -33L), class = "data.frame")

p3<-ggplot(data=mydf1, aes(x=year, y=channels, frame=year, cumulative=TRUE, fill=type))+
geom_bar(stat="identity")+labs(x="year", y="y")+
geom_text(aes(label=channels), vjust=1, color="white", size=3)+theme_minimal()+
ggtitle("Data here", "statistics")
p3

The problem is in the positions of the labels - they don't match the corresponding bars. See the picture for explanation.

How to add labels in the middle of the corresponding bar?

解决方案

With recent updates to ggplot2, you can do this by setting position = position_stack(vjust = 0.5) in geom_text().

It may also interest you that you can use geom_col() as a shortcut for geom_bar(stat = "identity"). Also, labs has a title and subtitle parameter, so you don't need to use it and ggtitle.

ggplot(data = mydf1, aes(x = year, y = channels, fill = type, frame = year, cumulative = TRUE)) +
  geom_col() +
  labs(x = "year", y = "y", title = "Data Here", subtitle = "Statistics") +
  geom_text(aes(label = channels), color = "white", size = 3, position = position_stack(vjust = 0.5)) +
  theme_minimal()

这篇关于条形图上的geom_text位置中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 06:59