从多列创建条形图

时间:2014-09-08 18:38:45

标签: mysql sql r plot

我有以下数据集:

Location    Type    FromDate    ToDate  1   2   3   4   5
  A          1        12-Jul    13-Jul  2   4   0   1   2
  A          2        12-Jul    13-Jul  0   0   1   4   1
  B          1        12-Jul    13-Jul  0   1   1   3   1
  B          2        12-Jul    13-Jul  1   0   0   0   1
  C          1        12-Jul    13-Jul  2   3   1   5   0
  C          2        12-Jul    13-Jul  3   3   1   0   0

如何在R中为每个位置创建条形图,包括第1天和第5天的类型1和2?

2 个答案:

答案 0 :(得分:1)

一种稍微替代的解决方案,而不是使用reshape2plyr使用dplyrtidyr。后一种组合使用了越来越受欢迎的管道。

首先阅读数据:

df <- read.table(header=TRUE, text="Location    Type    FromDate   ToDate 1   2   3   4   5
A          1        12-Jul    13-Jul  2   4   0   1   2
A          2        12-Jul    13-Jul  0   0   1   4   1
B          1        12-Jul    13-Jul  0   1   1   3   1
B          2        12-Jul    13-Jul  1   0   0   0   1
C          1        12-Jul    13-Jul  2   3   1   5   0
C          2        12-Jul    13-Jul  3   3   1   0   0")
# remove the X-es which are put in front of the days
names(df) <- gsub("X","",names(df))

加载所需的库:

library(dplyr)
library(tidyr)
library(ggplot2)

将数据从宽格式转换为长格式:

df.m <- df %>% gather(day,value,5:9)

创建情节:

ggplot(data=df.m, aes(x=day, y=value, fill=as.factor(Type))) + 
  geom_bar(stat="identity", position="dodge") + 
  xlab("Day of the week") +
  scale_fill_discrete("Type\nof\nsomething\n") +
  facet_grid(Location ~ ., labeller=label_both) +
  theme_bw() +
  theme(axis.title.y=element_blank())

导致: enter image description here


但是,考虑到您的数据,折线图可能是更好的可视化:

ggplot(data=df.m, aes(x=day, y=value, color=as.factor(Type), group=as.factor(Type))) + 
  geom_line(size=1.5) + 
  xlab("Days") +
  scale_color_discrete("Type\nof\nsomething\n") +
  facet_grid(Location ~ ., labeller=label_both) +
  theme_bw() +
  theme(axis.title.y=element_blank())

导致: enter image description here

答案 1 :(得分:0)

您应该更准确地澄清您的问题,以便读者准确了解您的需求。您还可以通过解释已经尝试过的内容来展示解决问题的方法。

因此我只能猜到你想要的东西,这是我的建议:

加载所需的包:

require(ggplot2)
require(reshape2)
require(plyr)

重新创建你的df:

location = c('A','A','B','B','C','C')
type = rep(c(1,2),3)
fdate = rep('12-Jul', 6)
tdate = rep('13-Jul', 6)
v1 = c(2,0,0,1,2,3)
v2 = c(4,0,1,0,3,3)
v3 = c(0,1,1,0,1,1)
v4 = c(1,4,3,0,5,0)
v5 = c(2,1,1,1,0,0)

dat = data.frame(location, type, fdate, tdate, v1, v2, v3, v4, v5)

重新排列绘图数据:

melted = melt(dat, id.vars=c('location', 'type', 'fdate', 'tdate'))
sums = ddply(melted, c('fdate', 'tdate', 'location', 'type', 'variable'), 
summarise, sum=sum(value))

用ggplot2绘图:

ggplot(aes(x=variable, y=sum, fill=as.factor(type)), data=sums) + 
    geom_bar(stat="identity", position="dodge") + 
    facet_grid(location ~ .)

resulting plot 编辑:使用您发布的确切数据框:

# read data
dat2 <- read.table(header=T, text="Location    Type    FromDate   ToDate 1   2   3   4   5
A          1        12-Jul    13-Jul  2   4   0   1   2
A          2        12-Jul    13-Jul  0   0   1   4   1
B          1        12-Jul    13-Jul  0   1   1   3   1
B          2        12-Jul    13-Jul  1   0   0   0   1
C          1        12-Jul    13-Jul  2   3   1   5   0
C          2        12-Jul    13-Jul  3   3   1   0   0")

# rearranging data for plotting
melted = melt(dat2, id.vars=c('Location', 'Type', 'FromDate', 'ToDate'))
sums = ddply(melted, c('FromDate', 'ToDate', 'Location', 'Type', 'variable'),
summarise, sum=sum(value))
# plot with ggplot2
ggplot(aes(x=variable, y=sum, fill=as.factor(Type)), data=sums) + 
   geom_bar(stat="identity", position="dodge") + 
   facet_grid(Location ~ .)