[英]How to make a Grouped Bar chart for multiple columns?
我想制作一個分組條形圖,其中每個月都聚集在一起,然后在該集群中,每個變量都有自己的條形圖。 因此,對於下面的示例代碼(我的實際數據超過 2 行),我想要一月份的 x1、x2、x3 和 x4 的條形圖,然后再為 x1、x2、x3 和 x4 的條形圖二月等
x1 | x2 | x3 | x4 | 月 |
---|---|---|---|---|
7.8 | 3.4 | 9.8 | 2.1 | 簡 |
5.5 | 2.3 | 6.1 | 0.7 | 簡 |
我一定遺漏了一些相當明顯的東西,但我不知道如何使每列成為條形。 這是我目前最好的猜測
Levels = c(x1, x2, x3, x4)
Plot <- ggplot(data, aes(x = Month, fill = Levels)) + geom_col(position = 'dodge')
這顯然是錯誤的,因為 Levels 沒有以任何方式連接到數據。
任何想法,將不勝感激。
library(tidyverse)
df %>%
pivot_longer(
-Month
) %>%
ggplot(aes(x = Month, y = value, fill=name)) +
geom_bar(position="dodge", stat="identity")
數據:
df <- structure(list(x1 = c(7.8, 5.5, 4.8, 5.5), x2 = c(3.4, 2.3, 6.4,
7.3), x3 = c(9.8, 6.1, 9.8, 3.1), x4 = c(2.1, 0.7, 2.1, 7.7),
Month = c("Jan", "Jan", "Feb", "Feb")), class = "data.frame", row.names = c(NA,
-4L))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.