Reputation: 95
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))
Upvotes: 1
Views: 2354
Reputation: 16178
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
Upvotes: 1
Reputation: 37953
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)
Upvotes: 1