首页 > 解决方案 > 用 R + ggplot 在折线图中画一个点

问题描述

通常我在 Excel 中创建这样的图:

在此处输入图像描述

我正在探索将其移至 ggplot,但是在我看不到可以使用的几何图形中,关于如何使用 R 和 ggplot 复制它的任何想法?

标签: rggplot2tidyverse

解决方案


您可以执行以下操作:

library(ggplot2)

ggplot(df, aes(x = x, y = 1, label= label))+
  scale_x_continuous(limits = c(0,100))+
  geom_segment(x = 0, xend = 100, y = 1, yend = 1, color = "blue")+
  geom_segment(x = 0, xend = 0, y = 0.99, yend = 1.01, color = "blue")+
  geom_segment(x = 100, xend = 100, y = 0.99, yend = 1.01, color = "blue")+
  geom_point(color = "red", size = 3)+
  geom_segment(aes(x = x, xend = x, y = 0.99, yend = 1.01),color = "red")+
  ylim(0.9,1.1)+
  geom_text(aes(y = 1.025))+
  geom_text(aes(y = 0.975, label = paste(x,"%", sep = "")))+
  annotate(geom= "text",label = 0, y = 0.975, x = 0)+
  annotate(geom = "text", label = 100, y = 0.975, x = 100)+
  theme_void()

在此处输入图像描述

可重现的数据

df <- data.frame(x = c(10,25,65),
                 label = c("Variable1","Variable2","Variable3"))

推荐阅读