forked from tra418/CSB-311-Spring-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntro to R - Ch 10.R
126 lines (102 loc) · 3.64 KB
/
Intro to R - Ch 10.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
############################################################
# R script to accompany Intro to R for Business, Chapter 10#
# Written by Troy Adair #
############################################################
# Clear out Console and Enviroment
rm(list=ls(all=TRUE))
cat("\014")
library(here)
## Get subsample from YT_Sample_Validated ready for graphing
# Load the previously referenced data frame in
# "YT_Sample_Validated.RData", copy it to "DF",
# and remove "YT_Sample_Validated" from memory
load(here("Data","YT_Sample_Validated.RData"))
DF <- YT_Sample_Validated
rm(YT_Sample_Validated)
# Reform DF with only the "good" observations
DF<-DF[which(DF$total_amount >0 &
DF$fare_amount <100000 &
DF$fare_amount >= 0 &
DF$trip_distance < 100),]
# Reformat passenger_count as factor and store data frame as DF
DF$passenger_count <- as.factor(DF$passenger_count)
# Select a random subsample of 10,000 rows
set.seed(10)
index <- sample(1:nrow(DF), 10000, replace=FALSE)
# Copy the row numbers for the sample only into DF
DF <- DF[index,]
##################################################
# Install and load ggplot2 package
if (!require("ggplot2")) install.packages("ggplot2")
library(ggplot2)
# Data set "diamonds" comes in ggplot2
data(diamonds)
dim(diamonds)
names(diamonds)
# Using R core plotting capabilities
plot (price ~ carat, data=diamonds)
plot(diamonds$carat, diamonds$price)
boxplot(diamonds$carat)
hist(diamonds$carat, main="Carat Histogram", xlab="Carat")
# Using ggplot2
ggplot(data = diamonds)
ggplot(data = diamonds)+geom_histogram(aes(x=carat))
ggplot(data = diamonds)+geom_density(aes(x=carat),fill='grey50')
ggplot(data=diamonds) +
geom_histogram(binwidth=500, aes(x=diamonds$price)) +
ggtitle("Diamond Price Distribution") +
xlab("Diamond Price U$") +
ylab("Frequency") +
theme_minimal()
ggplot(data=diamonds) +
geom_histogram(binwidth=500, aes(x=diamonds$price)) +
ggtitle("Diamond Price Distribution") +
xlab("Diamond Price U$ - Binwidth 500") +
ylab("Frequency") +
theme_minimal() +
xlim(0,2500)
ggplot(data=diamonds) +
geom_histogram(binwidth=100, aes(x=diamonds$price)) +
ggtitle("Diamond Price Distribution") +
xlab("Diamond Price U$- Binwidth 50") +
ylab("Frequency") +
theme_minimal() +
xlim(0,2500)
ggplot(data=diamonds) +
geom_histogram(binwidth=100, aes(x=diamonds$price)) +
ggtitle("Diamond Price Distribution by Cut") +
xlab("Diamond Price U$") +
ylab("Frequency") +
theme_minimal() +
facet_wrap(~cut)
ls(pattern = '^geom_', env = as.environment('package:ggplot2'))
help(aes)
help(aes_colour_fill_alpha)
help(aes_group_order)
help(aes_linetype_size_shape)
help(aes_position)
# Package devtools will be used to install latest ggplotgui
# from GitHub
if (!require("devtools")) install.packages("devtools")
library(devtools)
# Installing latest ggplotgui p[ackage from GitHub]
if (!require("ggplotgui")) devtools::install_github("gertstulp/ggplotgui")
library(ggplotgui)
# Running ggplot_shiny() with our data
ggplot_shiny(DF)
## This is where I put the code generated by ggplotgui
# You need the following package(s):
library("ggplot2")
# The code below will generate the graph:
graph <- ggplot(DF, aes(x = trip_distance, y = tip_amount, colour = passenger_count)) +
geom_point()+
geom_smooth(se = FALSE, method = 'lm')+
scale_colour_brewer(palette = 'RdBu') +
theme_bw()
graph
# If you want the plot to be interactive,
# you need the following package(s):
library("plotly")
ggplotly(graph)
# If you would like to save your graph, you can use:
ggsave('my_graph.pdf', graph, width = 14, height = 14, units = 'cm')