Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

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

r - How do I correct an error in not being able to generate my ggplot2 graph?

When I wanted to generate my graph, this error message comes up.

summarise() regrouping output by 'Date.of.Sale_year' (override with .groups argument)

Error: Faceting variables must have at least one value Run rlang::last_error() to see where the error occurred.

This is my code for generating the bar graph.

ura %>% 
  filter(Date.of.Sale_year %in% c(16, 17) & Postal.District %in% c(2, 5)) %>% 
  group_by(Date.of.Sale_year, Postal.District) %>% 
  summarize(avg_price = mean(Price....)) %>%
  ggplot(aes(x = as.character(Postal.District), y = avg_price, 
             fill = as.factor(Date.of.Sale_year))) + 
  geom_col() + 
  facet_grid(~ as.factor(Date.of.Sale_year)) +
  labs(x = 'Postal District', y = 'Average price', 
       title = 'Comparison of unit prices in district 2 and 5 between 2016 and 2017') +
  scale_fill_discrete(name = "Year of sale", labels = c("2016", "2017")) +
  geom_text(aes(label=round(avg_price, 2)), vjust= -0.5)

Some of the libraries I have loaded.

library(ggplot2)
library(dplyr)

#For Year and months column
library(stringr)

#Avoid Scientific Notation
options(scipen = 100000)

#read data
ura <- read.csv('URAdata_new.csv') 

#New Columns to capture month and year individually

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

Due to the large rows of my dataset over 50,000 rows, I have instead shared the document in a google excel sheet link to access it.

https://docs.google.com/spreadsheets/d/1QFXNUpgEjjPGdfXUwvzYIadnoxcD2-Ba6cw6BqrxfO8/edit?usp=sharing

Mainly, I would like to know why the error is happening and what changes can be made to the code so that the graph can be generated.


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

1 Answer

0 votes
by (71.8m points)

The error of interest is the facet_grid. You've asked for a grid, but only provided a single variable. Try facet_wrap, or add another variable to wrap by.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...