Другим решением может быть:
скажем, ваши данные выглядят следующим образом:
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)
И это дает вам: