组列表在线图中绘制在R问题中

时间:2015-12-13 04:22:23

标签: r graph ggplot2 analysis

我有一个按邮政编码和月份分隔数据的组列表。我无法在折线图中绘制数据。

data <- read.csv("./data/rental_12_11_2015_365.csv")
data$Listing.Contract.Date <- as.Date(data$Listing.Contract.Date, format = "%Y-%m-%d")
data$Month_Int <- as.numeric(strftime(data$Listing.Contract.Date,"%m"))
data$Month_Char <- month.abb[data$Month_Int]
groups_zipcode <- with(data, split(data, list(Zip.Code, Month_Char)))
summaryofgroups <- lapply(groups_zipcode, function(group){nrow(group)})

ggplot(data = groups_zipcode,aes(x = Month_Char,y = summaryofgroups,group = Zip.Code,col = Zip.Code))+ geom_line()

输出是我在下面但是在所有月份等等。我没有粘贴所有内容。 ggplot正在生成“错误:ggplot2不知道如何处理类列表的数据”。

$`75013.Apr`
[1] 11

$`75025.Apr`
[1] 39

$`75035.Apr`
[1] 71

$`75070.Apr`
[1] 103

$`75071.Apr`
[1] 43

$`75075.Apr`
[1] 15

$`75093.Apr`
[1] 13

$`75013.Aug`
[1] 18

$`75025.Aug`
[1] 39

$`75035.Aug`
[1] 80

$`75070.Aug`
[1] 93

$`75071.Aug`
[1] 49

$`75075.Aug`
[1] 16

$`75093.Aug`
[1] 19

解决方案:

您好我能找到解决方案。这就是我想要做的。祝他们在类似情况下好运。

 inventory_graph <- ggplot(data = data, aes(x=Month_Char, fill = as.factor(Zip.Code))) + geom_histogram(position = "dodge")

1 个答案:

答案 0 :(得分:2)

您的数据格式正确,您只需使用ggplot

即可
#Some fake data in the same format as you pasted
fake_data <- data.frame("Zip" = factor(rep(c(12345, 98765, 56473), 4)),
                    "Month" = factor(rep(c("April", "May", "June", "July"), each=3), levels=c("April", "May", "June", "July")),
                    "Count" = sample(20:50, 12))
library(ggplot2)

ggplot(data = fake_data, aes(x=Month, y=Count, group=Zip, col=Zip)) + geom_line()

plot