Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating x label text in ggplot

Tags:

r

ggplot2

The code below should rotate and align the text labels on the x-axis, but for some reason it don't:

ggplot(res, aes(x=TOPIC,y=count), labs(x=NULL)) + 
  scale_y_continuous(limits=c(0,130),expand=c(0,0)) +
  scale_x_discrete("",labels=c("ANA"="Anatomy","BEH"="Behavior","BOUND"="Boundaries",
                           "CC"="Climate change","DIS"="Disease","EVO"="Evolution",
                           "POPSTAT"="Pop status","POPABU"="Pop abundance",
                           "POPTR"="Pop trend","HARV"="Harvest","HAB"="Habitat",
                           "HABP"="Habitat protection","POLL"="Pollution",
                           "ZOO"="Captivity","SHIP"="Shipping","TOUR"="Tourism",
                           "REPEC"="Reprod ecology","PHYS"="Physiology","TEK"="TEK",
                           "HWC"="HWC","PRED"="Predator-prey","METH"="Methods",
                           "POPGEN"="Pop genetics","RESIMP"="Research impact",
                           "ISSUE"="Other","PROT"="Protection","PA"="Protected areas",
                           "PEFF"="Protection efficiency","MINOR"="Minor")) + 
  theme(axis.text.x=element_text(angle=90,hjust=1)) +
  geom_bar(stat='identity') +
  theme_bw(base_size = 16) +
  ggtitle("Peer-reviewed papers per topic")
like image 587
Dag Avatar asked Apr 17 '16 21:04

Dag


People also ask

How do I change X label in ggplot2?

To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .

How do I rotate the X axis labels in Matplotlib?

Rotate X-Axis Tick Labels in Matplotlib There are two ways to go about it - change it on the Figure-level using plt. xticks() or change it on an Axes-level by using tick. set_rotation() individually, or even by using ax.

How do you change the X axis ticks in R?

Option 1. Set xaxt = "n" and yaxt = "n" to remove the tick labels of the plot and add the new labels with the axis function. Note that the at argument sets where to show the tick marks.

How do I rotate the X axis labels in Excel?

#1 right click on the X Axis label, and select Format Axis from the popup menu list. And the Format Axis dialog will open. #2 click Alignment menu in the left of Format Axis dialog. And select Rotate all text 90 from the Text direction drop down list box.

How to rotate x-axis text labels in ggplot2?

We can rotate axis text labels using theme() function in ggplot2. To rotate x-axis text labels, we use “axis.text.x” as argument to theme() function. And we specify “element_text(angle = 90)” to rotate the x-axis text by an angle 90 degree.

How to annotate rotated text label?

To the angle argument, we have to assign the degree of rotation that we want to use (i.e. 90 degree). ggp + # Annotate rotated text label annotate ("text" , x = 2, y = 3 , label = "This Is My Text Label" , angle = 90)

How to set axis labels to a vertical angle in R?

If we want to set our axis labels to a vertical angle, we can use the theme & element_text functions of the ggplot2 package. We simply have to add the last line of the following R code to our example plot: Figure 2: Barchart with 90 Degree Angle.

How to avoid overlapping label texts in ggplot2?

In most cases we can use coord_flip () to switch and and y-axis and make the axis text easy to read. And with g gplot2 version 3.3.0, we can avoid overlapping label texts by moving the labels along y-axis alternatively.


1 Answers

You need to change the order of the layers, otherwise theme_bw will override theme:

ggplot(res, aes(x=TOPIC,y=count), labs(x=NULL)) + 
  scale_y_continuous(limits=c(0,130),expand=c(0,0)) +
  scale_x_discrete("",labels=c("ANA"="Anatomy","BEH"="Behavior","BOUND"="Boundaries",
                           "CC"="Climate change","DIS"="Disease","EVO"="Evolution",
                           "POPSTAT"="Pop status","POPABU"="Pop abundance",
                           "POPTR"="Pop trend","HARV"="Harvest","HAB"="Habitat",
                           "HABP"="Habitat protection","POLL"="Pollution",
                           "ZOO"="Captivity","SHIP"="Shipping","TOUR"="Tourism",
                           "REPEC"="Reprod ecology","PHYS"="Physiology","TEK"="TEK",
                           "HWC"="HWC","PRED"="Predator-prey","METH"="Methods",
                           "POPGEN"="Pop genetics","RESIMP"="Research impact",
                           "ISSUE"="Other","PROT"="Protection","PA"="Protected areas",
                           "PEFF"="Protection efficiency","MINOR"="Minor")) + 
  theme_bw(base_size = 16) +
  theme(axis.text.x=element_text(angle=90,hjust=1)) +
  geom_bar(stat='identity') +
  ggtitle("Peer-reviewed papers per topic")
like image 157
erc Avatar answered Oct 13 '22 21:10

erc