我認為我用于分組條形圖的代碼本身大致正確。但是,我的 tibble 無法呼叫我需要的三個類別(視圖、互動和評論)。我在做這項作業時有一個概念上的問題。
這就是我要在 ggplot2 中執行的操作:
bp_vic <- ggplot(data, aes(x = Day, y = value, fill = category))
geom_bar(position = 'dodge', stat = 'identity')
bp_vic
值和填充可能關閉。但是,我認為主要問題是沒有正確的類別呼叫。
我的 tibble 有六列。最后三個是我想要繪制的。視圖、互動和評論的整數計數。

或者,Category如果需要,您也可以輕松地繪制而不是一天,通過使用x = Category而不是x = Day.

如果您想更改類別的順序,那么您可以創建Category一個因子,您可以在不更改資料框的情況下執行此操作。
# Create order for the categories. If you want to do it by the number of views, then you can create the list from your dataframe.
level_order <- data %>%
dplyr::filter(Section == "Views") %>%
dplyr::arrange(desc(values)) %>%
pull(Category) %>%
unique()
# Then, set category as a factor and include the ordered categories.
ggplot(data, aes(fill = Section, y = values, x = factor(Category, level = level_order)))
geom_bar(position = "dodge", stat = "identity")

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364231.html
上一篇:分組資料上的ggplot方面
