knitr::opts_chunk$set(
  #collapse = TRUE,
  comment = "#,>",
  echo=TRUE
)
require(quickchartR)
require(ggplot2)
library(ggplot2)

Idea

We would like to show differences and similarities between a syntax of quickchartR and a popular R library - Ggplot2.

Pie chart {.tabset .tabset-fade .tabset-pills}

data <- iris
head(data)

mean_setosa_sepal_length<-mean(data[data$Species=="setosa",1])
mean_virginica_sepal_length<-mean(data[data$Species=="virginica",1])
mean_versicolor_sepal_length<-mean(data[data$Species=="versicolor",1])
mean_vector<-c(mean_setosa_sepal_length,mean_virginica_sepal_length,mean_versicolor_sepal_length)
mean_vector

QuickchartR

inputDataPie<-data.frame(table(mtcars$cyl),
  myLabelColumn = rep(c("number od cylinders"),3))
inputDataPie
browseURL(
  quickchartR(
    types = "pie",            # type of a chart
    inputData = inputDataPie, # name of dataset (e.g. dataframe)
    xData = cyl,              # X-axis data
    yData = n,                # Y-axis data
    labels = myLabelColumn    #labels
  )
)

Our result:

Ggplot2

inputDataPie<-data.frame(table(mtcars$cyl))
inputDataPie
ggplot(data=inputDataPie, aes(y=Freq,x='', fill=Var1))+geom_bar(width = 1,stat='identity', color = "white")+coord_polar('y')+
  theme_classic()+
    theme(plot.title = element_text(hjust=0.5),
          axis.line = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank())+
  geom_text(aes(label = Freq),
            position = position_stack(vjust = 0.5))

Doughnut chart {.tabset .tabset-fade .tabset-pills}

A doughnut chart requires the same data.

QuickchartR

myOptions = list(
  title = list(                        # list of title's options
    display = TRUE,                    # visibility
    text = "My first quickchart",      #text
    fontSize = 32 
  ),
  legend = list(position = "bottom"))  #possible values: botton, top, left, right, false
browseURL(
  quickchartR(
    types = "doughnut",
    inputData = inputDataPie,
    xData = cyl,
    yData = n,
    labels = myLabelColumn,
    options = myOptions,
    base64 = F
  )
)

Ggplot

inputDataPie$fraction = inputDataPie$Freq / sum(inputDataPie$Freq)
inputDataPie = inputDataPie[order(inputDataPie$fraction), ]
inputDataPie$ymax = cumsum(inputDataPie$fraction)
inputDataPie$ymin = c(0, head(inputDataPie$ymax, n=-1))



ggplot(data=inputDataPie, aes(fill=Var1, ymax=ymax, ymin=ymin, xmax=4, xmin=3))+geom_rect()+coord_polar('y')+
  theme_classic()+
    theme(plot.title = element_text(hjust=0.5),
          axis.line = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank())+xlim(c(0, 4))+
  geom_label(aes(label=Var1,x=3.5,y=(ymin+ymax)/2),inherit.aes = TRUE, show.legend = FALSE)

Bar chart {.tabset .tabset-fade .tabset-pills}

data <- iris
head(data)
mean_setosa_sepal_length<-mean(data[data$Species=="setosa",1])
mean_virginica_sepal_length<-mean(data[data$Species=="virginica",1])
mean_versicolor_sepal_length<-mean(data[data$Species=="versicolor",1])
mean_vector<-c(mean_setosa_sepal_length,mean_virginica_sepal_length,mean_versicolor_sepal_length)
mean_vector

QuickchartR

inputData2 = data.frame(
  myXColumn =c("setosa","virginica","versicolor"),
  myYColumn = mean_vector,
  myLabelColumn = rep("Iris", 3)
)



myOptionsb = list(
  title = list(                       
    display = TRUE,               
    text = "Mean of sepal length for different types of iris", 
    fontSize = 32 
  ))

browseURL(
  quickchartR(
    types = "bar",
    inputData = inputData2,
    xData = myXColumn,
    yData = myYColumn,
    labels = myLabelColumn,
    options=myOptionsb
  )
)

Ggplot2

inputData2 = data.frame(
  myXColumn =c("setosa","virginica","versicolor"),
  myYColumn = mean_vector
)

ggplot(data=inputData2, aes(y=myYColumn,x=myXColumn, fill=myXColumn))+geom_bar(width = 1,stat='identity', color = "white")+
  theme_classic()+
    theme(plot.title = element_text(hjust=0.5),
          axis.line = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank())+
  geom_text(aes(label = myYColumn),
            position = position_stack(vjust = 0.5))

Radar chart {.tabset .tabset-fade .tabset-pills}

Note: There are some issues regarding using radarchart in Google Chrome.

For setosa species we will show all mean dimensions for petals and sepals.

Please note that a radarchart could be misleading if you use incomparable types of data (for example a horse power for an engine and dimensions of a car.)

