首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使我的绘图显示更美观

使我的绘图显示更美观
EN

Stack Overflow用户
提问于 2021-01-24 09:51:34
回答 1查看 51关注 0票数 2

我想在我的绘图图表中改进我的值的显示。当我将鼠标悬停在图表上时,出现了一个问题,它以一种不受欢迎的方式显示特定图表的信息。

这可以使用我的图表的附加图像来解释。

从图表中可以看出,它以以下格式显示我的信息:

Type.of.Sale:经销,avg_price: 2166504,Type.of.Sale:经销

如何将其信息显示更改为所有三种颜色图表的这种格式:

销售类型:转售,均价: 2,166,504,销售类型:转售

我有理由相信,我现在的显示是由于我第一次加载数据时的默认列名,因此我想将原始数据列名从这个"Type.of.Sale“重新格式化为销售类型。

我已经附加了我的代码,我可以在哪里对它进行更改。

代码语言:javascript
复制
#Load Libaries

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])

#Plotly Graph in issue
Plot1<-ggplotly(ura %>% 
  group_by(Type.of.Sale) %>% 
  summarize(avg_price = mean(Price....)) %>%
  ggplot(aes(x = Type.of.Sale, y = avg_price, fill = Type.of.Sale)) + 
  geom_col() +
  labs(x = 'Type of sale', 
       y = 'Average price', 
       title = 'Average price across different types of sales') +
  scale_fill_discrete(name = "Type of sale", 
                      labels = c("New Sale", "Resale", "Sub Sale"))+
  theme(plot.title = element_text(colour="red",size=14, face="bold.italic",hjust = 0.5),
    axis.title.x = element_text(colour="blue",size=14, face="bold"),
    axis.title.y = element_text(colour="green",size=14, face="bold")) + 
    scale_y_continuous(name="Average Price",labels = scales::comma, limits = c(0, 2500000)))
  
Plot1

dput(head(ura,20))

代码语言:javascript
复制
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")

完整数据谷歌表格链接:https://docs.google.com/spreadsheets/d/1QFXNUpgEjjPGdfXUwvzYIadnoxcD2-Ba6cw6BqrxfO8/edit

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-24 10:12:24

由于您的数据具有非标准的列名,因此将空格替换为点(.)。您可以在使用read.csv的同时使用check.names = FALSE来纠正这一点。

但是,我认为更好的方法是在aes中使用text,并构建一个您希望在悬停时显示的字符串。

代码语言:javascript
复制
library(dplyr)
library(ggplot2)
library(plotly)
library(scales)


#Plotly Graph in issue
Plot1<-  ura %>% 
              group_by(Type.of.Sale) %>% 
              summarize(avg_price = mean(Price....)) %>%
              ggplot(aes(x = Type.of.Sale, y = avg_price, fill = Type.of.Sale, 
              text = sprintf('Type of Sale: %s<br>Avg Price: %s<br> Type of Sale: %s', Type.of.Sale, comma(avg_price), Type.of.Sale))) + 
              geom_col() +
              labs(x = 'Type of sale', 
                   y = 'Average price', 
                   title = 'Average price across different types of sales') +
              scale_fill_discrete(name = "Type of sale", 
                                      labels = c("New Sale", "Resale", "Sub Sale"))+
              theme(plot.title = element_text(colour="red",size=14, face="bold.italic",hjust = 0.5),
                    axis.title.x = element_text(colour="blue",size=14, face="bold"),
                    axis.title.y = element_text(colour="green",size=14, face="bold")) + 
              scale_y_continuous(name="Average Price",labels = scales::comma, limits = c(0, 2500000))

ggplotly(Plot1, tooltip = 'text')

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65866436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档