简体   繁体   English

在 R 基础 plot 和 GGPlot2 中的 x 轴和 y 轴上显示相应的虚线

[英]Show corresponding dash line on x-axis and y-axis in R base plot and GGPlot2

I need to show corresponding dash line of the data points on x-axis and y-axis, and the corresponding (x,y) coordinates near the respective data points on the graph.我需要在 x 轴和 y 轴上显示数据点的相应虚线,以及图表上各个数据点附近的相应 (x,y) 坐标。 I need to do this in base R plot and GGPlot2 respectively.我需要分别在基础 R plot 和 GGPlot2 中执行此操作。

Does anyone know how to do it?有谁知道该怎么做?

It sounds like you're looking for something like this:听起来你正在寻找这样的东西:

dat <- data.frame(x=1:10, y=1:10)

Base R:底座 R:

plot(dat$x, dat$y, axes=F, xlab="", ylab="", type="n")
segments(x0 = dat$x, x1 = dat$x, y0 = 0, y1 = dat$y, lty = 2)
segments(x0 = 0, x1 = dat$x, y0 = dat$y, y1 = dat$y, lty = 2)
text(paste0(dat$x, ", ", dat$y), x=dat$x+0.2, y=dat$y+0.2)

在此处输入图像描述

ggplot2: ggplot2:

library(ggplot2)
ggplot(data = dat) +
  geom_segment(aes(x=x, xend=x, y=0, yend=y), lty=2) +
  geom_segment(aes(x=0, xend=x, y=y, yend=y), lty=2) +
  geom_text(aes(x=x, y=y, label=paste0(x, ", ", y)), nudge_x = 0.1, nudge_y = 0.1) +
  theme_void()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM