這是我 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 幫助頁面,但我似乎無法真正找出我缺少哪個引數來解決這個問題,或者如果它只是我自己的過濾錯誤我需要修復。
uj5u.com熱心網友回復:
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 包(v2.0.1)于 2022 年 1 月 4 日創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/403260.html
標籤:
下一篇:PHP寫入odbc資料源失敗
