0

The data frame below is called median.price

postcode medianprice
1       NG1      1127.0
2       NG2       900.0
3       NG3       975.0
4       NG4       750.0
5       NG5       650.0
6       NG6       650.0
7       NG7      1027.0
8       NG8       695.0
9       NG9       762.5
10     NG10       650.0
11     NG11       795.0
12     NG12      1197.5
13     NG14       775.0
14     NG15       575.0
15     NG16       600.0

This was achieved by:

postcode <- c('NG1', 'NG2', 'NG3', 'NG4','NG5', 'NG6', 'NG7','NG8', 'NG9', 'NG10','NG11', 'NG12','NG14','NG15','NG16')

medianprice <- c(median(NG1$Price), median(NG2$Price), median(NG3$Price), 
median(NG4$Price),median(NG5$Price), median(NG6$Price), median(NG7$Price), 
median(NG8$Price), median(NG9$Price), median(NG10$Price), 
median(NG11$Price), median(NG12$Price), median(NG14$Price), 
median(NG15$Price), median(NG16$Price))

median.price <- data.frame(postcode, medianprice)

mpbarplot <- (table(median.price))

barplot(median.price)

returns " 'height' must be a vector or a matrix"

and

barplot(mpbarplot) 

returns as a stacked frequency bar chart.

2 Answers 2

1

@StupidWolf's answer should be the answer validated but just to post the alternative using ggplot2.

It will use a dataframe as input for making the plot. So, you can use your data directly (actually data provided by @StupidWolf):

library(ggplot2)
ggplot(median.price, aes(x = postcode, y = medianprice))+
  geom_bar(stat = "identity")+
  scale_x_discrete(limits = as.character(median.price$postcode))

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for validating my answer ;). But to my opinion StupidWolf's answer was the best one as he solved your issue using barplot, I just proposed an alternative to barplot. An upvote will have be enough. But thanks so and glad you get satisfied
1

barplot() takes in a vector, while you have median.price as a data.frame. So you need to provide the specific column to plot, and the names column to barplot, see below:

your data:

median.price=
structure(list(postcode = structure(c(1L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("NG1", "NG10", 
"NG11", "NG12", "NG14", "NG15", "NG16", "NG2", "NG3", "NG4", 
"NG5", "NG6", "NG7", "NG8", "NG9"), class = "factor"), medianprice = c(1127, 
900, 975, 750, 650, 650, 1027, 695, 762.5, 650, 795, 1197.5, 
775, 575, 600)), class = "data.frame", row.names = c("1", "2", 
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", 
"15"))

do:

with(median.price,barplot(medianprice,names.arg=postcode,las=2,cex.axis=0.7))

enter image description here

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.