ggplot2中的迷你图 [英] Sparklines in ggplot2

查看:145
本文介绍了ggplot2中的迷你图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为



我不远处但我坚持最小/最大值和标签的分配:

  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< - me lt(dd,id =Year)
名称(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))

解决方案

作为三组标签和阴影四分位数范围:

 #计算最小值和最大值,which.min返回第一个(像你的例子):
mins < - group_by(d,Crime.Typ e)%>%slice(which.min(Crime.Rate))
maxs < - group_by(d,Crime.Type)%>%slice(which.max(Crime.Rate))
ends< - group_by(d,Crime.Type)%>%过滤器(年==最大(年))
夸脱< - d%>%
group_by(Cr​​ime.Type )%>%
summary(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,max = 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(break = seq(1960, 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.frames可以使事情变得更加简洁,但是多个geom调用在这里似乎是最简单的。


Tufte Sparklines (as illustrated in his Beautiful Evidence) have been replicated in base graphics as part of YaleToolkit and further perfected as a result of this question. Sparklines have also been done in lattice as a part of my small side project Tufte in R (self-promotion not intended). My goal now is to replicate Tufte sparklines in ggplot2. There are some scripts floating around on Gist and also as a reply to this question on SO, but none of those give a solid base for making replicable sets of sparklines.

Now, I would like those multiple sparklines to look like this (it was done in base graphics and the code is available here) - dots stand for maximum/minimum values, number on right end is a final value in specific time series and grey band shows a rough quantiles range:

I'm not far away but I'm stuck with the assignment of minimal/maximum values and labels:

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)) 

解决方案

Here is one approach to getting single colored points, as well as the three sets of labels and shaded quartile ranges:

# 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, max = 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())

I'm assuming you don't want a legend here. You can almost certainly make things more concise by merging some data.frames, but multiple geom calls seem to be easiest here.

这篇关于ggplot2中的迷你图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