【问题标题】:Why is my bar graph in ggplot looking strange in ggbarplot?为什么我在 ggplot 中的条形图在 ggbarplot 中看起来很奇怪?
【发布时间】:2022-01-03 03:26:02
【问题描述】:

这是我 12 月的数据:

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

我通常从 ggplot 到 ggpubr 的过渡非常无缝,但今天我遇到了条形图功能的问题。当我在 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")

我得到这个正常的酒吧:

但是,当我尝试在 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)")

我得到了这个奇怪的条形图,它按每个花费的金额分面:

尝试查看 R 中的 ggbarplot 帮助页面,但我似乎无法真正找出我缺少哪个参数来解决这个问题,或者我需要修复它是否只是我自己的过滤错误。

【问题讨论】:

  • 我无法使用您的代码重现情节。数据仅包含第 11 个月。
  • 我猜是因为你在函数ggbarplot() 中有一个y,而你在ggplot() 中使用了weight。它们是不等价的。
  • 是的,我知道它们不同,但我不知道 ggbarplot 是否有 weight 参数。我只知道它必须有一个 y 参数,我认为它最好等同于 weight
  • 请添加第 12 个月的数据。我无法用给定的数据重现问题。
  • 我已将其编辑为仅包含 12 月

标签: r ggplot2 ggpubr


【解决方案1】:

ggbarplot 为每条带有黑色轮廓的记录绘制了一个条形图,因此您需要先filtermonth == 12 然后group_bytype1summarize 调用ggbarplot 之前的金额然后它会起作用很好。

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

或者您也指定颜色美学,以便轮廓与填充颜色相同。


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

reprex package (v2.0.1) 于 2022-01-04 创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    相关资源
    最近更新 更多