Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)
edited by

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

1 Answer

0 votes
by
edited by

To add x-axis and y-axis labels in ggplot2, you can use the following methods:

Using xlab and ylab Functions:

library("Sleuth2")

library("ggplot2")

ggplot(ex1221, aes(Density, Discharge)) +

  geom_point(aes(size=NO3)) + 

  scale_size_area() + 

  xlab("X-label") +

  ylab("Y-label") +

  ggtitle("Weighted Scatterplot of Density (people/km^

2) vs. Discharge and Nitrogen Levels (PPM)")

Using scale_x_continuous and scale_y_continuous:

ggplot(ex1221, aes(Density, Discharge)) +

  geom_point(aes(size=NO3)) + 

  scale_size_area() + 

  scale_x_continuous("X-label") +

  scale_y_continuous("Y-label") +

  ggtitle("Weighted Scatterplot of Density (people/km^

2) vs. Discharge and Nitrogen Levels (PPM)")

Using labs Function:

ggplot(ex1221, aes(Density, Discharge)) +

  geom_point(aes(size=NO3)) + 

  scale_size_area() + 

  labs(size= "Nitrogen",

       x = "X-label",

       y = "Y-label",

       title = "Weighted Scatterplot of Density (people/km^

2) vs. Discharge and Nitrogen Levels (PPM)")

Output:

image

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...