ggplot2:将其中一个边距保留为默认值

时间:2017-06-07 08:41:34

标签: r ggplot2

library(ggplot2)

#default
ggplot(iris,aes(Sepal.Length,Sepal.Width))+
  geom_point()

#modify margins
ggplot(iris,aes(Sepal.Length,Sepal.Width))+
  geom_point()+
  theme(plot.margin=grid::unit(c(1,1,1,1),"cm"))

现在我试图控制一些边距,而其他边缘则保留默认值。

#doesn't work
ggplot(iris,aes(Sepal.Length,Sepal.Width))+
  geom_point()+
  theme(plot.margin=grid::unit(c(1,1,1,NULL),"cm"))

使用NA或NULL似乎不起作用。

1 个答案:

答案 0 :(得分:1)

试试这个:

add_margin = function(...)  grid::convertUnit(theme_get()[["plot.margin"]] + margin(...), "cm")

 ggplot(iris,aes(Sepal.Length,Sepal.Width))+
   geom_point()+ theme(plot.margin = add_margin(l=2, unit="cm"))
相关问题