Andrea Amantini / Jun 28 2018

R-GGPlot-Cairo-SVGlite

apt-get update
apt-get install -y libcairo2-dev
install.packages('cairo')
install.packages('svglite')
install.packages("ggplot2")
library(ggplot2)
library(svglite)
library(ggplot2)
# create factors with value labels 
mtcars$gear <- factor(mtcars$gear,levels=c(3,4,5),
  	labels=c("3gears","4gears","5gears")) 
mtcars$am <- factor(mtcars$am,levels=c(0,1),
  	labels=c("Automatic","Manual")) 
mtcars$cyl <- factor(mtcars$cyl,levels=c(4,6,8),
   labels=c("4cyl","6cyl","8cyl")) 

ggplot

library(ggplot2)

qp = qplot(mpg, data=mtcars, geom="density", fill=gear, alpha=I(.5), 
   main="Distribution of Gas Milage", xlab="Miles Per Gallon", ylab="Density")

ggsave('/results/qp.svg', plot=qp)

#print(str(ret))
# print(class(ret))
p = last_plot()
print(str(p))
qp <- qplot(wt, mpg, data=mtcars, 
                     geom=c("point", "smooth"), 
   									 method="lm", formula=y~x, color=cyl, 
                     main="Regression of MPG on Weight", 
									   xlab="Weight", ylab="Miles per Gallon")

ggsave('/results/qp', plot=qp, device='png')

other

ggp <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
ggsave('/results/ggp.svg', plot=ggp)
svg("/results/plot.svg", bg='transparent')

par(mfrow=c(2, 2), cex=0.6, mar=c(4, 4, 1, 1))
y <- rnorm(20)
plot(y, type="p")
plot(y, type="l")
plot(y, type="b")
plot(y, type="h")

dev.off()
library(grid)
grid.rect(width = convertWidth(unit(1, "inches"), "npc"))
library(grid)  

# Create a new graph
grid.newpage()

pushViewport(viewport(width=.5, height=.8))

r = grid.rect(gp=gpar(fill="#D8F0D1"))

ret = popViewport()
print(class(r))
df <- data.frame(
   gp = factor(rep(letters[1:3], each = 10)),
   y = rnorm(30)
)
ds <- plyr::ddply(df, "gp", plyr::summarise, mean = mean(y), sd = sd(y))
ggplot(df) +
   geom_point(aes(gp, y)) +
   geom_point(data = ds, aes(gp, mean), colour = 'red', size = 3)
ggsave('/results/plot.svg')
parent.frame
list(~class, ~drv, ~fl)
vp