ggplot2:basic complete example
Data:diamonds
library(ggplot2) qplot(x, y = NULL, ..., data, facets = NULL, margins = FALSE, geom = "auto", xlim = c(NA, NA), ylim = c(NA, NA), log = "", main = NULL, xlab = deparse(substitute(x)), ylab = deparse(substitute(y)), asp = NA, stat = NULL, position = NULL) qplot( carat, price, data = diamonds) qplot(carat, price, data = dsmall, geom = c("point", "smooth"), span = 0.2) ##曲线的波动 qplot( carat, price, data = diamonds, geom = c("point", "smooth"),alpha = I(1/10)) qplot( carat, price, data = diamonds, geom ="point",colour= I(alpha("red", 0.2))) ##注意I()的用法 geom="jitter" geom="boxplot" geom="histogram" geom="density" geom="smooth" geom="point" geom="bar" geom="line" geom="path" geom="blank"
geom="area"
geom="tile"
geom="path"
变换
qplot(log(carat), log(price), data = diamonds) qplot(carat, price, data = diamonds, log = "xy") qplot( carat, price/carat, data = diamonds, ylab = expression(frac(price,carat)), ##expression的用法 xlab = "Weight (carats)", main="Small diamonds", xlim = c(.2,1)##这里可以.2 ) qplot(carat, ..density.., data = diamonds, facets = color ~ ., geom = "histogram", binwidth = 0.1, xlim = c(0, 3)) qplot(carat, data = diamonds, geom = "density", colour = color) qplot(carat, data = diamonds, geom = "histogram", fill = color) qplot(color, data = diamonds, geom = "bar", weight = carat) + scale_y_continuous("carat") qplot(carat, price, data=diamonds, colour= I("red")) + ggtitle("legend_box")+theme(plot.title = element_text(hjust = 0.5))#调主题位置 ggsave("plot.png", width = 5, height = 5)