R绘图类型“b”用文本而不是点 - 使用 ggplot2 的斜率图

发布于 2024-09-25 08:32:50 字数 579 浏览 2 评论 0原文

ggplot2 有没有办法获得绘图类型“b”?请参阅示例:

x <- c(1:5)
y <- x 
plot(x,y,type="b")

理想情况下,我想用点的值替换点,以获得与以下著名示例类似的内容:

alt text

编辑: 这里有一些示例数据(我想用绘图类型“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:

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鹤仙姿 2024-10-02 08:32:50

通过绘制没有任何内容的图来设置轴。

plot(x, y, type = "n")

然后使用 text 来制作数据点。

text(x, y, labels = y)

您可以使用lines添加线段。

lines(x, y, col = "grey80")

编辑:完全未能在问题中提及 ggplot。试试这个。

dfr <- data.frame(x = 1:5, y = 1:5)
p <- ggplot(dfr, aes(x, y)) + 
  geom_text(aes(x, y, label = y)) + 
  geom_line(col = "grey80")
p

另一个编辑:鉴于您的新数据集和请求,这就是您所需要的。

ggplot(df, aes(x, y)) + geom_point() + geom_line() + facet_wrap(~cat)

另一个编辑:我们开始解决一个真正的问题。就像“如何使线条不完全到达点”一样。

简而言之,这不是 ggplot2 中执行此操作的标准方法。执行此操作的正确方法是使用 geom_segment 并在数据点之间进行插值。然而,这需要付出很大的努力,所以我建议一个更简单的方法:在你的点周围画一个大的白色圆圈。这样做的缺点是它使网格线看起来很愚蠢,所以你必须去掉它们。

ggplot(df, aes(x, y)) + 
   facet_wrap(~cat) +
   geom_line() + 
   geom_point(size = 5, colour = "white") + 
   geom_point() + 
   opts(panel.background = theme_blank())

Set up the axes by drawing the plot without any content.

plot(x, y, type = "n")

Then use text to make your data points.

text(x, y, labels = y)

You can add line segments with lines.

lines(x, y, col = "grey80")

EDIT: Totally failed to clock the mention of ggplot in the question. Try this.

dfr <- data.frame(x = 1:5, y = 1:5)
p <- ggplot(dfr, aes(x, y)) + 
  geom_text(aes(x, y, label = y)) + 
  geom_line(col = "grey80")
p

ANOTHER EDIT: Given your new dataset and request, this is what you need.

ggplot(df, aes(x, y)) + geom_point() + geom_line() + facet_wrap(~cat)

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.

ggplot(df, aes(x, y)) + 
   facet_wrap(~cat) +
   geom_line() + 
   geom_point(size = 5, colour = "white") + 
   geom_point() + 
   opts(panel.background = theme_blank())
忆离笙 2024-10-02 08:32:50

gridExtra 中有一个实验性的 grob 可以在网格图形中实现此功能,

 library(gridExtra)
 grid.newpage() ; grid.barbed(pch=5)

在此处输入图像描述

There's an experimental grob in gridExtra to implement this in Grid graphics,

 library(gridExtra)
 grid.newpage() ; grid.barbed(pch=5)

enter image description here

寒江雪… 2024-10-02 08:32:50

现在使用 ggh4x::geom_pointpath 可以轻松实现这一点。设置 shape = NA 并添加 geom_text 图层。

library(ggh4x)
#> Loading required package: ggplot2

df <- data.frame(x = rep(1:5, each = 5), 
                 y = c(outer(seq(0, .8, .2), seq(0.02, 0.1, 0.02), `+`)),
                 cat = rep(paste0("a", 1:5)))

ggplot(df, aes(x, y)) +
  geom_text(aes(label = cat)) +
  geom_pointpath(aes(group = cat, shape = NA))

reprex 包 (v2.0.1)

This is now easy with ggh4x::geom_pointpath. Set shape = NA and add a geom_text layer.

library(ggh4x)
#> Loading required package: ggplot2

df <- data.frame(x = rep(1:5, each = 5), 
                 y = c(outer(seq(0, .8, .2), seq(0.02, 0.1, 0.02), `+`)),
                 cat = rep(paste0("a", 1:5)))

ggplot(df, aes(x, y)) +
  geom_text(aes(label = cat)) +
  geom_pointpath(aes(group = cat, shape = NA))

Created on 2021-11-13 by the reprex package (v2.0.1)

染年凉城似染瑾 2024-10-02 08:32:50

制作出色斜率图的另一种方法是使用包CGPfunctions

library(CGPfunctions)
newggslopegraph(newcancer, Year, Survival, Type)

您还有很多选项可供选择。您可以在这里找到一个很好的教程:

https://www .r-bloggers.com/2018/06/creating-slopegraphs-with-r/

Another way to make great slope graphs is using the package CGPfunctions.

library(CGPfunctions)
newggslopegraph(newcancer, Year, Survival, Type)

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文