显示标签为“Chart”的博文。显示所有博文
显示标签为“Chart”的博文。显示所有博文

2013年9月23日星期一

R :通过 cut + ggplot 迅速画出 按时段 week / month 统计图

见:Plot Weekly or Monthly Totals in R

切 week / month :

log$Month <- as.Date(cut(log$Date,  breaks = "month"))

log$Week <- as.Date(cut(log$Date,  breaks = "week",  start.on.monday = FALSE))

统计并画图:

ggplot(data = log,
  aes(Month, Quantity)) +
  stat_summary(fun.y = sum, # adds up all observations for the month
    geom = "bar") + # or "line"
  scale_x_date(
    labels = date_format("%Y-%m"),
    breaks = "1 month") # custom x-axis labels


用ggplot2画折线图、面积图、直方图等等,笔记:chart_example

2009年4月12日星期日

用R语言画柱状图 (bar)

vim高亮的插件:R.vim : Send R code from a VIM buffer to R
参考:
利用統計軟體R
Producing Simple Graphs with R
Quick-R
R Graph Gallery
在上面的网站逛两圈,就可以照猫画虎了。。。
柱状图:

源码:
slices <- c(3,1,4,2) 
names <- c( "甲", "乙", "丙", "丁") 

png("r-graph-sample.png") 

barplot(beside=TRUE, 
        slices, #纵轴取值 
        names.arg=names, #边框名字 
        border="black",  #边框颜色 
        col=c("purple","green3","blue","red"), #框内线条颜色
        density =   c(7.5,12.5,17.5,22.5), #框内线条密度
        angle =  c(45,60,120,135),  #框内线条倾斜角度
        width = c(4,2.2,2.2,3),  #边框宽度 
        space = c(1.5,0.5,0.5,1), #边框间距 
        ylim=c(0,5),  #纵轴取值范围 
        )  

title(xlab="横") #横轴名字 
title(ylab="竖") #纵轴名字 

lbls <- round(slices/sum(slices)*100) 
lbls <- paste(lbls,"%",sep="") # ad % to labels 
lbls <- paste(names, lbls) # add percents to labels 

#图例 
legend("topright",lbls, 
        fill=c("purple","green3","blue","red"), 
        density =   c(7.5,12.5,17.5,22.5), 
        angle =  c(45,60,120,135),   
        )