Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
692 views
in Technique[技术] by (71.8m points)

R plotly: How do I find the top 3 and put it in ascending order?

I have a URA dataset that I wanted to use for plotting graphs to discover insights to recommend investors (those who wish to buy properties) like on what type of properties should they buy, at what district should they buy where there is a trend of increasing price appreciation over the years.

My historical data is based on the year 2016 Q1 to 2020 Q2. How can I do a breakdown of my current graph below?

enter image description here

This graph describes the average price of the different types of apartments price appreciation from 2016 Q1 to 2020 Q4.

As can be seen, resale properties appreciate the most.

Hence, I am having difficulty in writing the code to do a plotly bar chart for doing a breakdown of the resale data to find out the top 2 districts sorted out in increasing trend of price appreciation (meaning increasing price difference) over the years 2017 to 2019.

An example reference graph is provided below to explain the part for increasing trend of price appreciation where the difference of price between one district from year 2019 to year 2017 has a bigger difference than the year after it (That's why it was sorted in ascending order).

enter image description here

This is my dput data()

structure(list(Project.Name = c("V ON SHENTON", "V ON SHENTON", 
"STIRLING RESIDENCES", "PARC CLEMATIS", "STIRLING RESIDENCES", 
"ONE PEARL BANK", "TWIN VEW", "WHISTLER GRAND", "WHISTLER GRAND", 
"WHISTLER GRAND", "WHISTLER GRAND", "WHISTLER GRAND", "KENT RIDGE HILL RESIDENCES", 
"KENT RIDGE HILL RESIDENCES", "KENT RIDGE HILL RESIDENCES", "KENT RIDGE HILL RESIDENCES", 
"KENT RIDGE HILL RESIDENCES", "KENT RIDGE HILL RESIDENCES", "KENT RIDGE HILL RESIDENCES", 
"STIRLING RESIDENCES"), Street.Name = c("SHENTON WAY", "SHENTON WAY", 
"STIRLING ROAD", "JALAN LEMPENG", "STIRLING ROAD", "PEARL BANK", 
"WEST COAST VALE", "WEST COAST VALE", "WEST COAST VALE", "WEST COAST VALE", 
"WEST COAST VALE", "WEST COAST VALE", "SOUTH BUONA VISTA ROAD", 
"SOUTH BUONA VISTA ROAD", "SOUTH BUONA VISTA ROAD", "SOUTH BUONA VISTA ROAD", 
"SOUTH BUONA VISTA ROAD", "SOUTH BUONA VISTA ROAD", "SOUTH BUONA VISTA ROAD", 
"STIRLING ROAD"), Type = c("Apartment", "Apartment", "Apartment", 
"Apartment", "Apartment", "Apartment", "Apartment", "Apartment", 
"Apartment", "Apartment", "Apartment", "Apartment", "Apartment", 
"Apartment", "Apartment", "Apartment", "Apartment", "Apartment", 
"Apartment", "Apartment"), Postal.District = c(1L, 1L, 3L, 5L, 
3L, 3L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L
), Market.Segment = c("CCR", "CCR", "RCR", "OCR", "RCR", "RCR", 
"OCR", "OCR", "OCR", "OCR", "OCR", "OCR", "RCR", "RCR", "RCR", 
"RCR", "RCR", "RCR", "RCR", "RCR"), Tenure = c("99 yrs lease commencing from 2011", 
"99 yrs lease commencing from 2011", "99 yrs lease commencing from 2017", 
"99 yrs lease commencing from 2019", "99 yrs lease commencing from 2017", 
"99 yrs lease commencing from 2019", "99 yrs lease commencing from 2017", 
"99 yrs lease commencing from 2018", "99 yrs lease commencing from 2018", 
"99 yrs lease commencing from 2018", "99 yrs lease commencing from 2018", 
"99 yrs lease commencing from 2018", "99 yrs lease commencing from 2018", 
"99 yrs lease commencing from 2018", "99 yrs lease commencing from 2018", 
"99 yrs lease commencing from 2018", "99 yrs lease commencing from 2018", 
"99 yrs lease commencing from 2018", "99 yrs lease commencing from 2018", 
"99 yrs lease commencing from 2017"), Type.of.Sale = c("Resale", 
"Resale", "New Sale", "New Sale", "New Sale", "New Sale", "New Sale", 
"New Sale", "New Sale", "New Sale", "New Sale", "New Sale", "New Sale", 
"New Sale", "New Sale", "New Sale", "New Sale", "New Sale", "New Sale", 
"New Sale"), No..of.Units = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Price.... = c(3548000L, 
3490000L, 1987000L, 1745000L, 1227000L, 1702000L, 1899000L, 704380L, 
1129960L, 1145540L, 1473540L, 1421880L, 1367000L, 1360000L, 3000000L, 
870000L, 1711000L, 899000L, 870000L, 1249000L), Nett.Price.... = c("-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-"), Area..Sqft. = c(1518L, 1518L, 
1055L, 1044L, 635L, 700L, 1249L, 441L, 764L, 764L, 990L, 958L, 
775L, 786L, 1776L, 484L, 958L, 484L, 484L, 635L), Type.of.Area = c("Strata", 
"Strata", "Strata", "Strata", "Strata", "Strata", "Strata", "Strata", 
"Strata", "Strata", "Strata", "Strata", "Strata", "Strata", "Strata", 
"Strata", "Strata", "Strata", "Strata", "Strata"), Floor.Level = c("46 to 50", 
"46 to 50", "26 to 30", "06 to 10", "31 to 35", "21 to 25", "26 to 30", 
"21 to 25", "21 to 25", "21 to 25", "31 to 35", "31 to 35", "01 to 05", 
"01 to 05", "01 to 05", "01 to 05", "01 to 05", "01 to 05", "01 to 05", 
"16 to 20"), Unit.Price...psf. = c(2338L, 2299L, 1884L, 1671L, 
1932L, 2433L, 1521L, 1596L, 1479L, 1499L, 1488L, 1484L, 1764L, 
1731L, 1689L, 1796L, 1786L, 1856L, 1796L, 1967L), Date.of.Sale = c("20-Jun", 
"20-Jun", "20-Jun", "20-Jun", "20-Jun", "20-Jun", "20-Jun", "20-Jun", 
"20-Jun", "20-Jun", "20-Jun", "20-Jun", "20-Jun", "20-Jun", "20-Jun", 
"20-Jun", "20-Jun", "20-Jun", "20-Jun", "20-Jun")), row.names = c(NA, 
20L), class = "data.frame")

If the dput data is not extensive enough, here my google sheet link for the data: https://docs.google.com/spreadsheets/d/1QFXNUpgEjjPGdfXUwvzYIadnoxcD2-Ba6cw6BqrxfO8/edit

These are some of the libraries I load, as well as the read csv.

library(ggplot2)
library(dplyr)
options(scipen = 100000)
library(scales)
library(stringr)
library(plotly)
library(tidyverse)

#Read dataset
ura <- read.csv('URAdata_new.csv') 

#Data cleaning
ura <- ura %>% mutate(Date.of.Sale_month = str_split(ura$Date.of.Sale, '-', simplify = T)[, 1],
               Date.of.Sale_year = str_split(ura$Date.of.Sale, '-', simplify = T)[, 2])

I need this graph, but I don't know how to plot it.

question from:https://stackoverflow.com/questions/65868862/r-plotly-how-do-i-find-the-top-3-and-put-it-in-ascending-order

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...