2012-05-03 240 views
94

如何更改此圖上的x和y標籤?在ggplot2中添加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") 

回答

148

[注:編輯現代化ggplot語法]

你舉的例子是不可複製的,因爲沒有ex1221new(存在Sleuth2ex1221,所以我想這是你的意思)。此外,您不需要(也不應該)將列拖出發送到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)") 

它給出了一個id真正的數字到上面的一個。

相關問題