在 ggplot2 中添加 x 和 y 轴标签 [英] adding x and y axis labels in ggplot2
问题描述
请问如何更改此图表上的 x 和 y 标签?
How do I change the x and y labels on this graph please?
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")
推荐答案
[注:编辑以现代化 ggplot 语法]
[Note: edited to modernize ggplot syntax]
您的示例无法重现,因为没有 ex1221new
(Sleuth2
中有一个 ex1221
,所以我想这就是您的意思).此外,您不需要(也不应该)将列拉出以发送到 ggplot
.一个优点是 ggplot
直接与 data.frame
一起使用.
Your example is not reproducible since there is no ex1221new
(there is an ex1221
in Sleuth2
, so I guess that is what you meant). Also, you don't need (and shouldn't) pull columns out to send to ggplot
. One advantage is that ggplot
works with data.frame
s directly.
您可以使用 xlab()
和 ylab()
设置标签,或使其成为 scale_*.*
调用的一部分.
You can set the labels with xlab()
and ylab()
, or make it part of the scale_*.*
call.
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)")
另一种仅指定标签的方法(如果您不更改秤的任何其他方面,则很方便)是使用 labs
函数
An alternate way to specify just labels (handy if you are not changing any other aspects of the scales) is using the labs
function
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.
这篇关于在 ggplot2 中添加 x 和 y 轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!