I'm still fairly new to R, and am having some real issues turning a data frame into a graphical representation. I downloaded some data for house prices in the UK and stripped it down to just the pieces I needed which are;

(example data)

Sale_Price, Date_Of_Sale, Postcode, City
1000000     2022-12-24    AB1 2CD   London

The sale price is an integer and the date of sale is of class 'Date'

I need the X axis to have monthly data (so stripping off the date part, keeping month and year only) The Y axis was supposed to have the averaged sales for that month so mean(df$Sale_Price) for each month.

Finally the fill value was going to have the three postcodes I was interested in... but as they are all mixed in and I'm only interested in specific sectors I wanted to use something like the following;

filter(df, str_detect(Postcode, "^AB1 2"))

postcode is an example, but would pick up anything beginning with AB1 2..... i.e. it would pick up both AB1 2CD and AB1 2XY and everything in between...

I'm really stuck as to how I should approach this problem as I can do small pieces if I break them into other structures, but never all together.

Eventually built into a graph that has three bars per month or if it ends up looking too congested, I'll swap to line graphs.

Any help would be very much appreciated!


1 Answer 1


You can use functions from the dplyr library for this, something like the following should work:

df %>%
    # select postcodes you are interested about
    filter(str_detect(Postcode, "^AB1 2")) %>%
    # create groups for which to calculate the average
    group_by(format(Date_Of_Sale, "%Y-%m"), Postcode) %>%
    # calculate the average of the sale price
    summarise(avg_sale_price = mean(Sale_Price))

This should give you a dataframe that contains the average sale price for each combination of the postcode and the period.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.