用R + ggplot在折线图中绘制点

时间:2020-02-28 16:58:49

标签: r ggplot2 tidyverse

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

enter image description here

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

1 个答案:

答案 0 :(得分:5)

您可以执行以下操作:

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()

enter image description here

可复制的数据

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