LoginSignup
1
1

More than 5 years have passed since last update.

I'm (Hysteric) Scatman

Last updated at Posted at 2014-03-13

scatterplot woth merginal histogram/density plot的なアレを所望します。びーばっぱばどっぼ。

これ

opts and theme_blank() is deprecated. use theme and element_blank()

remove grid (panel.grid = element_blank())

ggplot-vs-gridExtra
df<-read.delim("data.tab")
library(ggplot2)
library(gridExtra)

blank <- element_blank()

hist_top <- ggplot(df, aes(df$length)) + geom_histogram(binwidth = 0.1) + scale_x_log10() + theme(legend.position = "none", axis.title = blank, axis.text = blank)

empty <- ggplot() + geom_point(aes(1,1), colour = "white") + theme(axis.ticks = blank, axis.title = blank, axis.text = blank, panel.background = blank, panel.grid = blank)

scatter <- ggplot(df, aes(x = df$length, y = df$amount)) + geom_point() + scale_x_log10() + scale_y_log10()

hist_right <- ggplot(df, aes(df$amount)) + geom_histogram(binwidth = 0.1) + scale_x_log10() + theme(legend.position = "none", axis.title = blank, axis.text = blank) + coord_flip()

grid.arrange(hist_top, empty, scatter, hist_right, ncol=2, nrow=2, widths=c(4,1), heights=c(1,4))

こんなんなった
だがプロットの幅が変わってメモリが揃わない

ちなみにgridはggplot2パッケージから外されたとのこと

rbind-and-cbind
library(gtable)
p1 <- plot(1,1)
p2 <- plot(2,1)
grid.draw(rbind(ggplotGrob(p1),ggplotGrob(p2),size="last"))
grid.draw(cbind(ggplotGrob(p1),ggplotGrob(p2),size="last"))

@kohske さんにこれを教えてもろた(だがグリッドは揃うが描画されるプロットがずれる)
プロットの幅をアレしなければならないぽい

これに沿ってやってみる

ggplot-vs-gtable
library(ggplot2)
library(gtable)

## loading data
df <- read.delim("./data.tab")

x <- log10(df$length)
y <- log10(df$amount)

## Main scatterplot
p1 <- ggplot(df, aes(x, y)) + 
  geom_point(alpha = 1/10) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  expand_limits(y = c(min(y) - .1*diff(range(y)), 
                      max(y) + .1*diff(range(y))))  +
  expand_limits(x = c(min(x) - .1*diff(range(x)), 
                      max(x) + .1*diff(range(x))))  +
  theme(plot.margin= unit(c(0, 0, .5, .5), "lines"))

## To remove all axis labelling and marks from the two marginal plots
## DRY
blank <- element_blank()
theme_remove_all <- theme(axis.text = blank,
  axis.title = blank,
  axis.ticks =  blank,
  axis.ticks.margin = unit(0, "lines"),
  axis.ticks.length = unit(0, "cm"))

## Horizontal marginal density plot - to appear at the top of the chart
p2 <- ggplot(df, aes(x = x)) + 
  geom_density() +
  scale_x_continuous(expand = c(0, 0)) +
  expand_limits(x = c(min(x) - .1*diff(range(x)), 
                      max(x) + .1*diff(range(x))))  +
  theme_remove_all +
  theme(plot.margin= unit(c(.5, 0, 0, .5), "lines"))

## Vertical marginal density plot - to appear at the right of the chart
p3 <- ggplot(df, aes(x = y)) + 
  geom_density() +
  scale_x_continuous(expand = c(0, 0)) +
  expand_limits(x = c(min(y) - .1*diff(range(y)), 
                      max(y) + .1*diff(range(y))))  +
  coord_flip() +
  theme_remove_all +
  theme(plot.margin= unit(c(0, .5, .5, 0), "lines"))

## Get the gtables
gt1 <- ggplot_gtable(ggplot_build(p1))
gt2 <- ggplot_gtable(ggplot_build(p2))
gt3 <- ggplot_gtable(ggplot_build(p3))

## Get maximum widths and heights for x-axis and y-axis title and text
maxWidth = unit.pmax(gt1$widths[2:3], gt2$widths[2:3])
maxHeight = unit.pmax(gt1$heights[4:5], gt3$heights[4:5])

## Set the maximums in the gtables for gt1, gt2 and gt3
gt1$widths[2:3] <- as.list(maxWidth)
gt2$widths[2:3] <- as.list(maxWidth)

gt1$heights[4:5] <- as.list(maxHeight)
gt3$heights[4:5] <- as.list(maxHeight)

## Combine the scatterplot with the two marginal boxplots
## Create a new gtable
gt <- gtable(widths = unit(c(7, 2), "null"), height = unit(c(2, 7), "null"))

## Instert gt1, gt2 and gt3 into the new gtable
gt <- gtable_add_grob(gt, gt1, 2, 1)
gt <- gtable_add_grob(gt, gt2, 1, 1)
gt <- gtable_add_grob(gt, gt3, 2, 2)

## And render the plot
grid.newpage()
grid.draw(gt)

でけた

ほぼ先ほどのblogの写経である
Thank you Sandy!
個別のパラメータがどこに影響するのか理解できてない部分もあるが案件が片付いたら追ってアレすることにしたい(だが多分必要に迫られるまでしない気がする)

これをhistogramにfixしてやればと思ったがdensityで必要な情報は伝わるのでこれ以上コストはかけ(たく)ない

途中"有限でない location かつ/または 有限でない viewport です"という不親切なエラーが出たが「Rで理解できないエラーが出たらとりあえずRのバージョンを上げてみる」というスマートな解決策を実施

R 3.0.1で今はエラーも出ずにちゃんと描画してくれています

なおその過程でgfortranが古い→gccが古い→Xcodeが古い→XcodeをアップデートするにはOSXをアップデートしなければならない→無理→無理矢理AppleのDevCentreからLionで使えるXcodeを持ってくる→パーミッションの関係で入らない→無理矢理入れる→brewが死ぬ→zshが死ぬ→terminalが立ち上がらない→terminalのShellメニューからコマンドを直接実行→/usr/bin/zsh→brewを綺麗にしてbrew link zshを実行する→ターミナル復活→Rを綺麗にして強引にgfortranごとアップデートする、という楽しい水曜の夜を過ごした

無事プロットができたので夜の北千住でラーメンを食いましたがにんにくを入れすぎたことを後悔している

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1