1. ホーム
  2. r

[解決済み] ggplot2からグリッド、背景色、上下のボーダーを削除する

2022-08-01 07:22:08

質問

ggplot2を使って、すぐ下のプロットを再現したい。 近づけることはできますが、上と右の境界を取り除くことができません。 以下に、Stackoverflowで見つけたいくつかの提案を含め、ggplot2を使ったいくつかの試行を紹介します。 残念ながら、私はそれらの提案がうまくいくようにすることができませんでした。

私は、誰かが以下のコードスニペットの1つ以上を修正することができるかもしれないことを期待しています。

どんな提案でもありがとうございます。

# desired plot
a <- seq(1,20)
b <- a^0.25
plot(a,b, bty = "l")


library(ggplot2)

df <- as.data.frame(cbind(a,b))

# 1. ggplot2 default
ggplot(df, aes(x = a, y = b)) + geom_point()

# 2. removes background color
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black'))

# 3. also removes gridlines
none <- theme_blank()
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none)

# 4. does not remove top and right border
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none) + opts(panel.border = none)

# 5. does not remove top and right border
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none) + opts(axis.line = theme_segment())

# 6. removes x and y axis in addition to top and right border
# http://stackoverflow.com/questions/5458409/remove-top-and-right-border-from-ggplot2
ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none) + opts(panel.background=theme_rect(colour=NA))

# 7. returns error when attempting to remove top and right border
# https://groups.google.com/group/ggplot2/browse_thread/thread/f998d113638bf251
#
# Error in el(...) : could not find function "polylineGrob"
#
theme_L_border <- function(colour = "black", size = 1, linetype = 1) { 
   structure( 
     function(x = 0, y = 0, width = 1, height = 1, ...) { 
       polylineGrob( 
         x=c(x+width, x, x), y=c(y,y,y+height), ..., default.units = "npc", 
         gp=gpar(lwd=size, col=colour, lty=linetype), 
       ) 
     }, 
     class = "theme", 
     type = "box", 
     call = match.call() 
   )
}

ggplot(df, aes(x = a, y = b)) + geom_point() + opts(panel.background = theme_rect(fill='white', colour='black')) + opts(panel.grid.major = none, panel.grid.minor = none) + opts( panel.border = theme_L_border())

どのように解決するのですか?

編集 この回答は無視してください。より良い答えがあります。コメントを見てください。使用方法 + theme_classic()

EDIT

これは、より良いバージョンです。元記事の下記のバグは残っている(と思う)。しかし、軸線はパネルの下に描かれています。そのため、両方の panel.borderpanel.background をクリックすると、軸線が表示されます。

library(ggplot2)
a <- seq(1,20)
b <- a^0.25
df <- data.frame(a,b)

ggplot(df, aes(x = a, y = b)) + geom_point() +
  theme_bw() +
  theme(axis.line = element_line(colour = "black"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank()) 

<イグ

元の記事 これは近いですね。にはバグがありました。 axis.line が Y 軸で機能しないバグがありました ( はこちら を参照)、これはまだ修正されていないようです。したがって、パネルのボーダーを取り除いた後、Y 軸を個別に描画するには geom_vline .

library(ggplot2)
library(grid)

a <- seq(1,20)
b <- a^0.25
df <- data.frame(a,b)

p = ggplot(df, aes(x = a, y = b)) + geom_point() +
   scale_y_continuous(expand = c(0,0)) +
   scale_x_continuous(expand = c(0,0)) +
   theme_bw() +
   opts(axis.line = theme_segment(colour = "black"),
        panel.grid.major = theme_blank(),
        panel.grid.minor = theme_blank(),
        panel.border = theme_blank()) +
    geom_vline(xintercept = 0)
p

極端な点は切り取られていますが、この切り取られている部分は バプティスト .

gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)

<イグ

あるいは limits を使ってパネルの境界を移動することもできます。

ggplot(df, aes(x = a, y = b)) + geom_point() +
   xlim(0,22) +  ylim(.95, 2.1) +
   scale_x_continuous(expand = c(0,0), limits = c(0,22)) +
   scale_y_continuous(expand = c(0,0), limits = c(.95, 2.2)) +   
   theme_bw() +
   opts(axis.line = theme_segment(colour = "black"),
        panel.grid.major = theme_blank(),
        panel.grid.minor = theme_blank(),
        panel.border = theme_blank()) +
    geom_vline(xintercept = 0)