Home > database >  Moving labels in ggplot graph so that they match the x-axis tick marks
Moving labels in ggplot graph so that they match the x-axis tick marks

Time:02-14

I am working with a bar graph, and am trying to adjust the labels (location, in my dataset) so that they line up with the x-axis tick marks. The data is below:

data <- data.frame(location=c('Allen',
                              'Allen',
                              'Anderson',
                              'Atchison',
                              'Barber',
                              'Barber',
                              'Bourbon',
                              'Brown',
                              'Brown',
                              'Butler',
                              'Butler',
                              'Butler',
                              'Butler',
                              'Cherokee',
                              'Cherokee',
                              'Cheyenne',
                              'Comanche',
                              'Cowley',
                              'Crawford',
                              'Crawford',
                              'Crawford',
                              'Decatur',
                              'Dickinson',
                              'Dickinson',
                              'Dickinson',
                              'Doniphan',
                              'Douglas',
                              'Douglas',
                              'Douglas',
                              'Douglas',
                              'Edwards',
                              'Edwards',
                              'Ellis',
                              'Ellis',
                              'Finney',
                              'Finney',
                              'Finney',
                              'Ford',
                              'Geary',
                              'Gove',
                              'Graham',
                              'Grant',
                              'Gray',
                              'Harper',
                              'Harper',
                              'Harvey',
                              'Harvey',
                              'Harvey',
                              'Jackson',
                              'Jefferson',
                              'Jefferson',
                              'Jefferson',
                              'Jefferson',
                              'Jewell',
                              'Johnson',
                              'Johnson',
                              'Johnson',
                              'Johnson',
                              'Johnson',
                              'Johnson',
                              'Johnson',
                              'Johnson',
                              'Johnson',
                              'Johnson',
                              'Johnson',
                              'Labette',
                              'Labette',
                              'Labette',
                              'Lane',
                              'Leavenworth',
                              'Leavenworth',
                              'Leavenworth',
                              'Leavenworth',
                              'Leavenworth',
                              'Leavenworth',
                              'Linn',
                              'Lyon',
                              'Marion',
                              'Marion',
                              'Marshall',
                              'Marshall',
                              'Meade',
                              'Miami',
                              'Miami',
                              'Miami',
                              'Miami',
                              'Miami',
                              'Miami',
                              'Montgomery',
                              'Montgomery',
                              'Montgomery',
                              'Montgomery',
                              'Montgomery',
                              'Morris',
                              'Nemaha',
                              'Neosho',
                              'Ness',
                              'Osage',
                              'Osage',
                              'Pottawatomie',
                              'Pottawatomie',
                              'Pottawatomie',
                              'Reno',
                              'Reno',
                              'Reno',
                              'Reno',
                              'Reno',
                              'Reno',
                              'Republic',
                              'Rice',
                              'Riley',
                              'Riley',
                              'Riley',
                              'Rooks',
                              'Rush',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Sedgwick',
                              'Shawnee',
                              'Shawnee',
                              'Shawnee',
                              'Shawnee',
                              'Shawnee',
                              'Shawnee',
                              'Shawnee',
                              'Shawnee',
                              'Shawnee',
                              'Shawnee',
                              'Sheridan',
                              'Smith',
                              'Stanton',
                              'Sumner',
                              'Sumner',
                              'Trego',
                              'Wabaunsee',
                              'Wilson',
                              'Wilson',
                              'Woodson',
                              'Wyandotte',
                              'Wyandotte',
                              'Wyandotte',
                              'Wyandotte',
                              'Wyandotte',
                              'Wyandotte',
                              'Wyandotte',
                              'Wyandotte',
                              'Wyandotte',
                              'Wyandotte',
                              'Wyandotte',
                              'Wyandotte',
                              'Wyandotte',
                              'Wyandotte',
                              'Wyandotte'),
                   vehicles = c(43472,
                                51429,
                                53577,
                                54936,
                                56705,
                                63875,
                                46172,
                                54135,
                                53690,
                                72173,
                                93634,
                                83378,
                                72500,
                                46458,
                                53843,
                                55352,
                                58173,
                                78182,
                                56406,
                                45114,
                                74543,
                                43646,
                                68798,
                                57917,
                                46219,
                                62188,
                                53445,
                                79500,
                                86150,
                                63523,
                                55598,
                                58438,
                                65673,
                                71417,
                                73958,
                                117703,
                                36750,
                                52349,
                                50906,
                                56667,
                                67292,
                                65000,
                                67381,
                                54044,
                                49648,
                                55774,
                                78272,
                                67056,
                                69962,
                                73036,
                                74688,
                                63177,
                                66189,
                                51932,
                                67266,
                                27860,
                                72358,
                                106582,
                                72923,
                                71667,
                                115741,
                                97734,
                                113750,
                                98359,
                                0,
                                49193,
                                48553,
                                45313,
                                64805,
                                76667,
                                90597,
                                95944,
                                88431,
                                74613,
                                75625,
                                54848,
                                74750,
                                65057,
                                63824,
                                54250,
                                55651,
                                61797,
                                90083,
                                96450,
                                91549,
                                79722,
                                74543,
                                66111,
                                55865,
                                43750,
                                46310,
                                62411,
                                58750,
                                52838,
                                62813,
                                60972,
                                63125,
                                64495,
                                54492,
                                82574,
                                68850,
                                57557,
                                41100,
                                70313,
                                43152,
                                62482,
                                59155,
                                47713,
                                46613,
                                66583,
                                42853,
                                65773,
                                0,
                                54531,
                                52750,
                                25455,
                                28889,
                                55682,
                                52926,
                                43646,
                                26857,
                                34224,
                                81167,
                                81917,
                                79556,
                                86826,
                                80326,
                                43333,
                                84325,
                                103333,
                                78279,
                                97940,
                                71085,
                                91146,
                                98008,
                                83750,
                                44554,
                                78065,
                                75500,
                                84615,
                                76004,
                                88480,
                                80392,
                                65472,
                                77957,
                                90848,
                                59342,
                                49821,
                                50703,
                                60147,
                                73897,
                                57017,
                                68966,
                                48750,
                                45924,
                                50568,
                                0,
                                22399,
                                30625,
                                29676,
                                36250,
                                0,
                                0,
                                66806,
                                41714,
                                0,
                                0,
                                106574,
                                110909,
                                73487,
                                97083))

