<jupyter_start><jupyter_text>GGPLOT2The ggplot2 package, created by <NAME>, offers a powerful graphics language for creating elegant and complex plots. Its popularity in the R community has exploded in recent years. Originally based on <NAME>'s The Grammar of Graphics, ggplot2 allows you to create graphs that represent both univariate and multivariate numerical and categorical data in a straightforward manner. Grouping can be represented by color, symbol, size, and transparency. The creation of trellis plots (i.e., conditioning) is relatively simple.<jupyter_code>#Creating a blank canva
library(ggplot2)
ggplot()
#Adding title in the graph
ggplot() + labs(title="This is title")
#Storing the plot to the variable
plot <- ggplot()
plot <- plot + labs(title="Blank Canva")
plot
#Labelling axises and adding subtitle
plot <- ggplot()
plot <- plot + labs(title="Grafik Penduduk",subtitle="Bagian-1")
plot <- plot + labs(x="Nama Kabupaten",y="Jumlah Penduduk")
plot
#Using summary function on the graph object
summary(plot)
#Reading the data that will be used to making graph
dataset <- read.csv("https://academy.dqlab.id/dataset/dkikepadatankelurahan2013.csv",sep=",")
dataset
#Mapping x,y and color using "aes" function
plot <- ggplot(data=dataset,aes(x=LUAS.WILAYAH..KM2.,y=KEPADATAN..JIWA.KM2.,color=NAMA.KABUPATEN.KOTA))
summary(plot)
#Displaying the plot
plot
#Creating scatter plot using layer
dataset <- read.csv("https://academy.dqlab.id/dataset/dkikepadatankelurahan2013.csv",sep=",")
plot <- ggplot(data=dataset,aes(x=LUAS.WILAYAH..KM2.,y=KEPADATAN..JIWA.KM2.,color=NAMA.KABUPATEN.KOTA))
plot <- plot + layer(geom="point",stat="identity",position="identity")
plot
#Creating scatter plot using geom_point()
dataset <- read.csv("https://academy.dqlab.id/dataset/dkikepadatankelurahan2013.csv",sep=",")
plot <- ggplot(data=dataset,aes(x=LUAS.WILAYAH..KM2.,y=KEPADATAN..JIWA.KM2.,color=NAMA.KABUPATEN.KOTA))
plot <- plot + geom_point()
plot
#Adding title and labelling
dataset <- read.csv("https://academy.dqlab.id/dataset/dkikepadatankelurahan2013.csv",sep=",")
plot <- ggplot(data=dataset,aes(x=LUAS.WILAYAH..KM2.,y=KEPADATAN..JIWA.KM2.,color=NAMA.KABUPATEN.KOTA))
plot <- plot + theme(plot.title=element_text(hjust=0.5))
plot <- plot + labs(title="Luas Wilayah vs Kepadatan Penduduk",x="Luas Wilayah (km2)",y="Kepadatan Penduduk (km2)",color="Nama Kabupaten/Kota")
plot <- plot + geom_point()
plot
#Creating histogram plot using geom_histogram()
dataset <- read.csv("https://academy.dqlab.id/dataset/dkikepadatankelurahan2013.csv",sep=",")
plot <- ggplot(data=dataset,aes(x=KEPADATAN..JIWA.KM2.))
plot <- plot + geom_histogram(binwidth=10000)
plot
#Adding fill aesthetic on the graph
dataset <- read.csv("https://academy.dqlab.id/dataset/dkikepadatankelurahan2013.csv",sep=",")
plot <- ggplot(data=dataset,aes(x=KEPADATAN..JIWA.KM2.,fill=NAMA.KABUPATEN.KOTA))
plot <- plot + geom_histogram(binwidth=10000)
plot
#Reading the data that will be used to making line graph
dataset <- read.csv("https://academy.dqlab.id/dataset/inflasi.csv",sep=",")
dataset
#Error when plotting
dataset <- read.csv("https://academy.dqlab.id/dataset/inflasi.csv",sep=",")
plot <- ggplot(data=dataset,aes(x=Bulan,y=Inflasi,color=Negara))
plot <- plot + geom_line()
plot

