0 votes
1 view
in R Programming by (5.2k points)

I plot the following:

library(ggplot2)    

carrots <- data.frame(length = rnorm(500000, 10000, 10000))

cukes <- data.frame(length = rnorm(50000, 10000, 20000))

carrots$veg <- 'carrot'

cukes$veg <- 'cuke'

vegLengths <- rbind(carrots, cukes)

ggplot(vegLengths, aes(length, fill = veg)) +

 geom_density(alpha = 0.2)

Now say I only want to plot the region between x = -5000 to 5000, instead of the entire range.

How can I do that?

1 Answer

+1 vote
by (25.3k points)
edited by
 
Best answer

To change the limits for the axes in ggplot2 graphs, you can add the following functions to your graphs:

To change limits for the x-axis

xlim(-5000, 5000)   

To change the limits for the y-axis

ylim(-5000,5000)

You can also use the scale_x_continuous () and scale_y_continuous() as follows:

scale_x_continuous(limits =c(-5000,5000))

scale_y_continuous(limits = c(-5000,5000))

Both of these methods will remove all the data points outside the given range of limits.

To adjust the visible  area without removing the points, use the following function:

coord_cartesian(xlim = c(-5000, 5000)) 

If you want to learn more about R programming watch this tutorial on Introduction to Data Science with R 

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...