1

I have two columns that I would like to plot in a bar chart: "Property_type" and "Price". Using the code below, I will plot the "total price" instead of the "median price" for each property type. Could you help me fix the code?

theme_set(theme_bw())

# Draw plot
ggplot(data, aes(x=Property_type, y=Price)) + 
  geom_bar(stat="identity", width=.5, fill="tomato3") + 
  labs(title="Ordered Bar Chart", 
       subtitle="Average Price by each Property Type", 
       caption="Image: 5") + 
  theme(axis.text.x = element_text(angle=65, vjust=0.6))

Image Here

Tony Flager
  • 95
  • 1
  • 8
  • 1
    Simply aggregate before plotting: `ggplot(aggregate(Price ~ Property_type, data, FUN=median), ...` – Parfait Apr 18 '20 at 19:02

2 Answers2

1

Using dplyr, you can calculate the median price for each property and then pass this new variable as y value in ggplot2:

library(dplyr)
library(ggplot2)

data %>% 
  group_by(Property) %>% 
  summarise(MedPrice = median(Price, na.rm = TRUE)) %>%
  ggplot(aes(x = reorder(Property,-MedPrice), y = MedPrice)) +
  geom_col(fill = "tomato3", width = 0.5)+
  labs(title="Ordered Bar Chart", 
       subtitle="Average Price by each Property Type", 
       caption="Image: 5") + 
  theme(axis.text.x = element_text(angle=65, vjust=0.6))

Does it answer your question ?

If not, please provide a reproducible example of your dataset by following this guide: How to make a great R reproducible example

dc37
  • 15,840
  • 4
  • 15
  • 32
  • Thanks a lot! Just one quick question: I know have the median price, but the bars are not ordered from the bars with the highest median price to do the lowest. Could you please guide me on that? – Tony Flager Apr 18 '20 at 19:33
  • You're welcome ;) I edited my answer to reorder bars according their median price on the plotting. Let me know if it is ok. If not, please provide a reproducible example in your question – dc37 Apr 18 '20 at 19:38
1

While dc37's answer will get you what you want perfectly, I just wanted to point out that you could also use the stat_* family of functions within ggplot to calculate groupwise summary statistics.

library(ggplot2)

df <- data.frame(
  Property = rep(LETTERS[1:10], each = 10),
  Price = rnorm(100, rep(1:10, each = 10))
)

ggplot(df, aes(Property, Price)) +
  stat_summary(fun = median, geom = "col")

Created on 2020-04-18 by the reprex package (v0.3.0)

teunbrand
  • 33,645
  • 4
  • 37
  • 63