geom_text 标签不想在我的条形图上闪避

The geom_text label does not want to dodge on my bar chart

我的条形图上的标签有问题。我尝试了该网站的不同解决方案,但没有任何变化。

这是我的数据示例:

   <date>     <chr>                     <int>
 1 2019-03-01 Membershiptype1               2
 2 2019-04-01 Membershiptype1               1
 3 2019-07-01 Membershiptype1               1
 4 2019-08-01 Membershiptype1               1
 5 2019-09-01 Membershiptype1               2
 6 2019-10-01 Membershiptype1               1
 7 2019-11-01 Membershiptype1               1
 8 2020-01-01 Membershiptype1               2
 9 2020-01-01 Membershiptype2               2
10 2020-02-01 Membershiptype1               4

我使用了这些软件包: 库(dbplot) 图书馆(模型数据库) 图书馆(整洁的预测) 图书馆(整洁的宇宙) 图书馆(润滑)

这是我用来制作条形图的代码:

signupMT <- ggplot(SignupPM, aes(fill=`Membership type`,  y=count_by_Name, x=month)) + 
  geom_bar(position=position_dodge(preserve = 'single'), stat="identity") +
  scale_fill_manual(values=c("#2381EB", "#8442D1", "#CF07BA"), name = NULL, labels = c("Student/Unemployed", "Full member", "Online member")) +
  theme_classic() +
  labs( x= NULL, y= "Number of sign-ups", title = "Sing-ups per month by membership type")+
  theme(legend.position="bottom", 
        axis.text.x = element_text(angle = 45, hjust = 0.1, vjust = 0))+
  scale_x_date(breaks= "month",date_labels = "%b %Y") +
  geom_text(aes(label= count_by_Name, group = month), position=position_dodge(width = 1), vjust=-0.5)

我也试过 group = 'membership type' 或调整宽度值但没有任何变化。

我也试过这个建议,但后来收到错误消息:stat_count() can only have an x​​ or y aesthetic。

geom_text(aes(y=count_by_Name, label = stat(count)), stat = 'count', position=position_dodge(2), vjust=-0.5, size = 3)

这是我得到的条形图:

我在这里错过了什么?

使用@teunbrand 的伟大建议,这可能会有所帮助:

library(tidyverse)

#Data
SignupPM <- structure(list(month = structure(c(17956, 17987, 18078, 18109, 
18140, 18170, 18201, 18262, 18262, 18293), class = "Date"), Membership_type = c("Membershiptype1", 
"Membershiptype1", "Membershiptype1", "Membershiptype1", "Membershiptype1", 
"Membershiptype1", "Membershiptype1", "Membershiptype1", "Membershiptype2", 
"Membershiptype1"), count_by_Name = c(2L, 1L, 1L, 1L, 2L, 1L, 
1L, 2L, 2L, 4L)), row.names = c(NA, -10L), class = "data.frame")

#Code
ggplot(SignupPM, aes(fill=Membership_type,  y=count_by_Name, x=month)) + 
  geom_col(position = position_dodge2(width = 0.9, preserve = "single")) +
  scale_fill_manual(values=c("#2381EB", "#8442D1", "#CF07BA"), name = NULL, labels = c("Student/Unemployed", "Full member", "Online member")) +
  theme_classic() +
  labs( x= NULL, y= "Number of sign-ups", title = "Sing-ups per month by membership type")+
  theme(legend.position="bottom", 
        axis.text.x = element_text(angle = 45, hjust = 0.1, vjust = 0))+
  scale_x_date(breaks= "month",date_labels = "%b %Y") +
  geom_text(aes(label= count_by_Name, group = month), position=position_dodge(width=0.9), vjust=-0.5)

grid 图形系统(ggplot2 所基于的系统)而言,文本元素的宽度为零,因此无法像绘图列那样躲避。解决这个问题的方法是使用 hjust 作为美学映射:

ggplot(SignupPM, aes(y = count_by_Name, x = month)) + 
  geom_col(aes(fill = `Membership type`),
            position = position_dodge(preserve = 'single')) +
  geom_text(aes(label= count_by_Name, 
                hjust = - 2 * as.numeric(`Membership type`) + 4.3),
            vjust = -0.5) +
  scale_x_date(breaks = "month", date_labels = "%b %Y") +
  scale_fill_manual(values = c("#2381EB", "#8442D1", "#CF07BA"), 
                    name = NULL, 
                    labels = c("Student/Unemployed", 
                               "Full member", "Online member")) +
  labs( x = NULL, y = "Number of sign-ups", 
        title = "Sign-ups per month by membership type") +
  theme_classic() +
  theme(legend.position = "bottom", 
        axis.text.x     = element_text(angle = 45, hjust = 0.1, vjust = 0))

数据(根据相关情节推断)

SignupPM <- structure(list(count_by_Name = c(2L, 1L, 1L, 1L, 2L, 1L, 1L, 
  2L, 2L, 4L, 2L, 4L, 1L, 4L, 2L, 5L, 5L, 5L, 4L, 1L, 3L, 2L, 8L
  ), month = structure(c(17928, 17956, 18048, 18078, 18109, 18140, 
  18170, 18231, 18231, 18262, 18262, 18293, 18293, 18322, 18322, 
  18353, 18353, 18383, 18383, 18383, 18414, 18414, 18414), class = "Date"), 
      `Membership type` = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
      1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 3L, 1L, 2L, 
      3L), .Label = c("Membershiptype1", "Membershiptype2", "Membershiptype3"
      ), class = "factor")), row.names = c(NA, -23L), class = "data.frame")