Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将数据框绘制为分组条形图_R_Ggplot2 - Fatal编程技术网

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