Search code examples
rggplot2data-visualizationmedian

Plot the Average Value of a Variable using ggplot2


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


Solution

  • 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