반응형

/*******************************************************************************************************************
-- Title : [MSR] RTVS - Import Data from URL and Draw an interactive 3D plot
-- Reference : microsoft.com
-- Key word : microsoft r rtvs url ggplot ggplot2 rcurl geturl read.csv revoscaler rximport xdf quakes 
                  geom_point coord_map scale_colour_gradient ggplotgrob grid.draw plot3d with
*******************************************************************************************************************/

-- Chart



-- Microsoft 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# **************************************************
# -- Import Data from URL to Dataframe
# **************************************************
# This script shows how to import data into R that is referenced by a URL.
 
# -- Install the RCurl package if it's not already installed.
if (!require("RCurl", quietly = TRUE)) 
    install.packages("RCurl")
 
# - Load packages.
library("RCurl", quietly = TRUE)
 
# -- A URL contains the raw data.
github <- "https://raw.githubusercontent.com/brohrer-ms/RTVS-docs/master/examples/MRS_and_Machine_Learning/Datasets/"
inputDataURL <- paste0(github, "Flight_Delays_Sample.csv")
 
# -- Download data from the URL.
inputData <- getURL(inputDataURL)
 
# -- Read downloaded data into a data frame.
df <- read.csv(text = inputData, na.strings = "NA", stringsAsFactors = FALSE)
 
# -- Review the first 6 rows of data.
head(df)
 
 
# **************************************************
# -- Import Data from URL to xdf
# **************************************************
 
# -- Note messages, if it's not installed
if (!require("RevoScaleR")) {
    cat("RevoScaleR package does not seem to exist. 
        \nThis means that the functions starting with 'rx' will not run. 
        \nIf you have Microsoft R Server installed, 
        \nplease switch the R engine.
        \nFor example, in R Tools for Visual Studio: 
        \nR Tools -> Options -> R Engine. 
        \nIf Microsoft R Server is not installed, you can download it from: 
        \nhttps://www.microsoft.com/en-us/server-cloud/products/r-server/
        \n")
 
    quit()
}
 
# -- A URL contains the raw data.
github <- "https://raw.githubusercontent.com/brohrer-ms/RTVS-docs/master/examples/MRS_and_Machine_Learning/Datasets/"
inputDataURL <- paste0(github, "Flight_Delays_Sample.csv")
 
# -- Create a temporary directory to store the intermediate .xdf files.
tempdir()                         # "C:\\Users\\Hayden\\AppData\\Local\\Temp\\Rtmpkb2mcH"
td <- tempdir()
 
# -- Read a downloaded .csv file into a RxXdfData object.
outFile <- paste0(td, "/data.xdf")
df_xdf <- rxImport(inData = inputDataURL, outFile = outFile,
                   missingValueString = "M", stringsAsFactors = FALSE)
 
# Review the first 6 rows of data.
head(df_xdf)
 
 
# **************************************************
# -- Draw it using ggplot2
# **************************************************
 
# -- Read the help page for more information
? quakes
 
# -- Inspect the structure of the data (a data frame with 5 columns)
str(quakes)
head(quakes)
 
# -- install and load the packages
suppressWarnings(if (!require("ggplot2")) install.packages("ggplot2"))
library("ggplot2")
 
suppressWarnings(if (!require("rgl")) install.packages("rgl"))
library("rgl")
 
suppressWarnings(if (!require("mapproj")) install.packages("mapproj"))
library("mapproj")
 
# The package "gtable" allows you to work with objects called grob tables.
# A grob table captures all the information needed to layout grobs in a table
# structure. It supports row and column spanning, offers some tools to
# automatically figure out the correct dimensions, and makes it easy to align
# and combine multiple tables. 
suppressWarnings(if (!require("gtable")) install.packages("gtable"))
library(gtable)
 
# Plot longitude and latitude of quakes.
# To create a plot, you have to specify the data, then map aesthetics to 
# columns in your data. In this example, you map the column long to the x-axis
# and lat to the y-axis.
# Then you add a layer with points (geom_point) and a layer to plot maps.
p0 <- ggplot(quakes, aes(x = long, y = lat)) +
    geom_point() +
    coord_map()
p0
 
# You can use a number of different aesthetics, for example colour or size
# of the points.
 
# Map the depth column to the colour aesthetic.
p1 <- ggplot(quakes, aes(x = long, y = lat)) +
    geom_point(aes(colour = depth)) +
    coord_map()
p1
 
# Add size for magnitude. The bigger the magnitude, the larger the point.
p2 <- ggplot(quakes, aes(x = long, y = lat)) +
    geom_point(aes(colour = depth, size = mag)) +
    coord_map()
p2
 
# You can control the transparancy of a plot object with the alpha aesthetic.
# High values of alpha (close to 1) are opaque, while low values (close to 0)
# are translucent.
# Add alpha level to hide overplotting, thus revealing detail.
p3 <- ggplot(quakes, aes(x = long, y = lat)) +
    geom_point(aes(colour = depth, size = mag), alpha = 0.25+
    coord_map()
p3
 
# Change colour gradient by adding a gradient scale.
p4 <- ggplot(quakes, aes(x = long, y = lat)) +
    geom_point(aes(colour = depth, size = mag), alpha = 0.25+
    coord_map() +
    scale_colour_gradient(low = "blue", high = "red")
p4
 
# Add a plot title
p5 <- ggplot(quakes, aes(x = long, y = lat)) +
    geom_point(aes(colour = depth, size = mag), alpha = 0.25+
    scale_colour_gradient(low = "blue", high = "red"+
    ggtitle("Distribution of earthquakes near Fiji"+
    coord_map()
p5
 
# Now plot multiple plots on the same graphic
# The package "grid" is built into R and allows you to take control of the 
# plotting area. A grob is the abbreviation for "graphical object", and the 
# function ggplotGrob() in ggplot2 converts a ggplot2 object into a grob.
# You can then use the grid functions to combine your ggplot objects.
theme_set(theme_grey(12+ theme(legend.key.size = unit(0.5"lines")))
 
library(grid)
plot.new()
grid.draw(cbind(
    ggplotGrob(p1),
    ggplotGrob(p2),
    ggplotGrob(p3),
    size = "last"
    ))
    
plonglat <- ggplot(quakes, aes(x = long, y = lat, size = mag, col = depth)) +
    geom_point(alpha = 0.5+
    ggtitle("Top view")
 
plongdep <- ggplot(quakes, aes(x = long, y = -depth, size = mag, col = depth)) +
    geom_point(alpha = 0.5+
    ggtitle("Side view")
 
platdep <- ggplot(quakes, aes(x = depth, y = lat, size = mag, col = depth)) +
    geom_point(alpha = 0.5+
    ggtitle("Side view")
 
# Next, define a gtable and add grobs to the table.
gt <- gtable(widths = unit(rep(12), "null"),
             heights = unit(rep(12), "null"))
gt <- gtable_add_grob(gt,
                      grobs = list(
                        ggplotGrob(plonglat),
                        ggplotGrob(platdep),
                        ggplotGrob(plongdep)
                      ),
                      l = c(121), # left extent of the grobs
                      t = c(112# top extent of the grobs
)
plot.new()
grid.draw(gt)
 
# Finally, you can plot the data in an interactive 3D plot, using package rgl.
# Note this will open a seperate window with your plot.
# This window is interactive - you can click and drag in the window,
# thus changing the orientation of the data in 3 dimensions.
with(quakes, plot3d(lat, long, - depth, col = mag))

cs

반응형

+ Recent posts