how to modify axis labels ggplot in R

柔情痞子 提交于 2021-01-29 06:06:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!