为什么我在 ggplot 中的条形图在 ggbarplot 中看起来很奇怪?
Why is my bar graph in ggplot looking strange in ggbarplot?
这是我 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 帮助页面,但我似乎无法真正找出我缺少哪个参数来解决这个问题,或者如果它只是我自己的过滤错误我需要修复。
ggbarplot
用黑色轮廓为每条记录画了一个条,所以你需要 filter
month == 12
然后 group_by
type1
和 summarize
调用前的数量 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 创建
这是我 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 帮助页面,但我似乎无法真正找出我缺少哪个参数来解决这个问题,或者如果它只是我自己的过滤错误我需要修复。
ggbarplot
用黑色轮廓为每条记录画了一个条,所以你需要 filter
month == 12
然后 group_by
type1
和 summarize
调用前的数量 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 创建