好吧,我疯了。所以我有一些数据有一堆列。主要是姓名,地址和其他品质。
Name |||| Division | | |MonthNo
John |||| Seattle | | | | | 2
Chris|||| Seattle ||||||||| 4
Dave ||||Dallas |||||||||| 2
Suzy |||| San Fran |||| 5
Jill | | | | Dallas |||||||||| 4
要按月计算一个简单的发生次数,我就这样做了:
MC$MonthNo <- factor(MC$MonthNo, levels = c(1:11), labels = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November"))
MCH <- ggplot(na.omit(MC), aes(MonthNo))
MCH + geom_bar(color = "black", fill = "light blue") + labs(x = "2017", y = "# of Leads") + theme(axis.line = element_line(color = "black", size = 3, linetype = "solid"), axis.text.x = element_text(face = "bold", color = "black", size = 14), axis.text.y = element_text(face = "bold", color = "black", size = 14)) + scale_y_continuous(name = "# Of Leads", breaks = c(0, 500, 1000, 1500, 2000, 2500, 3000), limits = c(0, 3000)) + theme(panel.background = element_rect(colour = "black", fill = "white"), panel.grid.minor = element_line(color = "black", size = .5), panel.grid.major = element_line(color = "black", size = .5))
几乎只是一个基本的条形图。
现在这个数据中有一个名为Division的字段。有7个不同的部门,例如西雅图,匹兹堡,檀香山,达拉斯,旧金山,盐湖城,新奥尔良。我希望根据它们所处的划分将我的条形图分成堆栈。就像7个不同的条形区域一样,7个区段代表每个区域的每个区域的计数。就像在我的例子中,第2个月有西雅图和达拉斯。我希望在一个栏中有两个段。对于我的生活,我无法理解这一点。所有内容都是从包含几千个唯一行的CSV导入的。
提前致谢。
答案 0 :(得分:3)
你走了。 我将您的数据保存为csv
petey <-read.csv("Petey_data.txt")
#Adding a month variable, always good practice to
#keep original data as is and add variables
#I used the built-in month.abb constant to get
#abbreviated month names
#assigned as factor with labels as month order
petey$Month <-factor(month.abb[petey$MonthNo], levels = month.abb)
library(ggplot2)
ggplot(petey,aes(Month))+geom_bar(aes(fill=Division))
将来请查看?dput
以分享您的数据
还阅读?geom_bar
并查看示例以了解数据的排列方式。有关http://ggplot2.tidyverse.org/reference/geom_bar.html