r - ggplot2中的迷你图

标签 r ggplot2 data-visualization tufte

Tufte迷你图(如他的《美丽的证据》所示)已作为YaleToolkit的一部分复制到基本图形中,并由于this question而进一步完善。作为我的小型项目Tufte in R(不打算自我推广)的一部分,在晶格中也完成了迷你图。我现在的目标是在 ggplot2 中复制Tufte迷你图。有一些脚本floating around on Gist,也可以作为对this question on SO的答复,但是没有一个脚本为制作可复制的迷你图集提供了坚实的基础。

现在,我希望这些多条迷你图看起来像这样(在基本图形和code is available here中完成)-点代表最大值/最小值,右端的数字是特定时间序列中的最终值,灰色带表示粗糙分位数范围:

enter image description here

我并不遥远,但我对最小值/最大值和标签的分配感到困惑:

library(ggplot2)
library(ggthemes)
library(dplyr)
library(reshape)
library(RCurl)
dd <- read.csv(text =
  getURL("https://gist.githubusercontent.com/GeekOnAcid/da022affd36310c96cd4/raw/9c2ac2b033979fcf14a8d9b2e3e390a4bcc6f0e3/us_nr_of_crimes_1960_2014.csv"))
d <- melt(dd, id="Year")
names(d) <- c("Year","Crime.Type","Crime.Rate")
dd <- group_by(d, Crime.Type) %>% 
  mutate(color = (min(Crime.Rate) == Crime.Rate | max(Crime.Rate) == Crime.Rate))
ggplot(dd, aes(x=Year, y=Crime.Rate)) + 
  facet_grid(Crime.Type ~ ., scales = "free_y") + 
  geom_line(size=0.3) + geom_point(aes(color = color)) + 
  scale_color_manual(values = c(NA, "red"), guide=F) +
  theme_tufte(base_size = 15) + 
  theme(axis.title=element_blank(), 
        axis.text.y = element_blank(), axis.ticks = element_blank()) +
  theme(strip.text.y = element_text(angle = 0, vjust=0.2, hjust=0)) 

enter image description here

最佳答案

这是获取单个彩色点以及三组标签和四分位数阴影的一种方法:

# Calculate the min and max values, which.min returns the first (like your example):
mins <- group_by(d, Crime.Type) %>% slice(which.min(Crime.Rate))
maxs <- group_by(d, Crime.Type) %>% slice(which.max(Crime.Rate))
ends <- group_by(d, Crime.Type) %>% filter(Year == max(Year))
quarts <- d %>%
  group_by(Crime.Type) %>%
  summarize(quart1 = quantile(Crime.Rate, 0.25),
            quart2 = quantile(Crime.Rate, 0.75)) %>%
  right_join(d)

ggplot(d, aes(x=Year, y=Crime.Rate)) + 
  facet_grid(Crime.Type ~ ., scales = "free_y") + 
  geom_ribbon(data = quarts, aes(ymin = quart1, ymax = quart2), fill = 'grey90') +
  geom_line(size=0.3) +
  geom_point(data = mins, col = 'blue') +
  geom_text(data = mins, aes(label = Crime.Rate), vjust = -1) +
  geom_point(data = maxs, col = 'red') +
  geom_text(data = maxs, aes(label = Crime.Rate), vjust = 2) +
  geom_text(data = ends, aes(label = Crime.Rate), hjust = 0) +
  geom_text(data = ends, aes(label = Crime.Type), hjust = 0, nudge_x = 5) +
  expand_limits(x = max(d$Year) + (0.25 * (max(d$Year) - min(d$Year)))) +
  scale_x_continuous(breaks = seq(1960, 2010, 10)) +
  scale_y_continuous(expand = c(0.1, 0)) +
  theme_tufte(base_size = 15) +
  theme(axis.title=element_blank(),
        axis.text.y = element_blank(), 
        axis.ticks = element_blank(),
        strip.text = element_blank())

我假设您在这里不想要传说。您几乎可以肯定地说,通过合并一些data.frame可以使事情更简洁,但是在这里似乎最简单的方法是进行多个geom调用。

enter image description here

关于r - ggplot2中的迷你图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35434760/

相关文章:

R dplyr : Non-Standard Evaluation difficulty. 想在过滤器和变异中使用动态变量名

r - 如何在 RSM (R) 中填充轮廓颜色和写入轴名称

r - 使用 geom_segment 按因素分组数据

python - Pandas Seaborn Swarmplot 不绘图

r - tapply 函数提示 args 的长度不相等,但它们似乎匹配

python - 使用 R、Python 或 EXCEL 查找风速最高和最低的一天

r - Facet_Wrap 标题与 ggplotly 中的 y 轴重叠?

r - ggplot2 stat_sum : Label points with percentage

python - 绘制具有不同阵列长度的 3D Pandas 数据帧的光谱数据

r - 将 "rgb"图例添加到 R 传单热图