我有一些数据需要放入堆栈条形图中,但是当我添加计数标签时,有些标签在类别上方,而有些标签在类别下方。我尝试修改geom_text函数的位置参数无济于事。
下面是一个可重现的示例,显示了位于该类别上方的“ Under”类别座椅的标签和位于栏内的“ Over”类别座椅的标签。
library(tidyverse)
data.frame(AgeGroup = sample(c(rep("Over",10),"Under"), 6000, replace = TRUE),
DueDate = sample(
seq( as.Date("2015-01-01"),
as.Date("2015-06-30"), by="1 month") ,
6000,replace = TRUE),
stringsAsFactors = TRUE) %>%
group_by(AgeGroup,DueDate) %>%
tally() %>% ungroup %>%
ggplot() +
geom_bar(aes(x=DueDate, y=n, fill = AgeGroup),stat = "identity") +
geom_text(aes(x=DueDate, y=n
,label = prettyNum(n,big.mark = ","))
, vjust = 0, size = 2) +
scale_y_continuous(labels = scales::comma) +
theme_bw() +
labs(title="Where are the labels")
答案 0 :(得分:1)
只需将from flask_login import current_user
from my_app.tests.base import BaseTest
class MyTests(BaseTest):
def test_a(self):
with self.app:
print current_user
用作app.config['LOGIN_DISABLED'] = False
的{{1}}位置,它将始终落在栏内:
app.login_manager._login_disabled = False
编辑:该快速解决方案仅适用于您的特定示例。如果每个条形有两个以上类别,或者值分布更均匀,则它将不会显示。即:
n/2
因此,这是一个通用的解决方案,它在数据框(y
)上添加了一个“位置”列,以与geom_text()
一起使用并将标签准确地放置在它们所属的位置:
library(tidyverse)
data.frame(AgeGroup = sample(c(rep("Over",10),"Under"), 6000, replace = TRUE),
DueDate = sample(
seq( as.Date("2015-01-01"),
as.Date("2015-06-30"), by="1 month") ,
6000,replace = TRUE),
stringsAsFactors = TRUE) %>%
group_by(AgeGroup,DueDate) %>%
tally() %>% ungroup %>%
ggplot() +
geom_bar(aes(x=DueDate, y=n, fill = AgeGroup),stat = "identity") +
geom_text(aes(x=DueDate, y=n/2
,label = prettyNum(n,big.mark = ","))
, vjust = 0, size = 2) +
scale_y_continuous(labels = scales::comma) +
theme_bw() +
labs(title="Where are the labels")