Here is the code that I am using to generate my graph:

library(ggplot2)

ggplot(data = data, aes(x = location, y = vehicles))  
  geom_bar(stat = 'identity', fill = 'steelblue')  
  theme(axis.text.x = element_text(angle = 90))  
  xlab("Location")  
  ylab("Vehicles") 
  ggtitle("Vehicles per location")

As can be seen below, the labels do not match up with the tick marks. I need to shift them slightly to the left. How can I fix this?

enter image description here

CodePudding user response:

If you set hjust and vjust inside theme(axis.text.x = element_text(...)) you can tweak the positions however you like:

library(ggplot2)

ggplot(data = data, aes(x = location, y = vehicles))  
  geom_bar(stat = 'identity', fill = 'steelblue')  
  theme(axis.text.x = element_text(angle = 90, vjust = 0.3, hjust = 1))  
  xlab("Location")  
  ylab("Vehicles") 
  ggtitle("Vehicles per location") 

enter image description here

CodePudding user response:

This is in addition to Allan's sensible answer, but I just wanted to add the perhaps less widely known option that you can set the angle in the axis guide, which automatically adjust hjust/vjust for you. Especially for angles like 45 degrees, it is easier than using the theme.

library(ggplot2)

ggplot(data = data, aes(x = location, y = vehicles))  
  geom_bar(stat = 'identity', fill = 'steelblue')  
  guides(x = guide_axis(angle = 90))  
  xlab("Location")  
  ylab("Vehicles") 
  ggtitle("Vehicles per location")

  • Related