ggplot2で作図2:異なるグラフを重ねる
xval <- seq(1,10)
yval1 <-round(runif(10)*10,0)
yval2 <-round(runif(10)*10,0)
dat <- data.frame(xval=xval,yval1=yval1,yval2=yval2)
dat
## xval yval1 yval2
## 1 1 8 3
## 2 2 7 9
## 3 3 7 4
## 4 4 7 1
## 5 5 7 10
## 6 6 1 5
## 7 7 4 8
## 8 8 3 2
## 9 9 4 0
## 10 10 6 6
library(ggplot2)
ggplot() +
xlab("X value")+
ylab("Y value")+
ggtitle("Bar chart & line graph")+
scale_x_continuous(breaks=seq(0,11,1))+#x軸の範囲を指定
scale_y_continuous(breaks=seq(0,11,1))+
layer( #軸の範囲を指定。breaks=seq(下限値-1, 上限値+1, 区切り単位)
data=dat,
mapping=aes(x=xval, y=yval1),
geom="bar",
stat="identity",#データの統計処理方法を指定
position="identity",#データの表示位置
alpha=0.5
)+
layer(
data=dat,
mapping=aes(x=xval, y=yval2),
geom="line",
stat="identity",
position="identity",
colour="blue"
)+
layer(
data=dat,
mapping=aes(x=xval, y=yval2),
geom="point",
stat="identity",
position="identity",
colour="blue",
size=5
)

#dev.off()
#また,以下のコマンドでも同じグラフが出力される。
#========================
ggplot(data=dat) +
xlab("X value")+ ylab("Y value")+
ggtitle("Bar chart & line graph") +
scale_x_continuous(breaks=seq(0,11,1)) +
scale_y_continuous(breaks=seq(0,11,1)) +
geom_bar(aes(x=xval, y=yval1),
stat="identity", position="identity",
alpha=0.5) +
geom_line(aes(x=xval,y=yval2),
stat="identity", position="identity",
colour="blue") +
geom_point(aes(x=xval,y=yval2),
stat="identity",
position="identity",
colour="blue", size=5)

#dev.off()
========================
,グラフを出力する前にグラフのテーマの設定で指定することができる。
#========================
mytheme <- theme_bw()+
theme(axis.text.x=element_text(size=14),
axis.title.x=element_text(size=20),
axis.text.y=element_text(size=14),
axis.title.y=element_text(size=20),
title=element_text(size=14),
legend.title=element_text(size=18),
legend.text=element_text(size=14))
theme_set(mytheme)
#========================
#背景色が白になり(theme_bw()で設定),
#X軸,Y軸,タイトルなどのフォントサイズが大きくなった
#(axis.text.x=element_text(size=14)などで設定)。
##############################################
library(ggplot2)
library(gridExtra)
## Loading required package: grid
##
## Attaching package: 'grid'
##
## The following object is masked _by_ '.GlobalEnv':
##
## arrow
library(DT)
x <- 1:6
y <- 10*x
y1 <- 10*x-5
y2 <- 10*x+5
y3 <- 10*x-15
y4 <- 10*x+15
z=c(13,7,15,8,12,8)
rui <- cumsum(z)
xy <- data.frame("月日"=x,"実積値"=z,"累積値"=rui,
"シグマ1"=y4,"シグマ2"=y2,
"基準値"=y,"シグマ3"=y1,"シグマ4"=y3)
rownames(xy) <- paste0("第",1:6,"日目")
xy
## 月日 実積値 累積値 シグマ1 シグマ2 基準値 シグマ3 シグマ4
## 第1日目 1 13 13 25 15 10 5 -5
## 第2日目 2 7 20 35 25 20 15 5
## 第3日目 3 15 35 45 35 30 25 15
## 第4日目 4 8 43 55 45 40 35 25
## 第5日目 5 12 55 65 55 50 45 35
## 第6日目 6 8 63 75 65 60 55 45
datatable(t(as.matrix(xy)))
h = tableGrob(t(xy))
#最低限必要な引数
#ymax : 塗りつぶす範囲のY軸上の最大値(各X点ごとに)
#ymin : 塗りつぶす範囲のY軸上の最小値(各X点ごとに)
p1 <- ggplot(data=xy) +
xlab("月日")+ ylab("累積売上高")+
ggtitle("累積売上高推移グラフ") +
scale_x_continuous(breaks=seq(1,6,1)) +
scale_y_continuous(breaks=seq(0,max(xy),10)) +
geom_line(aes(x=月日,y=基準値),stat="identity",position="identity")+
geom_line(aes(x=月日,y=シグマ3),stat="identity",position="identity")+
geom_line(aes(x=月日,y=シグマ2),stat="identity",position="identity")+
geom_line(aes(x=月日,y=シグマ4),stat="identity",,position="identity")+
geom_line(aes(x=月日,y=シグマ1),stat="identity",position="identity")+
geom_ribbon(aes(x=月日,ymin=シグマ1,ymax=max(xy),
alpha=0.5,fill="blue"))+
geom_ribbon(aes(x=月日,ymin=シグマ2,ymax=シグマ1,
alpha=0.5,fill="green"))+
geom_ribbon(aes(x=月日,ymin=基準値,ymax=シグマ2,
alpha=0.5,fill="yellow"))+
geom_ribbon(aes(x=月日,ymin=基準値,ymax=シグマ3,
alpha=0.5,fill="yellow"))+
geom_ribbon(aes(x=月日,ymin=シグマ3,ymax=シグマ4,alpha=0.5,fill="pick"))+
geom_ribbon(aes(x=月日,ymin=min(シグマ4),ymax=シグマ4,
alpha=0.5,fill="red"))+
theme(legend.position="none")+
geom_line(aes(x=月日,y=累積値),
stat="identity", position="identity",
size=1.5,colour="blue") +
geom_point(aes(x=月日,y=累積値),
stat="identity",
position="identity",
colour="blue", size=5)
grid.arrange(p1,h, ncol=1) # stacked

