在ggplot2中添加x和y轴标签 [英] adding x and y axis labels in ggplot2

查看:3354
本文介绍了在ggplot2中添加x和y轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  library(Sleuth2)
library

如何更改此图表上的x和y标签? (ggplot2)
discharge <-ex1221new $放电
area <-ex1221new $ Area
氮<-ex1221new $ NO3
p < - ggplot(ex1221new,aes(放电,区域) ,main =Point)
p + geom_point(aes(size = nitrogen))+
scale_area()+
opts(title = expression(Weighted Scatterplot of Watershed Area versus Discharge and氮水平(PPM)),
subtitle =n = 41)


解决方案

[注意:编辑以更新ggplot语法]

由于没有 ex1221new (在 Sleuth2 中有一个 ex1221 ,所以我想这就是你的意思)。此外,您不需要(也不应该)将列拖出来发送到 ggplot 。一个优点是 ggplot 直接与 data.frame s协同工作。



您可以使用 xlab() ylab()来设置标签,或将其设置为 scale _ *。* call。



$ p $ library(Sleuth2)
library(ggplot2)
ggplot(ex1221,aes(Discharge,Area))+
geom_point(aes(size = NO3))+
scale_size_area()+
xlab(我的x标签)+
ylab(我的y标签)+
ggtitle(流域面积与排放和氮水平(PPM)的加权散点图)

  ggplot(ex1221,aes(Discharge,Area))+ 
geom_point(aes(size (My x label)+
scale_y_continuous(我的y标签)+
ggtitle(加权)=
scale_size_area(氮)+
scale_x_continuous流域面积与排放和氮含量(PPM)的散点图)


另一种指定标签的方式(如果不改变秤的其他方面,很方便)使用 labs function

  ggplot(ex1221,aes(Discharge,Area))+ 
geom_point(aes(size = NO3))+
scale_size_area()+
labs(size =Nitrogen,
x =My x label,
y =我的y标签,
title =流域面积与流量和氮水平(PPM)的加权散点图)

给出与上面相同的数字。


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

解决方案

[Note: edited to modernize ggplot syntax]

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.frames directly.

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