Aesthetic mapping
bb_aes()
for aesthetic mapping, that equivalents to ggplot2::aes()
.
library(plotbb)
<- bbplot(mtcars, bb_aes(mpg, disp, col=factor(cyl)))
p + bb_grid(col='grey50', lty='dashed') + bb_point(pch=19) p
Geometric layer
<- p + bb_point() + bb_lm(bb_aes(group=cyl), lwd=2)
p2 <- p2 + bb_lm(col="red", lwd=3, lty='dotted')
p3 <- p + bb_text(bb_aes(label=cyl), cex=2)
p4 ## oldpar <- par()$mfrow
## par(mfrow=c(1,3))
## p2; p3; p4
## par(mfrow=oldpar)
::plot_list(p2, p3, p4, ncol=3, tag_levels = 'A') aplot
Heatmap
<- data.frame(x = rep(1:10, 12),
df y = rep(1:12, each = 10),
values = rnorm(120, mean = 10, sd = 5),
type = sample(LETTERS[1:5], 120, replace=TRUE),
stringsAsFactors = FALSE)
<- bbplot(df, bb_aes(x,y, col=values)) + bb_tile() +
h1 bb_title("heatmap for continuous numerical values")
<- bbplot(df, bb_aes(x,y, col=values)) + bb_tile() + bb_scale_col_palette("YlOrRd") +
h2 bb_title("applying a color palette")
<- bbplot(df, bb_aes(x,y, col=type)) + bb_tile() +
h3 bb_title("heatmap for discrete categorical values")
<- bbplot(df, bb_aes(x,y, col=values)) + bb_tile() + bb_text(col='black') +
h4 bb_title("heatmap with text labels") + bb_theme_expand()
::plot_list(h1, h2, h3, h4, ncol=2, tag_levels = 'A') aplot
TODO
- bb_grid
- bb_point
- bb_lm
- bb_text
- bb_tile
- more layers need to be added
Setting labels
+ bb_labs(title = "hello", sub = "just for demo",
p2 xlab="this is xlab", ylab = "this is ylab") +
bb_title("hello world") # last one rules
Theme
<- p2 +
g bb_theme(col.main="red", cex.main=2,
mar = c(4, 4, 3, 1)) +
bb_title("applying graphics::par")
<- p2 + bb_title("theme has no side effect")
g2 ::plot_list(g, g2, ncol=2, tag_levels = 'A') aplot
bb_theme
has no side effect and will only apply to the bbplot
object that it added to. This is very important for developing pre-defined themes.
<- p3 + bb_theme_expand()
p4 ::plot_list(p4, p3, ncol=2, tag_levels = 'A') aplot
+ bb_grid(col='grey50', lty='dashed') +
p bb_point(pch=19) +
bb_theme_expand() +
bb_theme_grey()
+ bb_point(pch=19, cex=2) +
p bb_theme_expand() +
bb_theme_deepblue()
TODO
-
bb_theme_expand
-
bb_theme_grey
-
bb_theme_deepblue
- develop more pre-defined themes
Scale
-
bb_scale_col_palette
- more to be implemented
Legend
Not yet implemented
Using existing code with plotbb
Suppose we have existing code to plot something:
plot(mtcars$mpg, mtcars$disp)
abline(lm(disp ~ mpg, data=mtcars), col='red')
We can wrap the codes into a function:
<- function() {
f plot(mtcars$mpg, mtcars$disp)
abline(lm(disp ~ mpg, data=mtcars), col='red')
}
Then we can convert it to a bbplot
object. The plot produced by the function will be used as the canvas, and we can apply theme and add layers to it:
library(dplyr)
<- group_by(mtcars, cyl) %>%
d summarize(xm=mean(mpg), ym=mean(disp))
<- as.bbplot(f) +
pp bb_theme_expand() +
bb_theme_grey() +
bb_lm(bb_aes(mpg, disp, group=cyl, col=factor(cyl)), data=mtcars, lwd=2, lty='dashed') +
bb_point(bb_aes(xm, ym, col=factor(cyl)), data=d, pch=19, cex=2) +
bb_title("hello plotbb") +
bb_grid(col='grey30', lty='dashed') ## grid lines were plotted as background by default
As there are many features currently not available, plotbb
supports adding layers using base graphics commands that you are already familiar with. Any base graphics commands in formula, expression or function can be served as layer to be added to a bbplot
object.
+ (~points(30, 400, pch=19, col="red", cex=3)) +
pp ~text(30, 420, label="hae fun :)", col="blue", cex=1.2)
Here is another example to use plotbb
with the ape
package to visualize phylogenetic tree.
require(ape)
set.seed(2020-09-10)
= rtree(10)
x
= as.bbplot(
p function()
plot(x, cex=2, edge.width=2,
edge.color="white", tip.color='purple')
)
+ bb_theme_expand() + bb_theme_deepblue() +
p ~nodelabels(node = 14, pie = matrix(rep(1, 4), 1), cex = 2)) +
(~nodelabels(node = 15, pie = matrix(rep(1, 3), 1), cex = 2)) +
(~nodelabels(node = 18, pie = matrix(rep(1, 5), 1), cex = 2)) +
(bb_labs(title = 'phylogenetic tree plotted by base graphics') +
bb_theme(col.main = "firebrick", cex.main=2)