Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove part of x axis, keeping x axis limits

My graph displays text outside of existing values range on the x axis.

In order to get the text at the correct y level, I increased x axis limits using coord_cartesian.

I would like to remove part of the x axis, keeping the x limit of the plot.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My current reproducible code

### Loading library
library(ggplot2)

### Initiating data
data(iris)

### Initiating plot
ggplot(data=iris, aes(x=Sepal.Length, y=Species)) + 
  geom_point() + 
  coord_cartesian(xlim=c(4, 10)) + 
  geom_text(aes(y=Species, x=9, label=Species)) + 
  theme_classic()

enter image description here

My aim

enter image description here

Not sure this is feasible

>Solution :

ggplot(data=iris, aes(x=Sepal.Length, y=Species)) + 
  geom_point() + 
  coord_cartesian(xlim=c(4, 10)) + 
  geom_text(aes(y=Species, x=9, label=Species)) + 
  theme_classic() +
  scale_x_continuous(breaks = c(4,6,8)) +
  guides(x = guide_axis(cap = "upper"))

resulting plot showing the x-axis drawn only to the highest break

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading