在R中使用ggplot时,如何删除绘图区域周围的边距?
我正在尝试生成一些分形图,对于R中ggplot的边距有一个问题。我正在使用以下代码生成分形图在R中使用ggplot时,如何删除绘图区域周围的边距?,r,plot,ggplot2,R,Plot,Ggplot2,我正在尝试生成一些分形图,对于R中ggplot的边距有一个问题。我正在使用以下代码生成分形图 library(ggplot2) library(grid) max_iter=25 cl=colours() step=seq(-2,0.8,by=0.005) points=array(0,dim=c(length(step)^2,3)) t=0 for(a in step) { for(b in step+0.6) { x=0;y=0;n=0;dist=0 while(n&
library(ggplot2)
library(grid)
max_iter=25
cl=colours()
step=seq(-2,0.8,by=0.005)
points=array(0,dim=c(length(step)^2,3))
t=0
for(a in step) {
for(b in step+0.6) {
x=0;y=0;n=0;dist=0
while(n<max_iter & dist<4) {
n=n+1
newx=a+x^2-y^2
newy=b+2*x*y
dist=newx^2+newy^2
x=newx;y=newy
}
if(dist<4) {
color=24 # black
} else {
color=n*floor(length(cl)/max_iter)
}
t=t+1
points[t,]=c(a,b,color)
}
}
df=as.data.frame(points)
ggplot(data=df, aes(V1, V2, color=cl[V3]))+
geom_point() +
theme(panel.background=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin = unit(c(0, 0, 0, 0), "cm"),
axis.ticks=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
plot.background = element_rect(fill = "transparent",colour = NA),
plot.margin = unit(c(0, 0, 0, 0), "cm"),
legend.position = 'none')
last_plot() + scale_colour_manual(values=sort(c("#00000000", rainbow(35)), decreasing=FALSE))
ggsave('mandelbrot.png');
print('Image Saved.')
库(ggplot2)
图书馆(网格)
最大电阻=25
cl=颜色()
步骤=顺序(-2,0.8,比=0.005)
点=阵列(0,尺寸=c(长度(步长)^2,3))
t=0
为了(一步一步){
对于(步骤+0.6中的b){
x=0;y=0;n=0;距离=0
虽然(n在使用您的代码后,我可以更清楚地看到您在寻找什么。这是:
gg <- ggplot(data=df, aes(V1, V2, color=cl[V3]))
gg +
geom_point() +
labs(x = NULL, y = NULL, title = NULL) +
scale_x_continuous(expand = c(0, 0), limits = range(df$V1)) +
scale_y_continuous(expand = c(0, 0), limits = range(df$V2)) +
scale_colour_manual(values = sort(c("#00000000", rainbow(35)), decreasing = FALSE)) +
theme(
panel.background = element_rect(fill = "transparent", colour = NA),
plot.background = element_rect(fill = "transparent", colour = NA),
panel.grid = element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(0, 0, 0, 0), "null"),
panel.margin = unit(c(0, 0, 0, 0), "null"),
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank(),
axis.line = element_blank(),
legend.position = "none",
axis.ticks.length = unit(0, "null"),
axis.ticks.margin = unit(0, "null"),
legend.margin = unit(0, "null")
)
gg通过设置负打印边距并将轴标题设置为NULL
,我可以去掉白色边框。我已经在下面的代码中标记了编辑
p = ggplot(data=df, aes(V1, V2, color=cl[V3]))+
geom_point() +
theme(panel.background=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin = unit(c(0, 0, 0, 0), "cm"),
axis.ticks=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
plot.background = element_rect(fill = "transparent",colour = NA),
plot.margin = unit(c(-1, -1.2, -1.2, -1.5), "cm"), # Edited code
legend.position = 'none') +
labs(x=NULL, y=NULL) # New code
您还可以使用软件包中的theme\u nothing()
:
不幸的是,您仍然需要添加labs(x=NULL,y=NULL)
,因为在ggplot2的主题机制中无法完全删除轴。您需要在比例参数中设置expand=c(0,0)
,以确保比例不会超出您的数据范围
结果:
一种仅从ggplot布局中选择绘图面板的方法。它创建ggplot,将绘图面板中的元素设置为元素_空白,并且不扩展x和y比例。然后创建ggplot grob,以便只能从布局中选择绘图面板
次要编辑:更新到ggplot2.2.0
library(ggplot2)
library(grid)
max_iter=25
cl=colours()
step=seq(-2,0.8,by=0.005)
points=array(0,dim=c(length(step)^2,3))
t=0
for(a in step) {
for(b in step+0.6) {
x=0;y=0;n=0;dist=0
while(n<max_iter & dist<4) {
n=n+1
newx=a+x^2-y^2
newy=b+2*x*y
dist=newx^2+newy^2
x=newx;y=newy
}
if(dist<4) {
color=24 # black
} else {
color=n*floor(length(cl)/max_iter)
}
t=t+1
points[t,]=c(a,b,color)
}
}
df=as.data.frame(points)
# ggplot with elements in the plot panel set to element_blank()
# and no expansion on the scales
p = ggplot(data=df, aes(V1, V2, color=cl[V3]))+
geom_point() +
scale_x_continuous(expand = c(0,0), limits=range(df$V1)) +
scale_y_continuous(expand = c(0,0), limits=range(df$V2))+
theme(panel.grid=element_blank(),
panel.background=element_rect(fill = "transparent",colour = NA),
panel.border=element_blank()) +
scale_colour_manual(values=sort(c("#00000000", rainbow(35)), decreasing=FALSE))
# Get the ggplot grob
gt = ggplotGrob(p)
# Select plot panel only
# gt = gt[6,4] # Using index notation; OR
gt = gtable::gtable_filter(gt, "panel")
# Draw it
grid.newpage()
grid.draw(gt)
# Set up a print method
class(gt) = c("Panel", class(gt))
print.Panel <- function(x) {
grid.newpage()
grid.draw(x)
}
gt
ggsave('mandelbrot.png', gt)
库(ggplot2)
图书馆(网格)
最大电阻=25
cl=颜色()
步骤=顺序(-2,0.8,比=0.005)
点=阵列(0,尺寸=c(长度(步长)^2,3))
t=0
为了(一步一步){
对于(步骤+0.6中的b){
x=0;y=0;n=0;距离=0
而(ntry使用null
作为单位,即plot.margin=unit(c(0,0,0,0),“null”)
是那些专门用于此绘图的硬编码plot.margin
条目吗?null
解决方案(现在可以使用了)在我的回答中,如果是这样的话,它会更通用。是的。它们是硬编码的,所以通用的解决方案肯定更好。或者,使用cowplot包中的theme\u nothing()
。但基本上是一样的。教鱼……我认为“null”
比“line”更有效
如果内存用于完全消除间距,则可以在grob级别进行验证(如果是这样,我可以稍后检查)。我肯定想知道在这个应用程序中,null
是否是比行
更好的单位选择,所以请告诉我您发现了什么。从先验角度看,null
似乎会产生一些不必要的开销,因为它是一个相对单位,可以根据绘图中其他元素的大小而增减。depr电子操作:现在是面板。间距而不是面板。边距gtable_过滤器(gt,“面板”)
可能更易于选择面板
library(ggplot2)
library(grid)
max_iter=25
cl=colours()
step=seq(-2,0.8,by=0.005)
points=array(0,dim=c(length(step)^2,3))
t=0
for(a in step) {
for(b in step+0.6) {
x=0;y=0;n=0;dist=0
while(n<max_iter & dist<4) {
n=n+1
newx=a+x^2-y^2
newy=b+2*x*y
dist=newx^2+newy^2
x=newx;y=newy
}
if(dist<4) {
color=24 # black
} else {
color=n*floor(length(cl)/max_iter)
}
t=t+1
points[t,]=c(a,b,color)
}
}
df=as.data.frame(points)
# ggplot with elements in the plot panel set to element_blank()
# and no expansion on the scales
p = ggplot(data=df, aes(V1, V2, color=cl[V3]))+
geom_point() +
scale_x_continuous(expand = c(0,0), limits=range(df$V1)) +
scale_y_continuous(expand = c(0,0), limits=range(df$V2))+
theme(panel.grid=element_blank(),
panel.background=element_rect(fill = "transparent",colour = NA),
panel.border=element_blank()) +
scale_colour_manual(values=sort(c("#00000000", rainbow(35)), decreasing=FALSE))
# Get the ggplot grob
gt = ggplotGrob(p)
# Select plot panel only
# gt = gt[6,4] # Using index notation; OR
gt = gtable::gtable_filter(gt, "panel")
# Draw it
grid.newpage()
grid.draw(gt)
# Set up a print method
class(gt) = c("Panel", class(gt))
print.Panel <- function(x) {
grid.newpage()
grid.draw(x)
}
gt
ggsave('mandelbrot.png', gt)