我的資料框如下:
df2 <- structure(list(City = c("Naypyitaw", "Yangon", "Yangon", "Naypyitaw",
"Mandalay", "Mandalay", "Mandalay", "Naypyitaw", "Yangon"), month = structure(c(1L,
1L, 3L, 3L, 1L, 3L, 2L, 2L, 2L), .Label = c("Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
), class = "factor"), gross.income = c(1925.461, 1841.9585, 1793.2915,
1771.383, 1770.2885, 1647.4925, 1639.251, 1568.3325, 1421.9105
)), row.names = c(NA, -9L), groups = structure(list(City = c("Mandalay",
"Naypyitaw", "Yangon"), .rows = structure(list(5:7, c(1L, 4L,
8L), c(2L, 3L, 9L)), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
我正在嘗試再次繪制幾個月的條形圖 Gross.income。雖然我得到的情節如下:

我不想要按組的顏色。我想要這樣的東西:

我的代碼:
df2$month <- factor(df2$month, levels = month.abb)
plot3 <- ggplot(df2, aes(x = month, y = gross.income, fill = gross.income))
geom_bar(stat = "identity")
scale_x_discrete(limits = month.abb[1:3])
uj5u.com熱心網友回復:
如果您只需要每個條形圖一種顏色,則每個month. 因此你需要在summarise你的資料之前ggplot()。
library(tidyverse)
df2 %>%
group_by(month) %>%
summarize(gross.income = sum(gross.income)) %>%
ggplot(aes(month, gross.income, fill = gross.income))
geom_bar(stat = "identity")

由reprex 包于 2022-03-12 創建(v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/443258.html
上一篇:如何使用scale_fill_manual手動定義條形圖顏色
下一篇:當有兩個y變數時如何添加圖例?
