使用 ggplot2 生成过滤列的条形图

Produce Bar Chart of Filtered Columns with ggplot2

你能告诉我如何生成如图所示的图表吗? 我只想 select 每个城市的前 2 个街区(基于房价中位数的前 2 个街区)并显示它们的中位数价格。当然,如果酒吧有不同的颜色,那就更好了.. (请注意,我手动生成中位数价格并将其绘制在 Excel 中,因此它们不代表实际值)

    glimpse(CityNeighbourhoodPrice)
Observations: 37,245
Variables: 3
$ City          <fct> Amsterdam, Amsterdam, Amsterdam...
$ Neighbourhood <fct> A,B,C,D,E,F,G,H,I,J,K...
$ Price         <int> 970, 1320, 2060, 2480, 1070, 12...

到目前为止,这是我的代码(不起作用):

CityNeighbourhoodPrice %>% 
  group_by(Neighbourhood) %>%
  count(n) %>%
  top_n(2, MedPrice) %>%
  summarise(MedPrice = median(Price, na.rm = TRUE)) %>%
  ggplot(aes(x = reorder(Neighbourhood,-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))

另一种解决方案可能是:

假设您的数据如下:

library(dplyr)
library(ggplot)

data <- data.frame(Price=c(970, 245, 564, 895, 431, 100), City=c("Amsterdam", "Athens", "Amsterdam", "London", "Berlin", "Netherlands"), Neighborhood=c("A", "B", "D", "C", "E", "F"))

然后你做:

example_plot <- data %>%
  select(Price, City, Neighborhood) %>%
  group_by(City) %>%
  top_n(., 2, wt=Price) %>%
  spread(Neighborhood, Price) %>%
  data.frame %>%
  mutate(., Average=rowMeans(.[,-1], na.rm = TRUE)) %>%
  ggplot(., aes(City, Average, fill=City)) +
  ggtitle(str_wrap(c("Median Price for the Top-2 Neighborhoods in Different Cities:"), 20)) +
  theme_fivethirtyeight() +
  theme(legend.position = "none", plot.title = element_text(size= 22), axis.text = element_text(size=14))+
  geom_bar(stat = "identity") +
  geom_text(aes(x = City, y = Average, label = Average ), colour = "white", size = 11, vjust=1.2)

它给你:

使用一些随机示例数据,试试这个:

# Example data
set.seed(42)

CityNeighbourhoodPrice <- data.frame(
  City = rep(c("Amsterdam", "Berlin", "Edinburgh"), each = 30),
  Neighbourhood = rep(sample(LETTERS[1:5], 30, replace = TRUE), 3),
  Price = 3000 * runif(3 * 30)
)

library(ggplot2)
library(dplyr)
library(forcats)

# Plot
CityNeighbourhoodPrice %>% 
  group_by(City, Neighbourhood) %>%
  summarise(MedPrice = median(Price, na.rm = TRUE)) %>%
  top_n(2, MedPrice) %>%
  ungroup() %>% 
  arrange(City, MedPrice) %>% 
  mutate(City_Neighbourhood = paste0(Neighbourhood, "\n", City),
         City_Neighbourhood = forcats::fct_inorder(City_Neighbourhood)) %>% 
  ggplot(aes(x = City_Neighbourhood, 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))

reprex package (v0.3.0)

于 2020-04-20 创建