如何在ggplot2中创建等效的基本R图'type = b'? [英] How to create base R plot 'type = b' equivalent in ggplot2?
问题描述
基于plot()
的功能允许设置type='b'
并获得线与点的组合图,其中点从线段偏移了
Base plot()
functionality allows one to set type='b'
and get a combined line and point plot in which the points are offset from the line segments
plot(pressure, type = 'b', pch = 19)
我可以轻松地用线和点创建ggplot,如下所示.
I can easily create a ggplot with lines and points as follows.
ggplot(pressure, aes(temperature, pressure)) +
geom_line() +
geom_point()
但是,直线一直延伸到要点.我可以设想一种可以使用其他几何体(例如geom_segment()
?)将类似type='b'
功能的东西混在一起的方法,但是我想知道是否有更直接的方法可以通过geom_line()
和geom_point()
来完成此任务.
The lines, however, run right up to the points. I can envision a way that I might hack together something like type='b'
functionality using other geoms (e.g. geom_segment()
?), but I am wondering if there is a more direct way to accomplish this with geom_line()
and geom_point()
.
推荐答案
一种稍微有点怪异的方法是在较大的白点上绘制较小的黑点:
A slightly hacky way of doing this is to overplot a small black point on a larger white point:
ggplot(pressure, aes(temperature, pressure)) +
geom_line() +
geom_point(size=5, colour="white") +
geom_point(size=2) +
theme_classic() +
theme(panel.background = element_rect(colour = "black"))
此外,按照 ggplot中的控制点边界粗细,在ggplot2
的2.0.0版本中,可以使用geom_point
的stroke
参数控制边框的粗细,因此可以仅用(例如)geom_point(size=2, shape=21, fill="black", colour="white", stroke=3)
替换这两个geom_point
,而无需覆盖这些点.
In addition, following Control point border thickness in ggplot, in version 2.0.0 of ggplot2
it's possible to use the stroke
argument of geom_point
to control the border thickness, so the two geom_point
s can be replaced by just (e.g.) geom_point(size=2, shape=21, fill="black", colour="white", stroke=3)
, eliminating the need to overlay the points.
这篇关于如何在ggplot2中创建等效的基本R图'type = b'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!