Understand how to create plots with ggplot package
Learn about how to use qplot()
and ggplot()
functions
Create your own plots and customise them
Have a basic understanding of very advanced HTML graphics
ggplot2 is a system for decoratively creating graphics
It allows to create beautiful graphics and customize them
There are two main functions in ggplot package
qplot()
for quick plots
ggplot()
for more sophisticated graphics
Install and load ggplots library(ggplot2)
For more information read R Graphics Cookbook
I have created a dataframe which we will use for our plots
Lets make some graphs using this dataframe
We can make basic graphs using the qplot
qplot(Height, Weight, data = Data.fr, geom = "point")
Using
ggplot()
the above is equivalent to
ggplot(data=Data.fr, aes(x=Height, y=Weight)) + geom_point()
Instead of geom_point()
in
ggplot(data=Data.fr, aes(x=Height, y=Weight)) + geom_point()
you can use geom_line()
, geom_area()
, geom_smooth(method = lm)
, or combination etc…
ggplot(data = Data.fr, aes(x = Height, y = Weight)) + geom_line() + geom_smooth(method = lm)
We shall mainly work with ggplot
from now on. You can create histograms
ggplot(data = Data.fr, aes(Error1)) + geom_histogram(binwidth = 5)
Using
qplot()
the above is equivalent to
qplot(Error1,data=Data.fr, geom = "histogram", binwidth = 5)
Customize your plots
ggplot(data = Data.fr, aes(x = Con, y = Weight)) + geom_bar(stat = "identity",
fill = "lightblue")
Create boxplot by factor
ggplot(data = Data.fr, aes(x = Dis, y = Weight, colour = Fac)) + geom_boxplot()
Create plots by factor
ggplot(data = Data.fr) +
geom_point(mapping = aes(x = Height, y = Weight, colour=Fac, shape=Con))
Create very complicated plots by factor
ggplot(data = Data.fr) + geom_point(mapping = aes(x = Height, y = Pressure,
colour = Fac, shape = Con, size = Day))
You can use alpha=
instead of size
qgplot()
Note you are much more accurate using ggplot()
for example see
P1 <- qplot(Height, Weight, data = Data.fr, shape = Con, colour = "red")
P2 <- ggplot(data = Data.fr) + geom_point(mapping = aes(x = Height, y = Weight,
shape = Con), color = "red")
grid.arrange(P1, P2, ncol = 2)
Create plots by factor and add smooth lines
ggplot(data = Data.fr, mapping = aes(x = Height, y = Weight, colour = Fac)) +
geom_point() + stat_smooth(method = loess)
Arrange facets
ggplot(data = Data.fr, mapping = aes(x = Height, y = Weight, colour = Fac)) +
geom_point() + facet_grid(Fac ~ Con) + stat_smooth(method = loess)
Different
ggplot(data = Data.fr, mapping = aes(x = Day, y = Weight, fill = Con)) + geom_area()
Heat maps
library(scales)
ggplot(data = Data.fr) + geom_bar(aes(x = Day, y = Weight, fill = Con), stat = "identity",
position = "fill") + theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
xlab("Day") + ylab("Weight") + scale_y_continuous(labels = percent_format()) +
guides(fill = guide_legend(title = NULL))
library(plotly)
1Interactive plots with library(plotly)
p <- ggplot(data = Data.fr, aes(x = Con, fill = Dis)) + geom_bar(position = "dodge")
ggplotly(p)
library(plotly)
2Interactive plots with library(plotly)
Data.fr$Day <- as.factor(Day)
p <- ggplot(data = Data.fr, aes(x = Day, y = Pressure, colour = Day)) + geom_boxplot()
ggplotly(p)
library(plotly)
Interactive plots with library(plotly)
plot_ly(Data.fr, x = Data.fr$Height, y = Data.fr$Pressure, text = paste("Con: ",
Data.fr$Con), mode = "markers", color = Data.fr$Dis, size = Data.fr$Weight)
## No trace type specified:
## Based on info supplied, a 'scatter' trace seems appropriate.
## Read more about this trace type -> https://plot.ly/r/reference/#scatter
Read more on https://plot.ly/r/
library(rbokeh)
Interactive plots library(rbokeh)
figure() %>% ly_points(Height, Weight, data = Data.fr, color = Con, glyph = Dis,
hover = list(Height, Weight))
library(highcharter)
Interactive plots library(highcharter)
highchart() %>% hc_title(text = "Scatter chart with size and color") %>% hc_add_series_scatter(Data.fr$Height,
Data.fr$Pressure, Data.fr$Weight, Data.fr$Day)
library(ggiraph)
Interactive plots library(ggiraph)
p <- ggplot(data = Data.fr, aes(x = Day, y = Pressure, colour = Day))
p <- p + geom_point_interactive(aes(tooltip = Dis), size = 2)
girafe(code = print(p))
library(ggiraphExtra)
Interactive plots library(ggiraphExtra)
ggPoints(data = Data.fr, mapping = aes(x = Height, y = Weight, colour = Fac),
method = "lm", interactive = TRUE)