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

How to segment x axis in ggplot 2 in R?

I have a table with 16k rows, where I made a graph in ggplot with it. But the subtitles are many and I would like to show only a part of them in a uniform range.

elevation<-read.csv("http://wesleysc352.github.io/perfil_elev_miranda.csv", sep=";", header = TRUE)%>%
  as.data.frame()%>%
  mutate_at(c('distancia', 'cota'), as.numeric)%>%
  mutate(dist_km=((distancia)/1000))%>%
  mutate_at(vars(dist_km, distancia), funs(round(., 2)))

ggplot(elevation, aes(x=as.factor(dist_km), y=cota))+
  geom_point(size=0.1, color="red")+
  ggtitle("Profile Elevation river")+
  xlab("Dist(km)")+
  ylab("Elevation(m)")+
  theme_light()+
  theme(plot.title = element_text(size=13))+
  
  theme(axis.text.x=element_text(angle=90))+
  
  scale_x_discrete(breaks=seq(0,555, by=50))

I tried to use the following command scale_x_discrete(breaks=seq(0,555, by=50)) to segment every 50 km but it didn’t work, because the intervals were not uniform and it doesn’t even show the last distance 555km.

enter image description here

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

>Solution :

Not sure if this is what you’re looking for. If you want to have x-axis from 0 to 550 at 50 interval, you don’t need to set dist_km to factor. Then use scale_x_continuous for setting breaks.

library(tidyverse)

ggplot(elevation, aes(x=dist_km, y=cota))+
  geom_point(size=0.1, color="red")+
  ggtitle("Profile Elevation river")+
  xlab("Dist(km)")+
  ylab("Elevation(m)")+
  theme_light()+
  theme(plot.title = element_text(size=13))+
  theme(axis.text.x=element_text(angle=90))+
  scale_x_continuous(breaks=seq(0,555, by=50))

continuous_x_scale

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