简体   繁体   English

在 ggplot2 中添加 x 和 y 轴标签

[英]adding x and y axis labels in ggplot2

How do I change the x and y labels on this graph please?请问如何更改此图上的 x 和 y 标签?

library(Sleuth2)
library(ggplot2)
discharge<-ex1221new$Discharge
area<-ex1221new$Area
nitrogen<-ex1221new$NO3
p <- ggplot(ex1221new, aes(discharge, area), main="Point")
p + geom_point(aes(size= nitrogen)) + 
    scale_area() + 
    opts(title = expression("Weighted Scatterplot of Watershed Area vs. Discharge and Nitrogen Levels (PPM)"), 
         subtitle="n=41")

[Note: edited to modernize ggplot syntax] [注意:编辑以更新 ggplot 语法]

Your example is not reproducible since there is no ex1221new (there is an ex1221 in Sleuth2 , so I guess that is what you meant).您的示例不可重现,因为没有ex1221newex1221中有一个Sleuth2 ,所以我想这就是您的意思)。 Also, you don't need (and shouldn't) pull columns out to send to ggplot .此外,您不需要(也不应该)将列拉出以发送到ggplot One advantage is that ggplot works with data.frame s directly.一个优点是ggplot作品与data.frame小号直接。

You can set the labels with xlab() and ylab() , or make it part of the scale_*.* call.您可以使用xlab()ylab()设置标签,或使其成为scale_*.*调用的一部分。

library("Sleuth2")
library("ggplot2")
ggplot(ex1221, aes(Discharge, Area)) +
  geom_point(aes(size=NO3)) + 
  scale_size_area() + 
  xlab("My x label") +
  ylab("My y label") +
  ggtitle("Weighted Scatterplot of Watershed Area vs. Discharge and Nitrogen Levels (PPM)")

在此处输入图片说明

ggplot(ex1221, aes(Discharge, Area)) +
  geom_point(aes(size=NO3)) + 
  scale_size_area("Nitrogen") + 
  scale_x_continuous("My x label") +
  scale_y_continuous("My y label") +
  ggtitle("Weighted Scatterplot of Watershed Area vs. Discharge and Nitrogen Levels (PPM)")

在此处输入图片说明

An alternate way to specify just labels (handy if you are not changing any other aspects of the scales) is using the labs function仅指定标签的另一种方法(如果您不更改比例的任何其他方面,则很方便)是使用labs函数

ggplot(ex1221, aes(Discharge, Area)) +
  geom_point(aes(size=NO3)) + 
  scale_size_area() + 
  labs(size= "Nitrogen",
       x = "My x label",
       y = "My y label",
       title = "Weighted Scatterplot of Watershed Area vs. Discharge and Nitrogen Levels (PPM)")

which gives an identical figure to the one above.这给出了与上面相同的数字。

since the data ex1221new was not given, so I have created a dummy data and added it to a data frame.由于没有给出数据ex1221new,所以我创建了一个虚拟数据并将其添加到数据框中。 Also, the question which was asked has few changes in codes like then ggplot package has deprecated the use of此外,被问到的问题在代码中几乎没有变化,例如 ggplot 包已弃用

"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 变量中,然后我使用了

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM