[英]Remove grid, background color, and top and right borders from ggplot2
我想通過使用 ggplot2 重現下面的 plot。我可以靠近,但不能刪除頂部和右側邊框。 下面我展示了使用 ggplot2 的幾次嘗試,包括在 Stackoverflow 上或通過 Stackoverflow 找到的一些建議。 不幸的是,我無法讓這些建議發揮作用。
我希望有人能夠更正下面的一個或多個代碼片段。
謝謝你的任何建議。
# 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()
編輯
這是一個更好的版本。 原始帖子中下面提到的錯誤仍然存在(我認為)。 但是軸線是在面板下方繪制的。 因此,刪除panel.border
和panel.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())
原帖這就接近了。 有一個axis.line
不能在 y 軸上工作的錯誤( 請參閱此處),該錯誤似乎尚未修復。 因此,移除面板邊框后,必須使用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)
或者使用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)
最近對 ggplot (0.9.2+) 的更新徹底修改了主題的語法。 最值得注意的是, opts()
現在已被棄用,已被替換為theme()
。 桑迪的回答仍然會(截至 12 年 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'))
產生:
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
樣子:
免責聲明:我是包的作者。
我遵循了安德魯的回答,但由於我的 ggplot (v2.1.0) 版本中的錯誤,我也不得不遵循https://stackoverflow.com/a/35833548並分別設置 x 和 y 軸。
代替
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))
上述選項不適用於使用sf
和geom_sf()
創建的地圖。 因此,我想在這里添加相關的ndiscr
參數。 這將創建一個漂亮的干凈地圖,僅顯示特征。
library(sf)
library(ggplot2)
ggplot() +
geom_sf(data = some_shp) +
theme_minimal() + # white background
theme(axis.text = element_blank(), # remove geographic coordinates
axis.ticks = element_blank()) + # remove ticks
coord_sf(ndiscr = 0) # remove grid in the background
從上面安德魯的回答中簡化導致這個關鍵主題生成半邊。
theme (panel.border = element_blank(),
axis.line = element_line(color='black'))
這是一個非常簡單的答案
yourPlot +
theme(
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")
)
就這么簡單。 來源:結束本文章
您也可以檢查panel.background
。
theme(
panel.background = element_rect(fill = "black"),
panel.grid.major = element_blank(), panel.grid.minor = element_blank()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.