Tutorial by Examples

We plot a simple scatter plot using the builtin iris data set as follows: library(ggplot2) ggplot(iris, aes(x = Petal.Width, y = Petal.Length, color = Species)) + geom_point() This gives:
Display multiple plots in one image with the different facet functions. An advantage of this method is that all axes share the same scale across charts, making it easy to compare them at a glance. We'll use the mpg dataset included in ggplot2. Wrap charts line by line (attempts to create a square l...
ggplot2 works best with a long data frame. The following sample data which represents the prices for sweets on 20 different days, in a format described as wide, because each category has a column. set.seed(47) sweetsWide <- data.frame(date = 1:20, chocolate = run...
Add one common horizontal line for all categorical variables # sample data df <- data.frame(x=('A', 'B'), y = c(3, 4)) p1 <- ggplot(df, aes(x=x, y=y)) + geom_bar(position = "dodge", stat = 'identity') + theme_bw() p1 + geom_hline(aes(yintercept=5), colour=...
ggplot(data = diamonds, aes(x = cut, fill =color)) + geom_bar(stat = "count", position = "dodge") it is possible to obtain an horizontal bar chart simply adding coord_flip() aesthetic to the ggplot object: ggplot(data = diamonds, aes(x = cut, fill =color)) + geom_ba...
Violin plots are kernel density estimates mirrored in the vertical plane. They can be used to visualize several distributions side-by-side, with the mirroring helping to highlight any differences. ggplot(diamonds, aes(cut, price)) + geom_violin() Violin plots are named for their resemblance...
qplot is intended to be similar to base r plot() function, trying to always plot out your data without requiring too much specifications. basic qplot qplot(x = disp, y = mpg, data = mtcars) adding colors qplot(x = disp, y = mpg, colour = cyl,data = mtcars) adding a smoother qplot(x = d...

Page 1 of 1