dt<-data.frame("setosa",t(colSums(data[data$Species=='setosa',1:4]))/length(data[which(data$Species=="setosa"),1]))

QuickchartR

inputData3 = data.frame(
  myXColumn =c("Sepal Length","Sepal width","Petal Length","Petal width"),
  myYColumn = as.vector(t(dt[,-1])),
  # = c(1,2,3,4),
  myLabelColumn = c(rep("setosa", 4))
)

myOptionsr = list(
  title = list(                       
    display = TRUE,               
    text = "Means of sepal and petal dimensions of iris", 
    fontSize = 32 
  ),
  legend = list(position = "bottom"))

detailedOptions = list(list(fill='true'))
browseURL(
  quickchartR(
    types = "radar",
    inputData = inputData3,
    xData = myXColumn,
    yData = myYColumn,
    #rData = myRColumn,
    labels = myLabelColumn,
    options=myOptionsr,
    detailedOptions=detailedOptions,
    colors = c("blue","red"),
    base64 = F
  )
)

Ggplot2

There is no implementation of radar charts in Ggplot2.

Scatter plot {.tabset .tabset-fade .tabset-pills}

We can also create a basic scatter plot:

QuickchartR

inputDataScatter = data.frame(
  myYColumn =mtcars$hp,
  myXColumn = mtcars$qsec,
  myLabelColumn = rep(c(" "),length(mtcars[,1]))
)

myOptionss = list(
  title = list(                       
    display = TRUE,               
    text = "Horse power versus 1/4 mile time", 
    fontSize = 32 
  ))
browseURL(
  quickchartR(
    types = "scatter",
    inputData = inputDataScatter,
    xData = myXColumn,
    yData = myYColumn,
    rData = myRColumn,
    labels = myLabelColumn,
    options=myOptionss,
    base64 = T
  )
)

Ggplot2

inputDataScatter = data.frame(
  myYColumn =mtcars$hp,
  myXColumn = mtcars$qsec

)
ggplot(data=inputDataScatter, aes(x=myYColumn,y=myXColumn))+geom_point()+theme_classic()+labs(x='', y="")

Bubble plot{.tabset .tabset-fade .tabset-pills}

QuickchartR

inputDataBubble = data.frame(
  myYColumn =mtcars$hp,
  myXColumn = mtcars$qsec,
  myRColumn = mtcars$mpg,
  myLabelColumn = rep(c(" "),length(mtcars[,1]))
)

myOptionsbb = list(
  title = list(                       
    display = TRUE,               
    text = "Horse power versus 1/4 mile time versus mile per gallon distance", 
    fontSize = 22 
  ))
browseURL(
  quickchartR(
    types = "bubble",
    inputData = inputDataBubble,
    xData = myXColumn,
    yData = myYColumn,
    rData = myRColumn,
    labels = myLabelColumn,
    options = myOptionsbb,
    base64=T
  )
)

Ggplot2

inputDataBubble = data.frame(
  myYColumn =mtcars$hp,
  myXColumn = mtcars$qsec,
  myRColumn = mtcars$mpg
)
ggplot(data=inputDataBubble, aes(x=myYColumn,y=myXColumn, size=myRColumn))+geom_point(alpha=0.5)+theme_classic()+labs(x='', y="")

Mixed charts

QuickchartR allows also to present different charts in one space, called a mixed chart. Data are grouped by their labels and a types argument becomes a vector of strings.

QuickchartR

types = c("bar", "line", "bar")
detailedOptions = list(list(), list(fill = "false"), list())
inputDataMix = data.frame(
  myXColumn = rep(1:6, 3),
  myYColumn = sample(1:18, 18),
  myLabelColumn = rep(letters[1:3], 6)
)
browseURL(
  quickchartR(
    types = types,
    inputData = inputDataMix,
    xData = myXColumn,
    yData = myYColumn,
    labels = myLabelColumn,
    detailedOptions = detailedOptions,
    base64 = F
  )
)

Ggplot2

inputDataMixBar = inputDataMix[which(inputDataMix$myLabelColumn %in% c('a','c')),]
inputDataMixBar$dod<-c(rep(1,6),rep(2,6))
inputDataMixBar$myXColumn<-rep(1:6, 2)
g1<-ggplot(data=inputDataMix[inputDataMix$myLabelColumn=='b',])+geom_point( aes(x=1:6,y=myYColumn),alpha=0.5)+theme_classic()#+geom_bar(stat='identity', alpha=0.3)


g1+geom_bar(data=inputDataMixBar,aes(x=myXColumn,y=myYColumn, fill=as.factor(dod)),alpha=0.5,position = position_dodge2(width = 0.9, preserve = "single"), stat = "identity")+theme_classic()+labs(x='', y="")


LuPaRaMySa/quickchartR documentation built on Jan. 23, 2020, 3:49 a.m.