m = c(2, 4, 6, 8, 10, 12, 100, 20, 18, 16)),
.Names = c("n", "m"),
row.names = c(NA, -10L),
class = "data.frame")
# Confirm data.frame structure
str(dft)
# First plot example with xlim()
ggplot (dft) +
geom_point (aes (x=n, y=m)) +
xlim (c (1,5))
# Second plot example with scale_x_continuous()
ggplot (dft) +
geom_point (aes (x=n, y=m)) +
scale_x_continuous (limits = c(1,4))
##########################
###### end examples ######
##########################
##########################
#### Possible solutions ####
##########################
# Using coord_cartesian() for zooming
# x-axis restricted to 0 and 5 (xlim)
# y-axis restricted to 0 and 10 (ylim)
ggplot (dft) +
geom_point (aes (x=n, y=m)) +
coord_cartesian(xlim = c(0, 5), ylim = c(0,10)) +
ggtitle("Using coord_cartesian as a zooming function\nExample 1")
# Using coord_cartesian() for zooming
# x-axis restricted to 0 and 4 (xlim)
# y-axis restricted to 0 and 8 (ylim)
ggplot (dft) +
geom_point(aes (x = n, y = m)) +
coord_cartesian(xlim = c(0, 4), ylim = c(0, 8)) +
ggtitle('Using coord_cartesian as a zooming function\nExample 2')
# Alternatively, you could also place the data object and primary aesthetics inside of ggplot() and
# fine tune your plot with breaks, labels and a title like so...
ggplot (dft, aes(n, m)) +
theme_bw() +
geom_point() +
coord_cartesian(xlim = c(0, 5.25), ylim = c(0, 10.25)) +
scale_y_continuous(breaks = seq(0,12, by = 1.0)) +
labs(x = 'n', y = 'm') +
ggtitle('Using coord_cartesian as a zooming function\nExample 3')
##########################
##### end pos_solutions ####
##########################
Hope this helps.
Derek