gpt4 book ai didi

r - 在ggplot2中添加x和y轴标签

转载 作者:行者123 更新时间:2023-12-03 04:39:29 24 4
gpt4 key购买 nike

如何更改此图表上的 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")

最佳答案

[注意:编辑以使 ggplot 语法现代化]

您的示例不可重现,因为没有 ex1221new (Sleuth2 中有一个 ex1221,所以我猜这就是您的意思)。此外,您不需要(也不应该)拉出列来发送到 ggplot。一个优点是 ggplot 直接与 data.frame 配合使用。

您可以使用 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)")

enter image description here

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)")

enter image description here

仅指定标签的另一种方法(如果您不更改比例的任何其他方面,则很方便)是使用 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)")

这给出了与上面相同的数字。

关于r - 在ggplot2中添加x和y轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10438752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com