R 为什么我的图形的某些美学功能不显示?

R 为什么我的图形的某些美学功能不显示?,r,ggplot2,R,Ggplot2,如何增强数据标签的图形可见性(可能使它们浮动在条形图本身之上),并增加图形和边框之间的空间以容纳数据标签的空间 这是我当前的图表 此外,我很难在图表中添加带有标题的图例。我已经输入了一个代码语句作为我的ggplot2的一部分,如下所示添加图例,但它不起作用。如何为每个数据图例添加不同的颜色,而不是如下图所示的统一的棕色 +scale_fill_discrete(name = "Type of sale", labels = c("New Sale",

如何增强数据标签的图形可见性(可能使它们浮动在条形图本身之上),并增加图形和边框之间的空间以容纳数据标签的空间

这是我当前的图表

此外,我很难在图表中添加带有标题的图例。我已经输入了一个代码语句作为我的ggplot2的一部分,如下所示添加图例,但它不起作用。如何为每个数据图例添加不同的颜色,而不是如下图所示的统一的棕色

 +scale_fill_discrete(name = "Type of sale", labels = c("New Sale", "Resale", "Sub Sale"))
这是我的完整代码:

#Load Libaries

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

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


#Graph construction
ura %>% group_by(Type.of.Sale) %>% summarize(avg_price = mean(Price....)) %>%
  ggplot(aes(x = Type.of.Sale, y = avg_price)) + geom_col(fill = 'brown') +
  labs(x = 'Type of sale', y = 'Average price', title = 'Average price across different types of sales',color='Types of Sales') +
  scale_fill_discrete(name = "Type of sale", labels = c("New Sale", "Resale", "Sub Sale"))+
  geom_text(aes(label = scales::comma(round(avg_price, 2))), vjust= -0.15)+
  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 = comma)
如果数据不够广泛,我还输入了一个google sheet链接:

  • 要获得不同颜色的条,请在
    aes
    中填入
    ,值为
    Type.of.Sale
  • 要使数据标签浮动,请减小
    vjust
  • 增加y轴的限制以创建足够的空间

谢谢!它起作用了
> dput(head(ura,20))

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")
library(dplyr)
library(ggplot2)

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"))+
  geom_text(aes(label = scales::comma(round(avg_price, 2))), vjust= -1.8)+
  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))