ggsave("plot-table1.pdf", arrangeGrob(p1, h, ncol=1))
## Saving 7 x 9 in image
#dev.off()
—————————————–
xy3 <- xy[,8]-xy[,4]
xy4 <- round(xy[,8]/max(xy[,4]),3)
1-xy4
## [1] 1.067 0.933 0.800 0.667 0.533 0.400
xy[,8]
## [1] -5 5 15 25 35 45
xy5 <- max(xy[,4])-xy[,8]
基準値化
#########################
library(reshape2)
library(ggplot2)
library(DT)
df <- data.frame(x1=c(5,3,7,4,6,7),
x2=c(15,23,17,24,26,37),
x3=c(5,8,6,3,6,4)*3,
x4=round(c(3,5,12,4,9,7)*2.5,0),
x5=c(9,13,15,9,11,13))
MAX <- apply(df,2,max)
MIN <- apply(df,2,min)
MEAN <- apply(df,2,mean)
SD <- apply(df,2,sd)
(aa <- round(rbind(MAX,MIN,MEAN,SD),2))
## x1 x2 x3 x4 x5
## MAX 7.00 37.00 24.00 30.00 15.00
## MIN 3.00 15.00 9.00 8.00 9.00
## MEAN 5.33 23.67 16.00 16.67 11.67
## SD 1.63 7.79 5.25 8.36 2.42
datatable((as.matrix(aa)))
a <- scale(df)
str(a)
## num [1:6, 1:5] -0.204 -1.429 1.021 -0.816 0.408 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:5] "x1" "x2" "x3" "x4" ...
## - attr(*, "scaled:center")= Named num [1:5] 5.33 23.67 16 16.67 11.67
## ..- attr(*, "names")= chr [1:5] "x1" "x2" "x3" "x4" ...
## - attr(*, "scaled:scale")= Named num [1:5] 1.63 7.79 5.25 8.36 2.42
## ..- attr(*, "names")= chr [1:5] "x1" "x2" "x3" "x4" ...
(xy1 <- round(data.frame(a[1:6,1:5]),3))
## x1 x2 x3 x4 x5
## 1 -0.204 -1.113 -0.190 -1.037 -1.101
## 2 -1.429 -0.086 1.523 -0.558 0.550
## 3 1.021 -0.856 0.381 1.595 1.376
## 4 -0.816 0.043 -1.332 -0.798 -1.101
## 5 0.408 0.300 0.381 0.638 -0.275
## 6 1.021 1.712 -0.761 0.160 0.550
xy1$day <- 1:6
xy1
## x1 x2 x3 x4 x5 day
## 1 -0.204 -1.113 -0.190 -1.037 -1.101 1
## 2 -1.429 -0.086 1.523 -0.558 0.550 2
## 3 1.021 -0.856 0.381 1.595 1.376 3
## 4 -0.816 0.043 -1.332 -0.798 -1.101 4
## 5 0.408 0.300 0.381 0.638 -0.275 5
## 6 1.021 1.712 -0.761 0.160 0.550 6
xy2 <-melt(xy1,id="day")
p2 <- ggplot(data=xy2) +
xlab("月日")+ ylab("標準化")+
ggtitle("各仕事件数の標準化推移グラフ") +
scale_x_continuous(breaks=seq(1,6,1)) +
scale_y_continuous(breaks=seq(0,max(xy),10)) +
geom_line(aes(x=day,y=value,group=variable,size=1,
colour=variable),stat="identity",
position="identity")+
geom_point(aes(x=day,y=value,group=variable,colour=variable),
stat="identity",position="identity", size=3)+
geom_hline(yintercept=0.5,linetype="dashed",colour="green")+
geom_hline(yintercept=-0.5,linetype="dashed",colour="orange")+
geom_hline(yintercept=0,linetype="dashed",colour="black")+
geom_hline(yintercept=1,linetype="dashed",colour="blue")+
geom_hline(yintercept=-1,linetype="dashed",colour="red")
p2

#dev.off()
相関行列からクラスター分析
par(mfrow=c(1,2))
df1 <- cor(df)
data1.hc <- hclust(as.dist(df1))
plot(data1.hc,main="各仕事のクラスター分析")
df2 <- cor(t(df))
data2.hc <- hclust(as.dist(df2))
plot(data2.hc,main="各期間のクラスター分析")

par(mfrow=c(1,1))
#dev.off()