• このページは公開されるので個人情報などは記載しないこと。
  • 課題が完成したらknitPublishする。

課題名:散布図

rm(list = ls()) # 全オブジェクト削除
# ここにRコードを記入する。
# 例)
plot(1:9)

x <- 1:10


#データ
x1 <- c(7,4,6,2,9,3,8,1,6,3)
y1 <- c(3,6,5,8,3,4,2,9,2,6)

x2 <- c(6,3,4,9,1,8,4,2,5,7)
y2 <- c(7,8,2,1,7,9,6,3,5,4)

d <- data.frame(x1,y1,x2,y2)


#要約統計量
summary(data.frame(x1,y1,x2,y2))
##        x1             y1            x2             y2      
##  Min.   :1.00   Min.   :2.0   Min.   :1.00   Min.   :1.00  
##  1st Qu.:3.00   1st Qu.:3.0   1st Qu.:3.25   1st Qu.:3.25  
##  Median :5.00   Median :4.5   Median :4.50   Median :5.50  
##  Mean   :4.90   Mean   :4.8   Mean   :4.90   Mean   :5.20  
##  3rd Qu.:6.75   3rd Qu.:6.0   3rd Qu.:6.75   3rd Qu.:7.00  
##  Max.   :9.00   Max.   :9.0   Max.   :9.00   Max.   :9.00
  # カラーパレット
COL <- c(rgb(255,   0,   0,  105, max = 255), # 赤
         rgb(  0,   0, 255,  105, max = 255), # 青
         rgb(  0, 155,   0,  105, max = 255)) # 緑


#散布図

COL <- c(rgb(255,   0,   0,  105, max = 255), 
         rgb(  0,   0, 255,  105, max = 255),
         rgb(  0, 155,   0,  105, max = 255),
         rgb(155, 255,   0,  105, max = 255))


matplot(data.frame(x1,y1,x2,y2), type = 'p',pch = 16, col = COL,
        main = '主タイトル',
        xlab = 'x軸ラベル[単位]',
        ylab = 'y軸ラベル[単位]')

# 格子線(grid lines)
grid()
 
# 凡例(はんれい)
legend('topleft', pch = 16, col = COL, legend = colnames(d))

matplot(x = x, y = d, type = 'n', 
        ylim = c(-10, 10),
        main = '主タイトル',
        xlab = 'x軸ラベル[単位]',
        ylab = 'y軸ラベル[単位]')


abline(lty = 2,               
       col = gray(0.5, 0.25), 
       h = seq(-10, 10, 5),  
       v = seq(0, 100, 20))   

PCH <- 16:18  
matpoints(x = x, y = d$x1, pch = PCH[1], col = COL[1])
matpoints(x = x, y = d$y1, pch = PCH[2], col = COL[2])
matpoints(x = x, y = d$x2, pch = PCH[3], col = COL[3])
matpoints(x = x, y = d$y2, pch = PCH[4], col = COL[4])
legend('topright', pch = PCH, col = COL, legend = colnames(d))

pairs(d, pch = 16, col = COL[1])

library(plotly)
##  要求されたパッケージ ggplot2 をロード中です
## 
##  次のパッケージを付け加えます: 'plotly'
##  以下のオブジェクトは 'package:ggplot2' からマスクされています:
## 
##     last_plot
##  以下のオブジェクトは 'package:stats' からマスクされています:
## 
##     filter
##  以下のオブジェクトは 'package:graphics' からマスクされています:
## 
##     layout

#フォント設定
kyokasho <- list(size = 11, color = 'blue', family = 'UD Digi Kyokasho NK-R')

plot_ly() |>
  add_trace(x = x, y = d$x1, name = 'x1', marker = list(color = COL[1])) |>
  add_trace(x = x, y = d$y1, name = 'y1', marker = list(color = COL[2])) |>
  add_trace(x = x, y = d$x2, name = 'y2', marker = list(color = COL[3])) |>
  add_trace(x = x, y = d$y2, name = 'x2', marker = list(color = COL[4])) |>
  layout(barmode = 'group',
         font  = kyokasho,
         title = '主タイトル',
         xaxis = list(title = 'x軸ラベル[単位]'),
         yaxis = list(title = 'y軸ラベル[単位]', range = c(0, 10)))
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 36600 (\N{CJK UNIFIED IDEOGRAPH-8EF8}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12521 (\N{KATAKANA LETTER RA}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12505 (\N{KATAKANA LETTER BE}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12523 (\N{KATAKANA LETTER RU}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 65339 (\N{FULLWIDTH LEFT SQUARE BRACKET}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 21336 (\N{CJK UNIFIED IDEOGRAPH-5358}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 20301 (\N{CJK UNIFIED IDEOGRAPH-4F4D}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 65341 (\N{FULLWIDTH RIGHT SQUARE BRACKET}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 20027 (\N{CJK UNIFIED IDEOGRAPH-4E3B}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12479 (\N{KATAKANA LETTER TA}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12452 (\N{KATAKANA LETTER I}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)
## C:\Users\n_oku\AppData\Local\R\win-library\4.3\reticulate\python\rpytools\call.py:7: UserWarning: Glyph 12488 (\N{KATAKANA LETTER TO}) missing from current font.
##   value, error = rpycall.call_r_function(f, *args, **kwargs)