【関連する外部ページ】https://www.rdocumentation.org/packages/ggplot2/versions/3.3.5
https://d.hatena.ne.jp/triadsou/20100528/1275042816#overlay
geom_point の Aesthetics : x, y, alpha, colour, fill, shape, size
require(ggplot2) p <- ggplot(diamonds, aes(x = carat, y = price)) p + geom_point()
element_text(size=15) は x 軸ラベル, y 軸ラベル, x軸目盛り, y軸目盛りのフォントサイズ
require(ggplot2) p <- ggplot(diamonds, aes(x = carat, y = price)) p + geom_point() + xlim(0, 10) + ylim(0, 30000) + xlab("カラット") + ylab("価格") + theme(axis.title.x = element_text(size=24), axis.title.y = element_text(size=24), axis.text.x = element_text(size=24),axis.text.y = element_text(size=24))
require(ggplot2) p <- ggplot(diamonds, aes(x = carat, y = price)) p + geom_point( colour = "red", size = 3 )
require(ggplot2) p <- ggplot(diamonds, aes(x = carat, y = price)) p + geom_point( size = 3, alpha = 0.04 )
geom_point の colour に factor を指定. scale_colour_hue() を追加.
require(ggplot2) p <- ggplot(diamonds, aes(x = carat, y = price)) p + geom_point( aes(colour = color) ) + scale_colour_hue()
geom_point の colour に数値を指定. scale_colour_gradient(low = "cyan", high="magenta") を指定.※ colour に数式を書いても大丈夫(整数値でなくてもよい)
require(ggplot2) p <- ggplot(diamonds, aes(x = carat, y = price)) p + geom_point( aes(colour = depth) ) + scale_colour_gradient(low = "cyan", high="magenta")
geom_point の size に factor を指定
require(ggplot2) p <- ggplot(diamonds, aes(x = carat, y = price)) p + geom_point( aes(size = color, colour = color), alpha = 0.5 ) + scale_colour_hue()
geom_point の size に数値を指定 ※ 数式を書いても大丈夫(整数値でなくてもよい)
require(ggplot2) p <- ggplot(diamonds, aes(x = carat, y = price)) p + geom_point( aes(size = depth, colour = depth), alpha = 0.5 ) + scale_colour_gradient(low = "cyan", high="magenta")
geom_point の shape に factor を指定 ※ shape には整数値なら指定できる
require(ggplot2) p <- ggplot(diamonds, aes(x = carat, y = price)) p + geom_point( aes(shape = color, colour = color), alpha = 0.5, size = 4 ) + scale_colour_hue()
geom_point の shape に factor を指定.scale_shape を使う ※ shape には、整数値なら指定できる
require(ggplot2) p <- ggplot(diamonds, aes(x = carat, y = price)) p + geom_point( aes(shape = color, colour = color), alpha = 0.5, size = 4 ) + scale_colour_hue() + scale_shape(solid = FALSE)
geom_line の Aesthetics : x, y, alpha, colour, linetype, size
<例1>
economics は ggplot2 パッケージ由来のデータ
require(ggplot2) p <- ggplot(economics, aes(x = date, y = uempmed)) p + geom_line()
<例2>
Indometh は datasets パッケージ由来のデータ
require(ggplot2) p <- ggplot(Indometh, aes(x = time, y = conc, colour=Subject)) p + geom_line()
element_text(size=24) は x 軸ラベル, y 軸ラベル, x軸目盛り, y軸目盛りのフォントサイズを変更するのに使うことができる
require(ggplot2) p <- ggplot(economics, aes(x = date, y = uempmed)) + ylim(0, 15) + xlab("西暦年") + ylab("失業率") + theme(axis.title.x = element_text(size=24), axis.title.y = element_text(size=24), axis.text.x = element_text(size=24),axis.text.y = element_text(size=24)) p + geom_line()
require(ggplot2) p <- ggplot(Indometh, aes(x = time, y = conc, colour=Subject)) + ylim(0, 3) + xlab("conc") + ylab("時間") + theme(axis.title.x = element_text(size=24), axis.title.y = element_text(size=24), axis.text.x = element_text(size=24),axis.text.y = element_text(size=24)) p + geom_line()
require(ggplot2) p <- ggplot(economics, aes(x = date, y = uempmed)) p + geom_line( colour = "red", size = 3 )
geom_point の colour に数値を指定. scale_colour_gradient(low = "cyan", high="magenta") を指定.※ colour に数式を書いても大丈夫(整数値でなくてもよい)
require(ggplot2) p <- ggplot(economics, aes(x = date, y = uempmed) ) p + geom_line( aes(colour=uempmed) ) + scale_colour_gradient(low = "cyan", high="magenta")
require(ggplot2) p <- ggplot(Indometh, aes(x = time, y = conc, colour=Subject)) p + geom_line( aes( linetype = Subject ), size = 1 )
geom_path の Aesthetics : x, y, alpha, colour, linetype, size
seals は ggplot2 パッケージ由来のデータ
require(ggplot2) require(data.table) p <- ggplot(data.table(seals), aes(x = delta_lat, y = delta_long, colour = factor(long))) p + geom_path()
element_text(size=24) は x 軸ラベル, y 軸ラベル, x軸目盛り, y軸目盛りのフォントサイズを変更するのに使うことができる
require(ggplot2) require(data.table) p <- ggplot(data.table(seals), aes(x = delta_lat, y = delta_long, colour = factor(long))) + xlim(-1.5, 1.5) + ylim(-1.5, 1.5) + xlab("緯度変化") + ylab("経度変化") + theme(axis.title.x = element_text(size=24), axis.title.y = element_text(size=24), axis.text.x = element_text(size=24),axis.text.y = element_text(size=24)) p + geom_path()
require(ggplot2) require(data.table) p <- ggplot(data.table(table(diamonds$color)), aes(x = "", y = Freq, fill = Var1)) p + geom_bar( width = 1, stat = "identity" )
require(ggplot2) p <- ggplot(data.table(table(diamonds$color)), aes(x = "", y = Freq, fill = Var1)) p + geom_bar( width = 1, stat = "identity" ) + coord_polar("y")