问题
I know there are other questions like this. However, this is a bit of two fold item.
So, i am using the following code to arrange my horizontal bar chart in descending order by annual difference, A. First is the sample data and then making the data frame.
A<- c(150,125,0,-300,-350,-370)
Series<- c("Construction","Manufacturing","Information","Health_Care","Education","Government")
testdf <- data.frame(A,Series)
jobgrowth<-ggplot(data=testdf,mapping=aes(x=A,y=Series))+
geom_col(color="blue")+aes(x = reorder(Series,A),A)+ coord_flip()
The issue is that my y-axis label is showing as "reorder(Series,A)". I added the second aes segment as an attempt to have them arranged by descending order (greatest number at the top). PPrior to that, it was arranging them in a random (as far as i can tell) order. How do I get the y-axis label to show nothing? Second, If I wanted the title to be directly above the chart, what is the method for that?
回答1:
No need to call aes
twice, you can reorder
in the call to ggplot
. From there you can use labs
with x = NULL
to get rid of the x-axis lable and ggtitle
to add a title. You can call theme
to move the title to the exact center. If you prefer it centered over the plot area, remove the plot.title.position
line.
ggplot(data=testdf, aes(y=A, x = reorder(Series,A))) +
geom_col(color="blue") + coord_flip() +
labs(x = NULL) + ggtitle("Interesting Title") +
theme(plot.title.position = "plot",
plot.title = element_text(hjust = 0.5))
来源:https://stackoverflow.com/questions/65569149/how-to-modify-axis-labels-ggplot-in-r