并排条形图顶部的中心标签,ggplot2

问题描述 投票:0回答:1

我有一个读入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))

r ggplot2
1个回答
0
投票

您需要在

Animal
中添加
geom_text
作为分组变量。另外,您的
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))

© www.soinside.com 2019 - 2024. All rights reserved.