スポンサーリンク

ggplot2でx軸のラベルを変更

ggplot2でグラフを自由に描きたいので、以下の本を衝動買い!

(環境)
Windows 8.1 Pro
R Studio 1.1.383

ggplot2でx軸のラベルを変更

●x軸の値が離散値(?)の場合

scale_x_discrete(“Time”, labels = c(“1″=”first”, “2”=”second”, “3”=”third”)

のように用いる。

例)

b <- b + scale_x_discrete(“Time”, labels = c(“BW_00pre” = “Day0”, “BW_01POD” = “1POD”, “BW_07POD” = “7POD”, “BW_14POD” = “14POD”))

 

●x軸の値が連続値の場合

coord_cartesian(ylim = c(0,25)) + scale_x_continuous(breaks = seq(0,25,by=5),labels=c(0,5,10,15,20,25))

のような感じで用いる。

例)

b <- b + coord_cartesian(ylim = c(0,25)) + scale_x_continuous(breaks = seq(0,25,by=5),labels=c(0,5,10,15,20,25))

 

●ggplot2でグラフのタイトルを真ん中に。

b <- b + ggtitle(“New Title”)
b <- b + theme(plot.title = element_text(hjust=0.5))
b <- b + theme(plot.title = element_text(size = 20),
axis.title.x = element_text(size= 20),
axis.title.y = element_text(size= 20),
axis.text.x = element_text(size= 20),
axis.text.y = element_text(size= 20))

 

●グループによって少しだけx軸方向にずらす

pd <- position_dodge(0.1)

ggplot(tgc, aes(x=dose, y=len, colour=supp)) + geom_errorbar(aes(ymin=len-se, ymax=len+se), width=.1, position=pd) + geom_line(position=pd) + geom_point(position=pd)

のように使う。

http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)/

image

image

スポンサーリンク

R

Posted by twosquirrel