How to plot in R

Use of the plot systems available in Nextjournal

Data can be graphically displayed in multiple ways on Nextjournal. The standard built-in R methods can be used, while Plotly and ggplot2 syntax provide the most features on the viewer's side.

1.
R

x <- seq(-10, 10, length= 60); y <- x
f <- function(x, y) { r <- sqrt(x^2+y^2); sin(r)/r }
z <- outer(x, y, f)
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "#009caa"); NULL

Behind the scenes we're using built-in R graphic devices, saving the output to files in the /results directory. Any images saved there will be automatically displayed.

svg('/results/my-plot.png')
hist(rnorm(200, 1), breaks=100)
dev.off()

We've made this easier by defining a default device that will auto-increment the file names and has nice defaults.

deviceFn <- getOption('device')
print(get(deviceFn))
deviceFn

So, you don't have to bother with setting up devices:

curve(sin, from=-pi, to=pi)
abline(a=0, b=1, col='magenta')
abline(h=0, v=0)

cos2 <- function(t){cos(2*t)}
curve(cos2, from=2*pi, to=2*-pi, n=1000)
abline(h=0, v=0)

A Nextjournal cell can show multiple graphs—the runner will detect each new figure automatically and display them in order.

library(colorspace)
attach(iris)

plot(Petal.Length, Petal.Width,
     main="Iris Data",
     col=rainbow_hcl(3)[c(Species)], 
     xlab="Iris petal length",
     ylab="Iris petal width", pch=16)
abline(lm(Petal.Width~Petal.Length), col="#56B8D1")
lines(lowess(Petal.Length,Petal.Width), col="#5ABD93")
legend("topleft", pch=16, col=rainbow_hcl(3), legend=unique(Species))

pairs(~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,
      main="Scatterplot Matrix")

detach(iris)

2.
Plotly

This syntax allows full access to the functionality of Plotly on R.

library(plotly)

plot_ly(data=iris, x=~Petal.Length, y=~Petal.Width,
        type="scatter", mode="markers") %>%
  layout(title="Iris Data",
         xaxis=list(title="Iris petal length"),
         yaxis=list(title="Iris petal width"))

plot_ly(z = ~volcano, type = "surface")

d <- diamonds[sample(nrow(diamonds), 1000), ]
plot_ly(
 d, x = ~carat, y = ~price,
 # Hover text:
 text = ~paste("Price: ", price, '$<br>Cut:', cut),
 color = ~carat, size = ~carat, type='scatter', mode='markers')

x <- rnorm(200)
y <- rnorm(200)
subplot(
 plot_ly(x = x, type = "histogram"),
 plotly_empty(),
 plot_ly(x = x, y = y, type = "histogram2dcontour"),
 plot_ly(y = y, type = "histogram"),
 nrows = 2, heights = c(0.2, 0.8), widths = c(0.8, 0.2), margin = 0,
 shareX = TRUE, shareY = TRUE, titleX = FALSE, titleY = FALSE
) %>% layout(showlegend=FALSE)

Note that the data points can be hovered over to view the data for each, both here and in the published view. Traces can also be toggled on and off by clicking in the legend.

plot_ly(alpha = 0.6) %>%
  add_histogram(x = ~rnorm(500)) %>%
  add_histogram(x = ~rnorm(500) + 1) %>%
  layout(barmode = "overlay")

3.
ggplot2

We also support plotting via the ggplot2 package.

library(ggplot2)

qplot(speed, dist, data=cars) + geom_smooth()

ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point() +
  labs(title="Iris Data", 
         x="Iris petal length", 
         y="Iris petal width")

This generates static plots; however, if the plotly package is loaded then you can convert your ggplots into plotly ones via the ggplotly() function. In this way you can gain some of plotly's interactive functionality.

library(ggplot2)
library(plotly)

ggplotly(qplot(speed, dist, data=cars) + geom_smooth())