Home > Back-end >  Why is my bar graph in ggplot looking strange in ggbarplot?
Why is my bar graph in ggplot looking strange in ggbarplot?

Time:01-04

Here is my data for December:

structure(list(case = c(21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 
22L, 22L, 22L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 
25L, 25L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 
26L, 26L, 26L, 27L, 27L, 27L, 27L, 28L, 28L, 29L, 29L, 29L, 29L, 
29L, 29L, 29L, 29L, 29L, 29L, 29L, 30L, 30L, 30L, 30L, 30L, 30L, 
30L, 30L, 30L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 
32L, 32L, 32L, 32L, 32L, 32L, 32L, 32L, 33L, 33L, 33L, 33L, 33L, 
33L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 35L, 35L, 
35L, 35L, 35L, 35L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 
36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 37L, 37L, 38L, 
38L, 38L, 38L, 38L, 38L, 39L, 39L, 39L, 39L, 39L, 39L, 39L, 39L, 
39L, 39L, 39L, 40L, 40L, 40L, 40L, 41L, 41L, 41L, 41L, 41L, 42L, 
42L, 42L, 43L, 43L, 43L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 44L, 
45L, 45L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 46L, 
47L, 47L, 47L, 47L, 47L, 47L, 47L, 48L, 48L, 48L, 48L, 49L, 49L, 
49L), year = c(2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L), month = c(12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L), day = c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 
5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 9L, 
9L, 9L, 9L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 
20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 
22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 25L, 25L, 
25L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 27L, 27L, 28L, 28L, 
28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 29L, 29L, 29L, 29L, 
29L, 29L, 29L, 30L, 30L, 30L, 30L, 31L, 31L, 31L), type1 = c("drink", 
"food", "laundry", "laundry", "drink", "alcohol", "food", "alcohol", 
"food", "drink", "food", "food", "food", "alcohol", "food", "food", 
"food", "alcohol", "drink", "drink", "food", "drink", "travel", 
"travel", "food", "drink", "food", "travel", "travel", "drink", 
"food", "drink", "food", "travel", "food", "travel", "travel", 
"travel", "food", "drink", "misc", "misc", "misc", "misc", "food", 
"food", "food", "food", "misc", "misc", "food", "food", "food", 
"alcohol", "alcohol", "food", "wifi", "electric", "rent", "food", 
"food", "drink", "drink", "drink", "food", "food", "misc", "drink", 
"drink", "misc", "food", "food", "food", "food", "food", "food", 
"drink", "food", "travel", "alcohol", "food", "drink", "food", 
"food", "food", "travel", "laundry", "laundry", "alcohol", "food", 
"food", "food", "drink", "food", "drink", "travel", "travel", 
"food", "food", "alcohol", "food", "drink", "food", "misc", "food", 
"drink", "alcohol", "food", "travel", "travel", "travel", "drink", 
"travel", "drink", "food", "alcohol", "travel", "drink", "travel", 
"drink", "food", "misc", "food", "food", "food", "food", "food", 
"food", "alcohol", "food", "drink", "alcohol", "food", "travel", 
"food", "alcohol", "food", "alcohol", "travel", "travel", "food", 
"food", "drink", "food", "misc", "misc", "food", "misc", "alcohol", 
"food", "food", "food", "drink", "misc", "travel", "drink", "food", 
"drink", "food", "alcohol", "food", "food", "alcohol", "food", 
"drink", "misc", "food", "alcohol", "food", "travel", "laundry", 
"food", "drink", "misc", "laundry", "food", "food", "drink", 
"food", "food", "travel", "drink", "alcohol", "alcohol", "food", 
"food", "alcohol", "food", "food", "food", "misc", "misc", "misc", 
"misc", "food", "drink", "food", "food", "food", "food", "food", 
"food", "alcohol", "food"), amount = c(9.5, 7, 14, 24, 13, 32, 
20, 29, 23.9, 21, 73.8, 16.9, 53.8, 12.9, 17, 16.9, 8.5, 35, 
49, 12, 26, 18, 10, 8, 41, 11, 12, 10, 8, 8, 18, 18, 10, 20, 
378, 8, 5, 6, 109, 14, 46, 14.9, 8, 29.9, 10.5, 19.9, 12.9, 36.8, 
21.9, 24.9, 10, 7, 8, 35, 24, 20, 436, 99, 7300, 26.9, 45.9, 
13.9, 11.9, 9.9, 12, 9.9, 0.5, 12.9, 90, 33, 15.5, 24, 40, 12, 
10, 12, 14, 8, 10, 12.9, 10.5, 27.9, 13.5, 24.9, 19.9, 8, 28, 
24, 42.8, 19, 48, 21, 52, 7.5, 18.5, 5, 4, 100, 32.9, 29.8, 26, 
8.5, 10, 300, 53, 13, 223, 14, 11, 8, 5.7, 20, 40, 14, 20, 27, 
5.7, 20, 17.8, 70, 170, 39.9, 27, 20, 22, 24, 10.5, 20, 26, 31, 
20, 26.4, 37.5, 18.1, 190, 25.9, 100, 26, 8, 11, 41, 16, 18, 
14, 28, 13, 10.5, 59, 8, 18.9, 73.8, 7.5, 52, 0.5, 11.2, 18.5, 
75, 28, 15, 24, 10, 16.9, 20.2, 73, 18, 14.5, 10, 35, 37, 5.6, 
15, 37, 17.5, 143, 24, 27, 12, 9.5, 12, 41, 5.6, 38, 44, 60, 
14.5, 34, 30, 19.9, 33.9, 24.3, 60, 12, 12, 35, 12, 52, 15, 10.5, 
8.5, 13, 49.5, 27, 31.9, 17)), class = "data.frame", row.names = c(NA, 
-204L))

