Rで解析:ggplot2で色々プロット

Rの解析に役に立つ記事
スポンサーリンク

ggplot2でプロットできるコマンドの紹介です。ggplot2のインストールがまだであれば、下記コマンドを実行してください。

install.packages("ggplot2")
スポンサーリンク

プロット紹介

プロットの下にコマンド例を示します。色々試してみてください。

Bar Plot

geom_bar
###データを作成#####
x <- c(rep("a", 7), rep("b", 3))
y <- rnorm(10, mean = rep(c(1,2), each = 5), sd = 0.4)
PlotData <- data.frame(x = x, y = y, row.names = c(1:10))
########

###&#12503;&#12525;&#12483;&#12488;#####
p <- ggplot(PlotData, aes(x = PlotData[, 1]))
p + geom_bar(
  #&#22615;&#12426;&#12398;&#36879;&#26126;&#24230;
  alpha = 0.3,
  #&#32218;&#12398;&#33394;
  colour = "red",
  #&#22615;&#12426;&#12398;&#33394;
  fill = c("green", "blue"), 
  #&#32218;&#31278; 0:Blank, 1:Solid, 2:Dashed, 3:Dotted, 4:Dotdash, 5:Longdash, 6:Twodash
  linetype = 2)     #&#37325;&#12415;&#20184;&#12369;&#12364;&#24517;&#35201;&#12394;&#22580;&#21512;&#12399;weight&#12434;&#20351;&#29992;&#12375;&#12390;&#12367;&#12384;&#12373;&#12356;&#12290;
[/code]

CrossBar Plot

geom_crossbar
###&#12487;&#12540;&#12479;&#12434;&#20316;&#25104;#####
x <- c(rep("a", 7), rep("b", 3))
y <- rnorm(10, mean = rep(c(1,2), each = 5), sd = 0.4)
PlotData <- data.frame(x = x, y = y, row.names = c(1:10))
########

###&#12503;&#12525;&#12483;&#12488;#####
p <- ggplot(PlotData, aes(x = PlotData[, 1],
                          y = PlotData[, 2],
                          ymin = min(PlotData[, 2]),
                          ymax = max(PlotData[, 2])))
p + geom_crossbar(
  #&#22615;&#12426;&#12398;&#36879;&#26126;&#24230;
  alpha = 0.3, 
  #&#22615;&#12426;&#12398;&#33394;
  fill = "blue", 
  #&#32218;&#12398;&#33394;
  colour = "red", 
  #&#32218;&#12398;&#22826;&#12373;
  size = 1, 
  #&#32218;&#31278; 0:Blank, 1:Solid, 2:Dashed, 3:Dotted, 4:Dotdash, 5:Longdash, 6:Twodash
  linetype = 1) 

等高線

geom_density2d
###&#12487;&#12540;&#12479;&#12434;&#20316;&#25104;#####
x <- c(rep("a", 7), rep("b", 3))
y <- rnorm(10, mean = rep(c(1,2), each = 5), sd = 0.4)
PlotData <- data.frame(x = x, y = y, row.names = c(1:10))
########

###&#12503;&#12525;&#12483;&#12488;#####
p <- ggplot(PlotData, aes(x = PlotData[, 2], y = PlotData[, 2]))
p + geom_density2d(
  #&#22615;&#12426;&#12398;&#36879;&#26126;&#24230;
  alpha = 0.3, 
  #&#32218;&#12398;&#33394;
  colour = "green", 
  #&#32218;&#12398;&#22826;&#12373;
  size = 1, 
  #&#32218;&#31278; 0:Blank, 1:Solid, 2:Dashed, 3:Dotted, 4:Dotdash, 5:Longdash, 6:Twodash
  linetype = 1) 

BoxPlot

geom_boxplot
###&#12487;&#12540;&#12479;&#12434;&#20316;&#25104;#####
x <- c(rep("a", 7), rep("b", 3))
y <- rnorm(10, mean = rep(c(1,2), each = 5), sd = 0.4)
PlotData <- data.frame(x = x, y = y, row.names = c(1:10))
########

###&#12503;&#12525;&#12483;&#12488;#####
p <- ggplot(PlotData, aes(x = PlotData[, 1], y = PlotData[, 2]))
p + geom_boxplot(
  #&#22806;&#12428;&#20516;&#12398;&#33394;
  outlier.colour = NULL, 
  #&#22806;&#12428;&#20516;&#12398;&#12471;&#12531;&#12508;&#12523;
  outlier.shape = NULL,
  #&#22806;&#12428;&#20516;&#12398;&#12471;&#12531;&#12508;&#12523;&#12398;&#22823;&#12365;&#12373;
  outlier.size = NULL, 
  #&#12494;&#12483;&#12481;
  notch = FALSE, 
  #&#12494;&#12483;&#12481;&#12398;&#24133;
  notchwidth = 0.5, 
  #&#22615;&#12426;&#12398;&#36879;&#26126;&#24230;
  alpha = 0.3, 
  #&#32218;&#12398;&#33394;
  colour = "red", 
  #&#22615;&#12426;&#12398;&#33394;
  fill = c("green", "blue"), 
  #&#32218;&#31278; 0:Blank, 1:Solid, 2:Dashed, 3:Dotted, 4:Dotdash, 5:Longdash, 6:Twodash
  linetype = 2) 

棒グラフ

geom_histogram
###&#12487;&#12540;&#12479;&#12434;&#20316;&#25104;#####
x <- c(rep("a", 3), rep("b", 5), rep("c", 2))
y <- rnorm(10, mean = rep(c(1,2), each = 5), sd = 0.4)
PlotData <- data.frame(x = x, y = y, row.names = c(1:10))
########

###&#12503;&#12525;&#12483;&#12488;#####
p <- ggplot(PlotData, aes(x = PlotData[, 1]))
p + geom_histogram(
  #&#33394;&#12398;&#36879;&#26126;&#24230;
  alpha = 0.5, 
  #&#22615;&#12426;&#12398;&#33394;
  fill = "blue", 
  #&#32218;&#12398;&#33394;
  colour = "green", 
  #&#32218;&#12398;&#22826;&#12373;
  size = 1, 
  #&#32218;&#31278; 0:Blank, 1:Solid, 2:Dashed, 3:Dotted, 4:Dotdash, 5:Longdash, 6:Twodash
  linetype = 1) 

少しでも、あなたの解析が楽になりますように!!

タイトルとURLをコピーしました