rootDir = dirname(getwd()) knitr::opts_knit$set(root.dir = normalizePath(rootDir))
source("R/main.R") library(ggplot2) library(reshape2) dataset = loadDataset(target = FALSE, forceBuild = TRUE) drugs = c("Amphet", "Heroin", "Cannabis", "Cocaine", "Crack", "Nicotine", "Caffeine", "Alcohol") drugs_orig = lapply(drugs, function(d) { paste(d, "_orig", sep="") })
This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
ggplot(dataset, aes(x=Age)) + geom_bar(fill="#4b869c") + geom_text(stat='count', aes(label=..count..), vjust=-0.3) + scale_x_discrete(labels = c('18-24','25-34','35-44', '45-54', '55-65', '65+')) + labs(title = "Age distribution", y="Count")
ggplot(dataset, aes(x=Gender)) + geom_bar(fill=c("#f295ec", "#4edaf6")) + geom_text(stat='count', aes(label=..count..), position = position_stack(vjust = 0.5)) + labs(title = "Gender distribution", y = "Count")
ggplot(dataset, aes(x=Age, fill=Gender)) + scale_fill_manual(values = c("#f295ec", "#4edaf6")) + geom_bar() + geom_text(stat='count', aes(label=..count..), position = position_stack(vjust = 0.5)) + labs(title = "Gender distribution by Age", y = "Count")
ggplot(dataset, aes(x=Education)) + geom_bar(fill="#4b869c") + geom_text(stat='count', aes(label=..count..), position = position_stack(vjust = 0.5)) + scale_x_discrete(labels = c('Left school\n<16Y','Left school\nat 16Y','Left school\nat 17Y', 'Left school\nat 18Y', 'Some uni\nbut no degree', 'Professional\nCertificate', 'Bachelor\nDegree', 'Master\nDegree', 'Doctorate')) + labs(title = "Education Distribution", y = "Count", x = "Education Level")
ggplot(dataset, aes(x=NOfDrugsUsed, fill=Gender)) + geom_bar(fill="#4b869c") + labs(y = "Count", x = "Number of drugs used") + scale_x_continuous(breaks=0:15)
ggplot(dataset, aes(x=as.factor(UsedAnyDrug))) + geom_bar(fill=c("#00c853", "#bf360c")) + geom_text(stat='count', aes(label=..count..), position = position_stack(vjust = 0.5)) + labs(title = "Drug usage", y = "Count", x = "") + scale_x_discrete(labels=c("Never used drugs","Has used drugs"))
Aggiungere anche per ogni droga.
ggplot(dataset) + geom_density(aes(dataset$SensationSeeking, fill=factor(Cocaine)), alpha=0.3)
ggplot(dataset) + geom_density(aes(EScore, fill=Gender), alpha=0.3)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
#dataset.m <- melt(dataset, id=drugs) for(i in 1:length(drugs)) { drug = drugs[i] d_orig = paste(drug,"_orig", sep="") p = ggplot(dataset, aes_string(x = d_orig)) + geom_bar(fill="#4b869c") + labs(title = paste(drug, "usage"), y = "Number of people", x = "") + geom_text(stat='count', aes(label=..count..), position = position_stack(vjust = 0.5)) + scale_x_discrete(labels = c('Never\nUsed', 'A Decade Ago','In Last Decade', 'In Last Year', 'In Last Month', 'In Last Week', 'In Last Day')) + geom_text(stat='count', aes(label=..count..), position = position_stack(vjust = 0.5)) print(p) }
library(ggcorrplot) library(polycor) ds2 = dataset # for (drug in drugs) { # dataset[[drug]] = as.factor(dataset[[paste(drug, "_orig", sep="")]]) # } #vs2 = variables[! variables %in% c("CannabisPrice", "CrackPrice", "CocainePrice", "EcstasyPrice")] corr <- hetcor(as.data.frame(ds2[,(names(ds2) %in% c(variables, drugs))])) ggcorrplot(corr$correlations, outline.col = "white", insig = "pch") + theme(axis.text.x = element_text(size=10, angle=90, vjust=0.5), axis.text.y = element_text(size=10, vjust=0.5)) + labs(title = paste("Correlation Matrix"))
ggcorrplot(corr$correlations, outline.col = "white", insig = "pch", lab=TRUE) + theme(axis.text.x = element_text(size=10, angle=90, vjust=0.5), axis.text.y = element_text(size=10, vjust=0.5)) + labs(title = paste("Correlation Matrix"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.