LucasMation
LucasMation

Reputation: 2511

How to add x-axis ticks and labels bellow every point in a ggplot2 scatter plot

Let's say I have this plot

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

Which renders:

enter image description here

How can I add axis ticks and labels so that for every point there is a corresponding tick and label?

Upvotes: 3

Views: 2363

Answers (1)

gcons
gcons

Reputation: 431

You need to use scale_x_continuous to achieve what you want since your x axis is not discrete. The following code should work:

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

Upvotes: 6

Related Questions