如何使用ggplot2将条线覆盖在条形图上? [英] How can a line be overlaid on a bar plot using ggplot2?
问题描述
我正在寻找一种方法来绘制包含两个不同系列的条形图,隐藏其中一个系列的条形图,而是有一条线(如果可能的话,可以平滑)通过隐藏系列的条形顶部已经(类似于在直方图上覆盖频率多项式的方式)。我试过了下面的例子,但似乎遇到了两个问题。
首先,我需要按组总结(总计)数据,其次, (df2)转换为一行。
df < - data.frame(grp = c (AABBCC)val = c(1,1,2,2,3,3) - data.frame(grp = c(A,A,B,B,C,C),val = c(1,4,3,5,1,2) )
ggplot(df,aes(x = grp,y = val))+
geom_bar(stat =identity,alpha = 0.75)+
geom_bar(data = df2,aes x = grp,y = val),stat =identity,position =dodge)
也许您的示例数据并不代表您正在使用的真实数据,但是没有要绘制的 df2
。每个x和y值只有一个值。这里有一个修改版本的 df2
,它有足够的数据点来构建行:
df < - data.frame(grp = c(A,A,B,B,C,C),val = c(1,2,3,1 ,2,3))
df2 < - data.frame(grp = c(A,A,B,B,C,C),val = c (1,4,3,5,0,2))
p < - ggplot(df,aes(x = grp,y = val))
p < - p + geom_bar (stat =identity,alpha = 0.75)
p + geom_line(data = df2,aes(x = grp,y = val),color =blue)
$另外,如果上面的示例数据是正确的,那么可以用 geom_point(data = df2,aes(x = grp,y = val),color =red,size = 6)
。你可以明显地改变颜色和大小,你喜欢。
编辑:在回应评论
我不完全确定直方图上的频率多项式的视觉效果应该是什么样子。 x值是否应该相互连接?其次,你继续提到想要的行,但你的代码显示 geom_bar()
,我认为这不是你想要的吗?如果您需要线条,请使用 geom_lines()
。如果上面的两个假设是正确的,那么这里有一个方法来做到这一点:
#First让我们按组$ b $总结df2 (df2,...(grp),总结,total = sum(val))
> df3
grp total
1 A 5
2 B 8
3 C 3
#第二,让我们在处理grp变量时将df3绘制为一条线作为数字
p <-ggplot(df,aes(x = grp,y = val))
p <-p + geom_bar(alpha = 0.75,stat =identity)
p + geom_line(data = df3,aes(x = as.numeric(grp),y = total),color =red)
I'm looking for a way to plot a bar chart containing two different series, hide the bars for one of the series and instead have a line (smooth if possible) go through the top of where bars for the hidden series would have been (similar to how one might overlay a freq polynomial on a histogram). I've tried the example below but appear to be running into two problems.
First, I need to summarize (total) the data by group, and second, I'd like to convert one of the series (df2) to a line.
df <- data.frame(grp=c("A","A","B","B","C","C"),val=c(1,1,2,2,3,3))
df2 <- data.frame(grp=c("A","A","B","B","C","C"),val=c(1,4,3,5,1,2))
ggplot(df, aes(x=grp, y=val)) +
geom_bar(stat="identity", alpha=0.75) +
geom_bar(data=df2, aes(x=grp, y=val), stat="identity", position="dodge")
解决方案 Perhaps your sample data aren't representative of the real data you are working with, but there are no lines to be drawn for df2
. There is only one value for each x and y value. Here's a modifed version of your df2
with enough data points to construct lines:
df <- data.frame(grp=c("A","A","B","B","C","C"),val=c(1,2,3,1,2,3))
df2 <- data.frame(grp=c("A","A","B","B","C","C"),val=c(1,4,3,5,0,2))
p <- ggplot(df, aes(x=grp, y=val))
p <- p + geom_bar(stat="identity", alpha=0.75)
p + geom_line(data=df2, aes(x=grp, y=val), colour="blue")
Alternatively, if your example data above is correct, you can plot this information as a point with geom_point(data = df2, aes(x = grp, y = val), colour = "red", size = 6)
. You can obviously change the color and size to your liking.
EDIT: In response to comment
I'm not entirely sure what the visual for a freq polynomial over a histogram is supposed to look like. Are the x-values supposed to be connected to one another? Secondly, you keep referring to wanting lines but your code shows geom_bar()
which I assume isn't what you want? If you want lines, use geom_lines()
. If the two assumptions above are correct, then here's an approach to do that:
#First let's summarise df2 by group
df3 <- ddply(df2, .(grp), summarise, total = sum(val))
> df3
grp total
1 A 5
2 B 8
3 C 3
#Second, let's plot df3 as a line while treating the grp variable as numeric
p <- ggplot(df, aes(x=grp, y=val))
p <- p + geom_bar(alpha=0.75, stat = "identity")
p + geom_line(data=df3, aes(x=as.numeric(grp), y=total), colour = "red")
这篇关于如何使用ggplot2将条线覆盖在条形图上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!