我想在条形图中绘制两列:“ Property_type”和“ Price”。使用下面的代码,我将为每种房地产类型绘制“总价”而不是“中位价”。您能帮我修复代码吗?
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))
使用dplyr
,您可以计算每个属性的中位数价格,然后将该新变量作为y值传递给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))
它能回答您的问题吗?
如果不是,请按照以下指南提供数据集的可重现示例:如何制作出色的R可重现示例
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句