2012-06-02 112 views
68

我想通過使用ggplot2重現下面的圖形。我可以靠近,但不能刪除頂部和右邊界。下面我使用ggplot2進行了幾次嘗試,包括在Stackoverflow上或通過Stackoverflow發現的一些建議。不幸的是,我一直無法得到這些建議的工作。從ggplot2中刪除網格,背景顏色以及頂部和右側邊框

我希望有人能夠糾正下面的一個或多個代碼片段。

謝謝你的任何建議。

# 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()) 
+3

,張貼在下面留言,但是現在可以用做+ theme_classic() – nsheff

回答

88

編輯忽略此答案。現在有更好的答案。查看評論。使用+ theme_classic()

編輯

這是一個更好的版本。在原帖中提到的錯誤仍然存​​在(我認爲)。但軸線繪製在面板下方。因此,請刪除panel.borderpanel.background以查看軸線。

library(ggplot2) 
a <- seq(1,20) 
b <- a^0.25 
df <- as.data.frame(cbind(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()) 

enter image description here

原帖 此靠攏。 axis.line在y軸上沒有工作(see here),但似乎尚未修復。因此,在移除面板邊框後,必須使用geom_vline分別繪製y軸。

library(ggplot2) 
library(grid) 

a <- seq(1,20) 
b <- a^0.25 
df <- as.data.frame(cbind(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 

極端點被限幅,但限幅可以通過baptiste使用代碼復原。

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

enter image description here

或者使用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) 
+3

我想我也許應該給對勾現在的其他答案。你的回答是絕對值得的2012年的複選標記,是非常有益的。但是,現在另一個答案似乎更好。如果僅出於歷史原因,我討厭切換複選標記。 –

+2

如果是我,我會在12個月前完成它。 –

71

對ggplot(0.9.2+)的最近更新已經徹底改變了主題的語法。最值得注意的是,opts()現已棄用,已被theme()取代。 Sandy's答案仍然會(截至2012年1月)生成圖表,但會導致R發出一系列警告。

這裏的更新的代碼反映當前ggplot語法:

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

#base ggplot object 
p <- ggplot(df, aes(x = a, y = b)) 

p + 
    #plots the points 
    geom_point() + 

    #theme with white background 
    theme_bw() + 

    #eliminates background, gridlines, and chart border 
    theme(
    plot.background = element_blank() 
    ,panel.grid.major = element_blank() 
    ,panel.grid.minor = element_blank() 
    ,panel.border = element_blank() 
) + 

    #draws x and y axis line 
    theme(axis.line = element_line(color = 'black')) 

生成:

plot output

+29

或更簡單? ggplot(df,aes(x = a,y = b))+ geom_point()+ theme_classic() – user20650

+0

這些方法都不適用於我使用ggplot2 2.1.0 ... @ wkretzsch的回答很好。 – Nova

17

theme_classic()一個替代方案是隨附cowplot包主題,theme_cowplot()(與自動加載包)。它看起來類似於theme_classic(),有一些細微的差異。最重要的是,默認標籤尺寸較大,因此可以在出版物中使用所得數字,而無需進一步修改(特別是如果您使用save_plot()而不是ggsave()保存它們)。此外,背景是透明的,而不是白色,如果您想在Illustrator中編輯圖形,這可能很有用。最後,在我看來,多面情節看起來更好。

例子:

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

p <- ggplot(df, aes(x = a, y = b)) + geom_point() 
save_plot('plot.png', p) # alternative to ggsave, with default settings that work well with the theme 

這是由這個代碼生成的文件plot.png樣子: enter image description here

免責聲明:我包的作者。

7

我跟着Andrew's answer,但我也不得不按照https://stackoverflow.com/a/35833548,並分別設置x和y軸,因爲我的版本的ggplot(v2.1.0)中的一個錯誤。

而不是

theme(axis.line = element_line(color = 'black')) 

我用

theme(axis.line.x = element_line(color="black", size = 2), 
    axis.line.y = element_line(color="black", size = 2)) 
+0

非常酷的答案。沒有其他工作爲我 - 我想這個錯誤應該更好地記錄。 – thiagoveloso

相關問題