在ggplot2下的一个图表中的条形图和线图 [英] bar and line plot in one chart with a legend under ggplot2

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

问题描述

我想在同一个图表中添加一个条形图和一个与图例相关的两个独立但相关的系列(条形图是季度增长,线图是年增长)。

我现在用宽格式的data.frame和代码来做这件事:

  p <  -  ggplot()+ 
geom_bar(df,aes(x = Date,y = quarterly),color ='blue')+
geom_line(df,aes(x = Date,y = annual ),color ='red')

但我无法解决如何添加图例,标有年度增长的红线;和一个被称为季度增长的蓝色方块。



另外,我无法弄清楚如何为具有长形式data.frame的不同系列使用不同的geoms。



更新:

下面的示例代码让我成为解决方案的一部分,但带有一个非常难看的复制图例。仍然在寻找一个完整的解决方案......这种方法基于将数据放在长表单中,然后绘制数据的子集... ...

  library(ggplot2)
library(reshape)
library(plyr)
library(scales)

### ---制作假数据设置
x< - rep(as.Date('2012-01-01'),24)+(1:24)* 30
ybar< - 1:24
yline< ; - ybar + 1

df < - data.frame(x = x,ybar = ybar,yline = yline)
melt < - melt(df,id.vars =' x',measure.vars = c('ybar','yline'))
melt $ line< - ifelse(molten $ variable =='yline',TRUE,FALSE)
melt $ bar < - ifelse(熔化的$ variable =='ybar',TRUE,FALSE)

### ---子集数据集
df.line< - subset(熔化, line == TRUE)
df.bar< - subset(molten,bar == TRUE)

### ---绘制它
p < - ggplot() +
geom_bar(data = df.bar,mapping = aes(x = x,y = value,fill = variable,color = variable),
stat ='identity',position ='dodge )+
geom_line(data = df.line,mapping = aes(x = x,y = value,color = variable))+

opts(title =Test Plot .position =right)

ggsave(p,width = 5,height = 3,filename ='plot.png',dpi = 150)


还有一个例子...



解决方案

code> subset 参数给geoms。

 > x = 1:10; df = data.frame(x = x,y = x + 1,z = x + 2)
> ggplot(fusion(df),
aes(x,value,color = variable,fill = variable))+
geom_bar(subset =。(variable ==y),stat =identity )+
geom_line(subset =。(variable ==z))

< img src =https://i.stack.imgur.com/qGOlw.pngalt =在这里输入图片描述>


I would like put a bar and a line plot of two separate but related series on the same chart with a legend (the bar plot is of quarterly growth the line plot is of annual growth).

I currently do it with a data.frame in wide format and code like this:

p <- ggplot() +
    geom_bar(df, aes(x=Date, y=quarterly), colour='blue') +
    geom_line(df, aes(x=Date, y=annual), colour='red')

but I cannot work out how to add a legend, which has a red line labeled 'Annual Growth'; and a blue square labeled 'Quarterly Growth'.

Alternatively, I cannot work out how to have differnt geoms for different series with a long-form data.frame.

UPDATE:

The following example code gets me part of the way towards a solution, but with a really ugly duplicate legend. Still looking for a complete solution ... This approach is based on putting the data in long form and then plotting subsets of the data ...

library(ggplot2)
library(reshape)
library(plyr)
library(scales)

### --- make a fake data set
x <- rep(as.Date('2012-01-01'), 24) + (1:24)*30
ybar <- 1:24
yline <- ybar + 1

df <- data.frame(x=x, ybar=ybar, yline=yline)
molten <- melt(df, id.vars='x', measure.vars=c('ybar', 'yline'))
molten$line <- ifelse(molten$variable=='yline', TRUE, FALSE)
molten$bar <- ifelse(molten$variable=='ybar', TRUE, FALSE)

### --- subset the data set
df.line  <- subset(molten, line==TRUE)
df.bar   <- subset(molten, bar==TRUE)

### --- plot it
p <- ggplot() +
geom_bar(data=df.bar, mapping=aes(x=x, y=value, fill=variable, colour=variable),
    stat='identity', position='dodge') +
geom_line(data=df.line, mapping=aes(x=x, y=value, colour=variable)) +

opts(title="Test Plot", legend.position="right") 

ggsave(p, width=5, height=3, filename='plot.png', dpi=150)

And an example plot ...

解决方案

By use of the subset argument to geoms.

> x=1:10;df=data.frame(x=x,y=x+1,z=x+2)
> ggplot(melt(df),
    aes(x,value,color=variable,fill=variable))+
  geom_bar(subset=.(variable=="y"),stat="identity")+
  geom_line(subset=.(variable=="z"))

这篇关于在ggplot2下的一个图表中的条形图和线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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