adding x and y axis labels in ggplot2

后端 未结 2 1174
执笔经年
执笔经年 2020-11-30 19:38

How do I change the x and y labels on this graph please?

library(Sleuth2)
library(ggplot2)
discharge<-ex1221new$Discharge
area<-ex1221new$Area
nitrogen         


        
2条回答
  •  Happy的楠姐
    2020-11-30 20:21

    since the data ex1221new was not given, so I have created a dummy data and added it to a data frame. Also, the question which was asked has few changes in codes like then ggplot package has deprecated the use of

    "scale_area()" and nows uses scale_size_area()
    "opts()" has changed to theme()
    

    In my answer,I have stored the plot in mygraph variable and then I have used

    mygraph$labels$x="Discharge of materials" #changes x axis title
           mygraph$labels$y="Area Affected" # changes y axis title
    

    And the work is done. Below is the complete answer.

    install.packages("Sleuth2")
    library(Sleuth2)
    library(ggplot2)
    
    ex1221new<-data.frame(Discharge<-c(100:109),Area<-c(120:129),NO3<-seq(2,5,length.out = 10))
    discharge<-ex1221new$Discharge
    area<-ex1221new$Area
    nitrogen<-ex1221new$NO3
    p <- ggplot(ex1221new, aes(discharge, area), main="Point")
    mygraph<-p + geom_point(aes(size= nitrogen)) + 
      scale_size_area() + ggtitle("Weighted Scatterplot of Watershed Area vs. Discharge and Nitrogen Levels (PPM)")+
    theme(
     plot.title =  element_text(color="Blue", size=30, hjust = 0.5), 
    
     # change the styling of both the axis simultaneously from this-
     axis.title = element_text(color = "Green", size = 20, family="Courier",)
     
    
       # you can change the  axis title from the code below
       mygraph$labels$x="Discharge of materials" #changes x axis title
       mygraph$labels$y="Area Affected" # changes y axis title
       mygraph
    
    
    
       
    

    Also, you can change the labels title from the same formula used above -

    mygraph$labels$size= "N2" #size contains the nitrogen level 
    

提交回复
热议问题