TomTe
TomTe

Reputation: 319

ggplot2 plot axis values only where values are displayed

I am currently working with these "lollipop" plots out of ggplot2. I am trying to have the year values on the axis plotted only where observations are displayed. So I guess this would be an individual axis formatting but I couldn't find anything for this so far. I have a multitude of such plots to do - so that'S why i marked the red ones manually.

Any ideas?

This is my code and my plot:

enter image description here

# Create data
value1 <- c(33000000,45000000,45000000, 60000000,65000000,40000000)
value2 <- c(102984862,129342769,147717833,300228084,240159255,312242626)
value3 <- c(2002,2004,2007,2010,2012,2016)

data <- data.frame(value1=value1, value2=value2)

# Plot
ggplot(data) +
  geom_segment( aes(x=value3, xend=value3, y=value1, yend=value2), color="grey") +
  geom_point( aes(x=value3, y=value1), color=rgb(0.0,0.0,0.0,0.9), size=2 ) +
  geom_point( aes(x=value3, y=value2), color=rgb(0.0,0.9,0.0,0.9), size=2 ) +
  coord_flip()+
  theme_grey() +
  scale_y_continuous(name="", limits = c(0, 400000000)) +
  theme(legend.position = "none",panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.text.x = element_blank()) +

Upvotes: 0

Views: 146

Answers (1)

Will Oldham
Will Oldham

Reputation: 1054

You can add: scale_x_continuous(breaks = value3).

Upvotes: 1

Related Questions