library(ggplot2)head(diamonds)
## carat cut color clarity depth table price x y z## 1 0.23 Ideal E SI2 62 55 326 4.0 4.0 2.4## 2 0.21 Premium E SI1 60 61 326 3.9 3.8 2.3## 3 0.23 Good E VS1 57 65 327 4.0 4.1 2.3## 4 0.29 Premium I VS2 62 58 334 4.2 4.2 2.6## 5 0.31 Good J SI2 63 58 335 4.3 4.3 2.8## 6 0.24 Very Good J VVS2 63 57 336 3.9 4.0 2.5
set.seed(1410)dsmall <- diamonds[sample(nrow(diamonds), 100), ]
# Basic useqplot(carat, price, data = diamonds)
qplot(log(carat), log(price), data = diamonds)
qplot(carat, x * y * z, data = diamonds)
# Color and shapeqplot(carat, price, data = dsmall, colour = color)
qplot(carat, price, data = dsmall, shape = cut)
# Alpha aestheticqplot(carat, price, data = diamonds, alpha = I(1/10))
qplot(carat, price, data = diamonds, alpha = I(1/100))
qplot(carat, price, data = diamonds, alpha = I(1/200))
# Add smootherqplot(carat, price, data = dsmall, geom = c("point", "smooth"))
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
qplot(carat, price, data = diamonds, geom = c("point", "smooth"))
## geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.
smoother
# method = "loess", default for n < 1000qplot(carat, price, data = dsmall, geom = c("point", "smooth"), span = 0.2)
qplot(carat, price, data = dsmall, geom = c("point", "smooth"), span = 1)
# method = "gam", default for n > 1000qplot(carat, price, data = dsmall, geom = c("point", "smooth"), method = "gam", formula = y ~ s(x))
qplot(carat, price, data = dsmall, geom = c("point", "smooth"), method = "gam", formula = y ~ s(x, bs = "cs"))
# method = "lm"qplot(carat, price, data = dsmall, geom = c("point", "smooth"), method = "lm")
library(splines)qplot(carat, price, data = dsmall, geom = c("point", "smooth"), method = "lm", formula = y ~ ns(x,5))
# method = "rlm"library(MASS)qplot(carat, price, data = dsmall, geom = c("point", "smooth"), method = "rlm")
# Basic commandqplot(color, price / carat, data = diamonds, geom = "jitter")
qplot(color, price / carat, data = diamonds, geom = "boxplot")
# More colorful?qplot(color, price / carat, data = diamonds, geom = "jitter", color = color)
qplot(color, price / carat, data = diamonds, geom = "boxplot", fill = color)
# Add semi-transparentqplot(color, price / carat, data = diamonds, geom = "jitter", alpha = I(1 / 5))
qplot(color, price / carat, data = diamonds, geom = "jitter", alpha = I(1 / 50))
qplot(color, price / carat, data = diamonds, geom = "jitter", alpha = I(1 / 200))
# basicqplot(carat, data = diamonds, geom = "histogram")
## stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
qplot(carat, data = diamonds, geom = "density")
# add binwidth & xlimqplot(carat, data = diamonds, geom = "histogram", binwidth = 1, xlim = c(0,3))
qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.1, xlim = c(0,3))
qplot(carat, data = diamonds, geom = "histogram", binwidth = 0.01, xlim = c(0,3))
## Warning: position_stack requires constant width: output may be incorrect
# more colorful?qplot(carat, data = diamonds, geom = "density", colour = color)
qplot(carat, data = diamonds, geom = "histogram", fill = color)
# simple versionqplot(color, data = diamonds, geom = "bar")
# weighted version (by carat)qplot(color, data = diamonds, geom = "bar", weight = carat) + scale_y_continuous("carat")
# more colorful?qplot(color, data = diamonds, geom = "bar", fill = color)
# with lineqplot(date, unemploy / pop, data = economics, geom = "line")
qplot(date, uempmed, data = economics, geom = "line")
# with pathyear <- function(x) as.POSIXlt(x)$year + 1900qplot(unemploy / pop, uempmed, data = economics, geom = c("point", "path"))
qplot(unemploy / pop, uempmed, data = economics, geom = "path", color = year(date)) + scale_size_area()
# facetsqplot(carat, data = diamonds, facets = color ~ ., geom = "histogram", binwidth = 0.1, xlim = c(0, 3))
# with ..density..qplot(carat, ..density.., data = diamonds, facets = color ~ ., geom = "histogram", binwidth = 0.1, xlim = c(0, 3))
qplot( carat, price, data = dsmall, xlab = "Price ($)", ylab = "Weight (carats)", main = "Price-weight relationship")
qplot( carat, price/carat, data = dsmall, ylab = expression(frac(price,carat)), xlab = "Weight (carats)", main="Small diamonds", xlim = c(.2,1))
## Warning: Removed 35 rows containing missing values (geom_point).
qplot(carat, price, data = dsmall, log = "xy")
[trang chủ] [liên hệ] [code]