R 将数据框绘制为分组条形图
如何最好地将此数据集绘制为分组条形图,如下所示R 将数据框绘制为分组条形图,r,ggplot2,R,Ggplot2,如何最好地将此数据集绘制为分组条形图,如下所示 首先收集数据,然后使用ggplot2绘图。假设您的原始数据被称为df: library(tidyverse) df1 <- gather(df, Month, val, -Zone) ggplot(df1, aes(x=Zone, y=val, fill = Month)) + geom_bar(stat="identity", position=position_dodge()) 库(tidyverse) df1这里是一个更具重塑性
首先收集数据,然后使用
ggplot2
绘图。假设您的原始数据被称为df
:
library(tidyverse)
df1 <- gather(df, Month, val, -Zone)
ggplot(df1, aes(x=Zone, y=val, fill = Month)) +
geom_bar(stat="identity", position=position_dodge())
库(tidyverse)
df1这里是一个更具重塑性的数据集
zone count month
1 1127071 Jan
2 1025269 Jan
3 196059 Jan
4 43535 Jan
1 1193999 Feb
2 1002110 Feb
3 156429 Feb
4 39396 Feb
是否可以将计数
绘制为y,将月份
绘制为x,但按区域
分组
我成功地写下了这样的话:
ggplot(statLs, aes(month, count, fill=zone, group=zone)) +
geom_bar(stat="identity", position = "stack")
scale_x_discrete(limits=c("Jan", "Feb")) +
scale_fill_manual(values=c("red", "orange", "yellow", "grey"))
但这会返回一个错误
Error: Continuous value supplied to discrete scale
您对分组条形图的定义是什么?我想对条形图进行分组,以便在y轴中,Oct、Nov、Dec
中的值按区域进行分组,请查看如何在x轴中绘制月
,但按区域进行分组?
Error: Continuous value supplied to discrete scale