【问题标题】:R: Specify special points in line graph using ggplotR:使用ggplot指定折线图中的特殊点
【发布时间】:2015-11-11 14:20:31
【问题描述】:

我想使用 ggplot 在折线图上指定两个点,如下所示。我该怎么做?

【问题讨论】:

  • 你能提供一个可重现的例子吗?

标签: r ggplot2 linegraph


【解决方案1】:
library(ggplot2)

ggplot(mtcars, aes(wt, mpg)) + 
  geom_line() + 
  geom_segment(aes(x = 2, y = 0, xend = 2, yend = 25), linetype=2) + 
  geom_segment(aes(x = 0, y = 25, xend = 2, yend = 25), linetype=2) + 
  geom_point(data=data.frame(lat = c(25), long = c(2)),aes(long,lat),colour="blue",size=4) + 
  geom_segment(aes(x = 1, y = 0, xend = 1, yend = 20), linetype=2) + 
  geom_segment(aes(x = 0, y = 20, xend = 1, yend = 20), linetype=2) + 
  geom_point(data=data.frame(lat = c(20), long = c(1)),aes(long,lat),colour="blue",size=4)

您可以使用 geom_segment 和/或 geom_point。

查看here 了解如何更改轴标签/刻度。

编辑:为了第二点,我编辑了我的帖子。

【讨论】:

  • 一个建议:如果需要多个段,您可以考虑创建一个包含所有相关信息的数据框,防止重复geom_segment。在这种情况下,您可以执行类似segment_data <- data.frame(x=c(2,0,1,0),y=c(0,25,0,20),xend=c(2,2,1,1), yend=c(25,25,20,20)) 然后b + geom_segment(data=segment_data, aes(x=x,xend=xend,y=y,yend=yend)) 的操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多