0

I have a table read into R. It summarises the count of cats and dogs recorded each week, and every time a new week of data is recorded, extra rows are added.

However, when I run the code to make a side-by-side bar graph using ggplot2, the labels showing the count are off-centre. They are not directly top centre of the bars. Every first label is slightly to the right, and every second label is slightly to the left.

So far, I have tried the following solutions, among others:

These and other solutions I have tried either make the bars disappear or do not work.

What code would I need to change to get the labels directly centre of the bars? Please find the code below.

Table:

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))

graph code:

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))

1 Answers1

0

You need to add Animal as a grouping variable in geom_text. Also, your Week variable is in POSIX format, which is actually an integer number of seconds, so you'll need to dodge in the order of hundreds of thousands of seconds. I found about 500,000 worked well:

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))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87