如何在 ggplot2 散点图中的每个点下方添加 x 轴刻度和标签

Luc*_*ion 3 r ggplot2 axis-labels

假设我有这个情节

iris2 <- iris %>% data.table
iris2 <- iris2[Sepal.Length<6]
iris2[,Sepal.Width:=mean(Sepal.Width),by=Sepal.Length]
iris2 %>% ggplot(aes(x=Sepal.Length,y=Sepal.Width,color=Species,group=Species)) + 
  geom_line() + geom_point()
Run Code Online (Sandbox Code Playgroud)

其呈现:

在此输入图像描述

如何添加轴刻度和标签,以便每个点都有相应的刻度和标签?

小智 6

你需要使用它scale_x_continuous来实现你想要的,因为你的 x 轴不是离散的。以下代码应该可以工作:

iris2 %>% 
    ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species, group = Species)) + 
    geom_line() + 
    geom_point() +
    scale_x_continuous(breaks = unique(iris$Sepal.Length))
Run Code Online (Sandbox Code Playgroud)