![]() ![]() This means that instead of simply skipping these values (like most graphing engines would do), Excel reads them as zeros ‘0’, and in return inputs false data into the graphed picture. Scale_colour_gradient fills the data points with color based on the variable, “apr_price”, please note that the function used here is different from the one used in task 1.Why are the missing data points such a big issue? Microsoft Excel looks at these empty data points as data and not missing values. Geom_point adds the data points of estate price to the plot. ![]() xlim(xlim,xlim) and ylim(ylim,ylim) fix the x- and y-axis of the plot and make them the same as the map_bg. Step-by-step explanation: annotation_custom(rasterGrob(hk_map_bg, width=unit(1,"npc"), height=unit(1,"npc")), -Inf, Inf, -Inf, Inf) xlim(xlim,xlim) # x-axis Mapping ylim(ylim,ylim) # y-axis MappingĪnnotation_custom is used to import the prepared map background image to the plot. # 2D Plot library(ggplot2) library(grid) estate_price = ggplot(estate_df) annotation_custom(rasterGrob(hk_map_bg, width=unit(1,"npc"), height=unit(1,"npc")), -Inf, Inf, -Inf, Inf) xlim(xlim,xlim) # x-axis Mapping ylim(ylim,ylim) # y-axis Mapping geom_point(aes(x=Longitude, y=Latitude, color=apr_price),size=2) scale_colour_gradient(name = '成交呎價(實)\n(HKD)', limits=range(estate_df$apr_price), low="#FCB9B2", high="#B23A48") theme(axis.line=element_blank(), =element_blank(), =element_blank(), =element_blank(), =element_blank(), axis.ticks=element_blank(), panel.background = element_blank()) # Clean Everything estate_price ![]() Task 2: Transform the 2D plot into a 3D plot with rayshaderĭata of real_estate_master_df.csv are scraped from Hong Kong Property. Lastly, ggsave is used to export the map_bg with the right scale derived from the plot coordinates. Output: map_bg.png # Save as PNG xlim = ggplot_build(map_bg)$layout$panel_scales_x]$range$range ylim = ggplot_build(map_bg)$layout$panel_scales_y]$range$range ggsave(‘map_bg.png’, width = diff(xlim)*40, height = diff(ylim)*40, units = “cm”) Then, layer gives a borderline to each region which is the white line shown. Regions with darker colour means that they have a higher population. On the line, scale_fill_gradient, the polygons shown in the map are filled by colour based on population data. In the script shown above, there are 2 main things done. Ggplot2 is another wonderful package in R providing many kinds of charts like bar chart, pie chart, heat map … you name it. library(ggplot2) # Map map_bg = ggplot(map_data, aes(long, lat, group=group, fill = Population)) geom_polygon() # Shape scale_fill_gradient(limits=range(map_data$Population), low="#FFF3B0", high="#E09F3E") layer(geom="path", stat="identity", position="identity", mapping=aes(x=long, y=lat, group=group, color=I('#FFFFFF'))) Please download the rds file from the Github repo first. To plot a map of any regions, you need to have the location data on hand. Step-by-step explanation: hkmap = readRDS("HKG_adm1.rds") # geo data of HK map Task 1: Generate a 2D map plot with ggplot2 library(sp) hkmap = readRDS("HKG_adm1.rds") # geo data of HK map # Preprocessing map_data = ame(id=hkmap$ID_1, Code=hkmap$HASC_1, Eng_name=hkmap$NAME_1) map_data$Code = gsub('HK.', '', as.character(map_data$Code)) map_data = merge(map_data, district_name, by = 'Eng_name') hkmapdf = fortify(hkmap) map_data = merge(hkmapdf, map_data, by="id") map_data = merge(map_data, population, by = "Chi_name") map_data$Population = as.numeric(map_data$Population) library(ggplot2) # Map map_bg = ggplot(map_data, aes(long, lat, group=group, fill = Population)) geom_polygon() # Shape scale_fill_gradient(limits=range(map_data$Population), low="#FFF3B0", high="#E09F3E") layer(geom="path", stat="identity", position="identity", mapping=aes(x=long, y=lat, group=group, color=I('#FFFFFF'))) theme(legend.position = "none", axis.line=element_blank(), =element_blank(), =element_blank(), =element_blank(), =element_blank(), axis.ticks=element_blank(), panel.background = element_blank()) # Clean Everything map_bg # Save as PNG xlim = ggplot_build(map_bg)$layout$panel_scales_x]$range$range ylim = ggplot_build(map_bg)$layout$panel_scales_y]$range$range ggsave('map_bg1.png', width = diff(xlim)*100, height = diff(ylim)*100, units = "cm") ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |