具有不同尺度的多线图

时间:2017-04-28 15:55:19

标签: r plot rescale

我想绘制音乐物理特征的时间依赖趋势。

例如,

from 1980 to 2010, the average song duration (unit: 100000 ms)
from 1980 to 2010, the average loudness (unit: 20dB)

您可以看到它们具有不同的比例,随着时间的推移,它们可以直接绘制每个特征的线条图。

但如果我说30个这样的功能,那么将所有这些线图包含在同一个图中是否合适呢? (因为绘制30个独立的线图会有点浪费。)

如果是这样,我怎样才能克服不同缩放的问题(100000ms vs 20dB)?我应该先重新调整数据?

THX

1 个答案:

答案 0 :(得分:1)

这是一个ggplot方法(用假数据说明):

library(reshape2)
library(ggplot2)
library(ggthemes)

# Fake data
dat = mtcars[order(mtcars$mpg), c(1,3:7,11)]
dat = cbind(dat, setNames(dat, LETTERS[1:7]), setNames(dat, LETTERS[8:14]), 
            setNames(dat, LETTERS[15:21]), setNames(dat[,1:2], LETTERS[22:23]), 
            year=1980:(1980 + nrow(dat) - 1))

# Melt data to long format and plot
ggplot(melt(dat, id.var="year"), aes(year, value)) +
  geom_line(lwd=0.3) +
  facet_wrap(~ variable, ncol=5, scales="free_y") +
  theme_tufte(base_size=7) +
  expand_limits(y=0)

enter image description here