R语言 将标签居中放置在并排条形图的顶部,ggplot2

z0qdvdin  于 2023-05-15  发布在  其他
关注(0)|答案(1)|浏览(130)

我有一个读到R的表。它总结了每周记录的猫和狗的计数,每次记录新的一周数据时,都会添加额外的行。
然而,当我运行代码使用ggplot2制作并排条形图时,显示计数的标签偏离了中心。它们不直接位于杆的顶部中心。每个第一个标签都稍微向右,每个第二个标签都稍微向左。
到目前为止,我已经尝试了以下解决方案:

我尝试过的这些和其他解决方案要么使酒吧消失,要么不起作用。
我需要更改什么代码才能使标签直接位于条形图的中心?请在下面找到代码。
表:

df <- structure(list(Week = structure(c(1672704000, 1672704000, 1673222400, 
                                  1673222400, 1673827200, 1673827200, 1674432000, 1674432000, 1675036800, 
                                  1675036800, 1675641600, 1675641600, 1676246400, 1676246400, 1676851200, 
                                  1676851200, 1677456000, 1677456000, 1678060800, 1678060800, 1678665600, 
                                  1678665600, 1679270400, 1679270400, 1679875200, 1679875200, 1680480000, 
                                  1680480000, 1681084800, 1681084800, 1681689600, 1681689600, 1682294400, 
                                  1682294400, 1682985600, 1682985600, 1683590400, 1683590400), class = c("POSIXct", 
                                                                                                         "POSIXt"), tzone = "UTC"), Animal = structure(c(1L, 2L, 
                                                                                                                                                              1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
                                                                                                                                                              1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
                                                                                                                                                              1L, 2L, 1L, 2L), .Label = c("Cat", "Dog"
                                                                                                                                                              ), class = "factor"), count = c(63L, 100L, 56L, 210L, 40L, 200L, 
                                                                                                                                                                                              102L, 40L, 60L, 240L, 105L, 130L, 102L, 89L, 162L, 54L, 160L, 
                                                                                                                                                                                              50L, 160L, 75L, 160L, 75L, 120L, 80L, 124L, 85L, 136L, 60L, 110L, 
                                                                                                                                                                                              60L, 155L, 123L, 197L, 144L, 175L, 110L, 65L, 82L)), class = c("grouped_df", 
                                                                                                                                                                                                                                                           "tbl_df", "tbl", "data.frame"), row.names = c(NA, -38L), groups = structure(list(
                                                                                                                                                                                                                                                             Week = structure(c(1672704000, 1673222400, 1673827200, 1674432000, 
                                                                                                                                                                                                                                                                                1675036800, 1675641600, 1676246400, 1676851200, 1677456000, 
                                                                                                                                                                                                                                                                                1678060800, 1678665600, 1679270400, 1679875200, 1680480000, 
                                                                                                                                                                                                                                                                                1681084800, 1681689600, 1682294400, 1682985600, 1683590400
                                                                                                                                                                                                                                                             ), class = c("POSIXct", "POSIXt"), tzone = "UTC"), .rows = list(
                                                                                                                                                                                                                                                               1:2, 3:4, 5:6, 7:8, 9:10, 11:12, 13:14, 15:16, 17:18, 
                                                                                                                                                                                                                                                               19:20, 21:22, 23:24, 25:26, 27:28, 29:30, 31:32, 33:34, 
                                                                                                                                                                                                                                                               35:36, 37:38)), row.names = c(NA, -19L), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                                  "tbl", "data.frame"), .drop = TRUE))

图形代码:

df %>%
  ggplot(aes(x=Week, y=count, fill= Animal)) +
  geom_bar(position = "dodge", stat = "identity")+
  labs(x = "Date", y = 'Number of animals',title = "Cats and dogs data")+
  geom_text(aes(label = count), position = position_dodge(width=0.9), vjust = -0.5, size = 2)+
  theme_classic()+
  labs(fill=NULL)+
  scale_y_continuous(expand = c(0, 0), limits=c(0, max(df$count)*1.1))
1zmg4dgp

1zmg4dgp1#

您需要在geom_text中添加Animal作为分组变量。此外,Week变量是POSIX格式的,实际上是一个整数秒,因此需要以数十万秒的数量级进行处理。我发现大约500,000工作得很好:

df %>%
  ggplot(aes(x = Week, y = count, fill = Animal)) +
  geom_col(position = "dodge") +
  labs(x = "Date", y = 'Number of animals', title = "Cats and dogs data",
       fill = NULL) +
  geom_text(aes(label = count, group = Animal), 
            position = position_dodge(width = 5e5), vjust = -0.5, size = 2) +
  theme_classic() +
  scale_y_continuous(expand = c(0, 0), limits = c(0, max(df$count) * 1.1))

相关问题