#its mean that the system is confused due to every month on the x axis has 2 observations
#Grouping
dataset <- read.csv("https://academy.dqlab.id/dataset/inflasi.csv",sep=",")
plot <- ggplot(data=dataset,aes(x=Bulan,y=Inflasi,color=Negara,group=Negara))
plot <- plot + geom_line()
plot
#Fixing the order of the month 
dataset$Bulan <- factor(dataset$Bulan,level=c("Jan-2017","Feb-2017","Mar-2017","Apr-2017","May-2017","Jun-2017","Jul-2017","Aug-2017","Sep-2017","Oct-2017"))
dataset$Bulan
#Display the plot
dataset <- read.csv("https://academy.dqlab.id/dataset/inflasi.csv",sep=",")
plot <- ggplot(data=dataset,aes(x=Bulan,y=Inflasi,color=Negara,group=Negara))
plot <- plot + geom_line()
plot <- plot + geom_text(aes(label=Inflasi),hjust=-0.2,vjust=-0.5)
plot
#Creating bar graph using geom_bar
dataset <- read.csv("https://academy.dqlab.id/dataset/datakependudukandki-dqlab.csv",sep=",")
plot <- ggplot(data=dataset,aes(x=NAMA.KABUPATEN.KOTA))
plot <- plot + geom_bar()
plot
#Creating bar graph with y aesthetic and stat identity
dataset <- read.csv("https://academy.dqlab.id/dataset/datakependudukandki-dqlab.csv",sep=",")
plot <- ggplot(data=dataset,aes(x=NAMA.KABUPATEN.KOTA,y=JUMLAH))
plot <- plot + geom_bar(stat="identity")
plot
#Creating bar graph with fill aesthetic and dodge position
dataset <- read.csv("https://academy.dqlab.id/dataset/datakependudukandki-dqlab.csv",sep=",")
plot <- ggplot(data=dataset,aes(x=NAMA.KABUPATEN.KOTA,y=JUMLAH,fill=JENIS.KELAMIN))
plot <- plot + geom_bar(stat="identity",position="dodge")
plot
#Using aggregate function for finding mean
p <- aggregate(x=list(RATA.RATA = dataset$JUMLAH),FUN="mean",by=list(NAMA.KABUPATEN.KOTA=dataset$NAMA.KABUPATEN.KOTA,JENIS.KELAMIN=dataset$JENIS.KELAMIN))
p
#Tidying up the graph
dataset <- read.csv("https://academy.dqlab.id/dataset/datakependudukandki-dqlab.csv",sep=",")
p <- aggregate(x=list(JUMLAH=dataset$JUMLAH),FUN="sum",by=list(NAMA.KABUPATEN.KOTA=dataset$NAMA.KABUPATEN.KOTA,JENIS.KELAMIN=dataset$JENIS.KELAMIN))
plot <- ggplot(data=p,aes(x=NAMA.KABUPATEN.KOTA,y=JUMLAH,fill=JENIS.KELAMIN,label=JUMLAH))
plot <- plot + geom_bar(stat="identity",position="dodge")
plot <- plot + labs(title="JUMLAH PENDUDUK > 35 TAHUN",x="Nama Kabupaten/Kota",y="Jumlah",fill="Jenis Kelamin")
plot <- plot + theme(axis.text.x=element_text(angle=45,vjust=0.5),plot.title=element_text(hjust=0.5))
plot <- plot + geom_text(position=position_dodge(1.2))
plot
#Faceting on scatter plot
dataset <- read.csv("https://academy.dqlab.id/dataset/dkikepadatankelurahan2013.csv",sep=",")
plot <- ggplot(data=dataset,aes(x=LUAS.WILAYAH..KM2.,y=KEPADATAN..JIWA.KM2.,color=NAMA.KABUPATEN.KOTA))
plot <- plot + theme(plot.title=element_text(hjust=0.5))
plot <- plot + labs(title="Luas Wilayah vs Kepadatan Penduduk",x="Luas Wilayah (km2)",y="Kepadatan Penduduk (km2)",color="Nama Kabupaten/Kota")
plot <- plot + geom_point()
plot <- plot + facet_wrap(~NAMA.KABUPATEN.KOTA,ncol=2)
plot
#Faceting on Histogram plot
dataset <- read.csv("https://academy.dqlab.id/dataset/dkikepadatankelurahan2013.csv",sep=",")
plot <- ggplot(data=dataset,aes(x=KEPADATAN..JIWA.KM2.,fill=NAMA.KABUPATEN.KOTA))
plot <- plot + geom_histogram(binwidth=10000)
plot <- plot + facet_wrap(~NAMA.KABUPATEN.KOTA,ncol=2)
plot<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1646266829865361, 'fraction_numerical': 0.01988984088127295, 'mean_word_length': 6.618881118881119, 'pattern_counts': {'":': 0, '<': 70, '<?xml version=': 0, '>': 8, 'https://': 16, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '605036', 'license': 'MIT', 'n_tokens_mistral': 2744, 'n_tokens_neox': 2485, 'n_words': 440, 'path': 'Learning/Data Visualization with R.ipynb', 'repo_name': 'Maulanaaz/R', 'size': '6554'}