R 将x和y轴添加到所有面_包裹
通常希望尽量减少绘图中的墨迹。我有一个刻面的绘图(R 将x和y轴添加到所有面_包裹,r,ggplot2,facet,facet-wrap,R,Ggplot2,Facet,Facet Wrap,通常希望尽量减少绘图中的墨迹。我有一个刻面的绘图(facet\u wrap),希望在保持可读性的同时尽可能多地去除墨水。我已经按照我的意愿进行了设置,除了x轴和y轴不存在于刻面(子地块)中,除非在最左侧或底部。去掉这么多墨水后,我相信眼睛需要这些线索,我想知道如何在面_-wrap内的所有绘图中放置x轴和y轴。以下是我迄今为止的代码、当前输出和deired输出(红线是所需的外接程序): 当前绘图 所需绘图 这将大大简化事情: library('ggthemes') ggplot(mtcars,
facet\u wrap
),希望在保持可读性的同时尽可能多地去除墨水。我已经按照我的意愿进行了设置,除了x轴和y轴不存在于刻面(子地块)中,除非在最左侧或底部。去掉这么多墨水后,我相信眼睛需要这些线索,我想知道如何在面_-wrap
内的所有绘图中放置x轴和y轴。以下是我迄今为止的代码、当前输出和deired输出(红线是所需的外接程序):
当前绘图
所需绘图
这将大大简化事情:
library('ggthemes')
ggplot(mtcars, aes(mpg, hp)) + geom_point() + facet_wrap(~carb, scales='free') +
theme_tufte() + theme(axis.line=element_line()) +
scale_x_continuous(limits=c(10,35)) + scale_y_continuous(limits=c(0,400))
这里有一个很长的解决方法 首先,将原始绘图存储为对象,然后创建另一个没有轴记号和轴文本的绘图
p1<-ggplot(mtcars, aes(mpg, hp)) +
geom_point() +
facet_wrap(~carb) +
theme(panel.grid = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
axis.line = element_line(),
strip.background = element_blank(),
panel.margin = unit(2, "lines"))
p2<-ggplot(mtcars, aes(mpg, hp)) +
geom_point() +
facet_wrap(~carb) +
theme(panel.grid = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
axis.line = element_line(),
strip.background = element_blank(),
panel.margin = unit(2, "lines"),
axis.ticks=element_blank(),
axis.text=element_blank())
因此,使用绘图2的相应GROB替换绘图1中的零GROB,您将得到轴线
g1[[1]][[15]]<-g2[[1]][[14]]
g1[[1]][[16]]<-g2[[1]][[14]]
g1[[1]][[18]]<-g2[[1]][[14]]
g1[[1]][[19]]<-g2[[1]][[14]]
g1[[1]][[20]]<-g2[[1]][[23]]
g1[[1]][[21]]<-g2[[1]][[23]]
g1[[1]][[22]]<-g2[[1]][[23]]
grid.draw(g1)
g1[[1][[15]]最简单的方法是在每个绘图面板中添加线段
ggplot(mtcars, aes(mpg, hp)) +
geom_point() +
facet_wrap(~carb) +
theme_minimal() +
annotate("segment", x=-Inf, xend=Inf, y=-Inf, yend=-Inf)+
annotate("segment", x=-Inf, xend=-Inf, y=-Inf, yend=Inf)
在托马斯从上面回答之后-
您只需在facet\u wrap
中设置scales='free'
,并确保在scale\u x\u continuous
和scale\u y\u continuous
ggplot(mtcars, aes(mpg, hp)) + geom_point() + facet_wrap(~carb, scales='free') +
scale_x_continuous(limits=c(10,35)) + scale_y_continuous(limits=c(0,400))
软件包增加了这个功能;看见
. (示例代码和绘图从那里开始。)
在您的示例中,我发现一个找不到函数“unit”
的错误。@Thomas,我很抱歉。让我添加所使用的库。您无法轻松完成此操作,因为除非scales=“free”
,否则axis.line
主题元素不会显示在每个面板上,panel.border
主题元素是一个矩形,并且您不能为矩形元素的不同侧面指定不同的值。你可以用geom_vline
和geom_hline
来伪装它,但要让它看起来正确是很困难的。你可以从@baptiste的答案中得到一些想法,而且。gridExtra::borderGrob(type=9)
可能会有所帮助。为什么不在GTTable的每个面板上添加段呢?@baptiste,因为我不知道怎么做。欢迎你展示这一点。效果很好,但我看到巴蒂斯提供了一个更概括的答案,他删掉了+1@TylerRinker是的,巴普蒂斯特的答案要好得多。最简单的答案不幸的是scales=“free”
对我来说并不好+1@TylerRinker请参见编辑。没有注意到您将所有内容限制为相同的比例。此答案不适用于最新的ggplot2 2.2版。1@wikiselev适用于带有ggplot2_2.2.1和ggthemes_3.4的我。0@Thomas那好吧,但我想是因为我的主题吧?ggplot本身不起作用?对我来说,它在附加行的不同宽度处结束,这很烦人……我希望在我的图形中也使用这个简单的答案,但我的x轴使用日期,这会产生错误:“错误:无效输入:日期只适用于类日期的对象”。有什么想法吗?@JelenaČuklina尝试添加一个size
参数,例如,annotate(“段”,x=-Inf,xend=Inf,y=-Inf,yend=-Inf,size=1)+
。这对我有用。
ggplot(mtcars, aes(mpg, hp)) +
geom_point() +
facet_wrap(~carb) +
theme_minimal() +
annotate("segment", x=-Inf, xend=Inf, y=-Inf, yend=-Inf)+
annotate("segment", x=-Inf, xend=-Inf, y=-Inf, yend=Inf)
ggplot(mtcars, aes(mpg, hp)) + geom_point() + facet_wrap(~carb, scales='free') +
scale_x_continuous(limits=c(10,35)) + scale_y_continuous(limits=c(0,400))
library(lemon)
p + facet_rep_grid(drv ~ cyl) + coord_capped_cart(bottom='both', left='both') +
theme_bw() + theme(panel.border=element_blank(), axis.line=element_line())