반응형
/*******************************************************************************************************************
-- Title : [MSR] RTVS - Import the dataset in VisualStudio and draw ggplot
-- Reference : microsoft.com
-- Key word : microsoft r rtvs weather_sample setkey as.data.frame data.frame ggplot ggplot2 aes geom_point
geom_smooth scale_x_continuous scale_y_continuous geom_text visual studio
*******************************************************************************************************************/
-- How to import the dataset in Visualstudio
-- 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 | # ------------------------------ # -- Preparing the Dataset # ------------------------------ install.packages("data.table") library(data.table) # -- read csv file # You can import the dataset in "Visual Studio>R Tools>Data>Import Data Into R Session From Textfile..." csv_file <- read.csv(file = "C:\\RProject\\RTVS\\Datasets\\Weather_Sample.csv" , header = TRUE, row.names = NULL, encoding = "UTF-8", sep = ",", dec = ".", quote = "\"", comment.char = "") head(csv_file) class(csv_file) # -- Allocate dataframe Weather_Sample <- data.table(Weather_Sample) class(Weather_Sample) # "data.table" "data.frame" head(Weather_Sample) # -- Grouping on a specific column setkey(Weather_Sample, AirportID) # ------------------------------ # -- Average Wind Speed by Airport ID # ------------------------------ # -- 전체 테이블에 대한 mean()값 avg_windspeed_by_ID0 <- as.data.frame(Weather_Sample[, mean(WindSpeed, na.rm = TRUE)]) # -- AirportID 그룹별 mean()값 avg_windspeed_by_ID <- as.data.frame(Weather_Sample[, mean(WindSpeed, na.rm = TRUE), by = AirportID]) class(avg_windspeed_by_ID) # "data.frame" head(avg_windspeed_by_ID) # ------------------------------ # -- Average Wind Speed by Airport ID (Plot) # ------------------------------ chart_by_ID <- as.data.frame(Weather_Sample[, mean(WindSpeed, na.rm = TRUE), by = AirportID]) ggplot(chart_by_ID, aes(x = AirportID, y = V1)) + geom_point(stat = "identity") + geom_smooth(method = "lm", formula = y ~ splines::bs(x, 3)) + scale_x_continuous(name = "Airport ID") + scale_y_continuous(name = "Average Wind Speed") + geom_text(aes(label = AirportID), size = 3, vjust = 1.0) + geom_text(aes(label = round(V1, digits = 2)), size = 3, vjust = 2.0) # • ggplot() : This is the plotting function. # • chart_by_ID, : Feeds the data.frame of Weather_Sample into this variable # • aes(x = AirportID, y = V1)) : aes is the aesthetic definition for ggplot2. # It is saying that the x axis(horizontal) shows AirportID and that the y axis(vertical) # shows V1, which is going to the average wind speed, in this instance. # • geom_point(stat = "identity") : Lets you define how you want the points on the plot defined. # In this case, we want them shown as an identity. # • geom_smooth(method = "lm", formula = y ~ splines::bs(x, 3)) : Lets us add a conditional # average to the plot. # • scale_x_continuous(name = "Airport ID") : Lets us continuously scale the x axis. # • scale_y_continuous(name = "Wind Speed") : Lets us continuously scale the y axis. # • geom_text(aes(label = AirportID), size = 3, vjust = 1.0) : Lets us label and offset # the x - axis label. # • geom_text(aes(label = round(V1, digits = 2)), size = 3, vjust = 1.0) : Lets us label and # offset the y - axis label. # ------------------------------ # -- Average Temperature by Airport ID(°F) # ------------------------------ avg_temperature_by_ID <- as.data.frame(Weather_Sample[, mean(DryBulbFarenheit, na.rm = TRUE), by = AirportID]) head(avg_temperature_by_ID) ggplot(chart_by_ID, aes(x = AirportID, y = V1)) + geom_point(stat = "identity") + geom_smooth(method = "lm", formula = y ~ splines::bs(x, 3)) + scale_x_continuous(name = "AirportID") + scale_y_continuous(name = "Average Temperature") + geom_text(aes(label = AirportID), size = 3, vjust = 1.0) + geom_text(aes(label = round(V1, digits = 2)), size = 3, vjust = 2.0) | cs |
-- File
반응형