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")
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 .
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.
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.
#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.
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.
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)
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.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With