R绘图类型“b”用文本而不是点 - 使用 ggplot2 的斜率图
ggplot2 有没有办法获得绘图类型“b”?请参阅示例:
x <- c(1:5)
y <- x
plot(x,y,type="b")
理想情况下,我想用点的值替换点,以获得与以下著名示例类似的内容:
编辑: 这里有一些示例数据(我想用绘图类型“b”在一个方面绘制每个“猫”):
df <- data.frame(x=rep(1:5,9),y=c(0.02,0.04,0.07,0.09,0.11,0.13,0.16,0.18,0.2,0.22,0.24,0.27,0.29,0.31,0.33,0.36,0.38,0.4,0.42,0.44,0.47,0.49,0.51,0.53,0.56,0.58,0.6,0.62,0.64,0.67,0.69,0.71,0.73,0.76,0.78,0.8,0.82,0.84,0.87,0.89,0.91,0.93,0.96,0.98,1),cat=rep(paste("a",1:9,sep=""),each=5))
is there a way in ggplot2 to get the plot type "b"? See example:
x <- c(1:5)
y <- x
plot(x,y,type="b")
Ideally, I want to replace the points by their values to have something similar to this famous example:
EDIT:
Here some sample data (I want to plot each "cat" in a facet with plot type "b"):
df <- data.frame(x=rep(1:5,9),y=c(0.02,0.04,0.07,0.09,0.11,0.13,0.16,0.18,0.2,0.22,0.24,0.27,0.29,0.31,0.33,0.36,0.38,0.4,0.42,0.44,0.47,0.49,0.51,0.53,0.56,0.58,0.6,0.62,0.64,0.67,0.69,0.71,0.73,0.76,0.78,0.8,0.82,0.84,0.87,0.89,0.91,0.93,0.96,0.98,1),cat=rep(paste("a",1:9,sep=""),each=5))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过绘制没有任何内容的图来设置轴。
然后使用
text
来制作数据点。您可以使用
lines
添加线段。编辑:完全未能在问题中提及 ggplot。试试这个。
另一个编辑:鉴于您的新数据集和请求,这就是您所需要的。
另一个编辑:我们开始解决一个真正的问题。就像“如何使线条不完全到达点”一样。
简而言之,这不是 ggplot2 中执行此操作的标准方法。执行此操作的正确方法是使用
geom_segment
并在数据点之间进行插值。然而,这需要付出很大的努力,所以我建议一个更简单的方法:在你的点周围画一个大的白色圆圈。这样做的缺点是它使网格线看起来很愚蠢,所以你必须去掉它们。Set up the axes by drawing the plot without any content.
Then use
text
to make your data points.You can add line segments with
lines
.EDIT: Totally failed to clock the mention of ggplot in the question. Try this.
ANOTHER EDIT: Given your new dataset and request, this is what you need.
YET ANOTHER EDIT: We're starting to approach a real question. As in 'how do you make the lines not quite reach the points'.
The short answer is that that isn't a standard way to do this in ggplot2. The proper way to do this would be to use
geom_segment
and interpolate between your data points. This is quite a lot of effort however, so I suggest an easier fudge: draw big white circles around your points. The downside to this is that it makes the gridlines look silly, so you'll have to get rid of those.gridExtra 中有一个实验性的 grob 可以在网格图形中实现此功能,
There's an experimental grob in gridExtra to implement this in Grid graphics,
现在使用
ggh4x::geom_pointpath
可以轻松实现这一点。设置shape = NA
并添加geom_text
图层。由 reprex 包 (v2.0.1)
This is now easy with
ggh4x::geom_pointpath
. Setshape = NA
and add ageom_text
layer.Created on 2021-11-13 by the reprex package (v2.0.1)
制作出色斜率图的另一种方法是使用包
CGPfunctions
。您还有很多选项可供选择。您可以在这里找到一个很好的教程:
https://www .r-bloggers.com/2018/06/creating-slopegraphs-with-r/
Another way to make great slope graphs is using the package
CGPfunctions
.You have also many options to choose. You can find a good tutorial here:
https://www.r-bloggers.com/2018/06/creating-slopegraphs-with-r/