I usually have a pretty seamless transition from ggplot to ggpubr, but today I'm having an issue with the bar graph function. When I make this pretty simple bar in ggplot:

budget %>% 
  filter(month==12) %>%
  ggplot(aes(x=type1,
         weight = amount,
         fill = type1)) 
  geom_bar() 
  theme_bw() 
  labs(title = "Bar Graph : Spending by Type for December",
       x="Type of Spending",
       y="Amount (HKD)") 
  theme(legend.position = "none")

I get this normal bar:

enter image description here

However when I try to do the same in ggbarplot:

budget %>% 
  filter(month==12) %>% 
  ggbarplot(x="type1",
         y="amount",
         fill = "type1") 
  theme_bw() 
  theme(legend.position = "none") 
  labs(title = "Bar Graph : Spending by Type for December",
       x="Type of Spending",
       y="Amount (HKD)")

I get this weird bar graph that facets by each amount spent:

enter image description here

Tried reviewing the ggbarplot help page in R, but I can't seem to really pick out which argument I'm missing to fix this or if its just my own filtering error I need to fix.

CodePudding user response:

ggbarplot drew a bar for every record with a black outline so you need to filter the month == 12 then group_by the type1 and summarize the amount before calling ggbarplot then it will work fine.

# I recognize that in your example type1 data is not factorized
# Which results different color code on different graph. adjusted a bit now
budget$type1 <- factor(budget$type1)

budget %>%
  filter(month==12) %>% 
  group_by(type1) %>%
  summarize(amount = sum(amount)) %>%
  ggbarplot(x="type1",
            y="amount",
            fill = "type1")  
  theme_bw() 
  theme(legend.position = "none") 
  labs(title = "Bar Graph : Spending by Type for December",
       x="Type of Spending",
       y="Amount (HKD)")

Or you specifying the color aesthetic as well so that the outline is the same color with the fill.


budget %>% 
  filter(month==12) %>% 
  ggbarplot(x="type1",
            y="amount",
            fill = "type1",
            color = "type1")  
  theme_bw() 
  theme(legend.position = "none") 
  labs(title = "Bar Graph : Spending by Type for December",
       x="Type of Spending",
       y="Amount (HKD)")

Created on 2022-01-04 by the reprex package (v2.0.1)

  •  Tags:  
  • Related