How to overlay geom_bar and geom_line plots with different number of elements using ggplot2?

早过忘川 提交于 2019-12-19 05:08:15

问题


Assuming I have two data.frames with different data but in the same range of x-values

a <-data.frame(x=c(1,1,1,2,2,2,3,3,3),
               y=c(0.3,0.4,0.3,0.2,0.5,0.3,0.4,0.4,0.2), 
               z=c("do","re","mi","do","re","mi","do","re","mi"))

b <- data.frame(x=c(1,2,3),y=c(10,15,8))

Both, a and b have the same range of X values (1,2,3) but while a is a data.frame with 9 rows, b is a data.frame with 3 rows.

I use geom_bar in order to plot the distribution of values of a, like this:

ggplot(a, aes(x=x, y=y, fill=z)) +
    geom_bar(position="stack",stat="identity") + 
    ylab("") + 
    xlab("x")

And I use geom_line to plot b data, like this:

ggplot(b, aes(x=x, y=y)) + 
    geom_line(stat="identity") + 
    ylab("") + xlab("x") + ylim(0,15)

Now I would like to overlay this geom_line plot to the previous geom_bar plot. My first try was to do the following:

ggplot(a, aes(x=x, y=y, fill=z)) +
    geom_bar(position="stack",stat="identity") + 
    ylab("") + xlab("x") +
    ggplot(b, aes(x=x, y=y)) + 
    geom_line(stat="identity") + 
    ylab("") + xlab("x") + ylim(0,15)

With no success.

How can I overlay a geom_line plot to a geom_bar plot?


回答1:


Try this

p <- ggplot() 
p <- p + geom_bar(data = a, aes(x=x, y=y, fill=z), position="stack",stat="identity")
p <- p + geom_line(data = b, aes(x=x, y=y/max(y)), stat="identity") 
p

Update: You can rescale the one y to make them the same. As I don't know the relations between the two ys, I rescaled them by using y/max(y). Does this solve you problem?




回答2:


Try merging the datasets first, then plotting, like this:

require(ggplot2)

df <- merge(a,b,by="x")

ggplot(df, aes(x=x, y=y.x, fill=z)) +
  geom_bar(position="stack",stat="identity") + 
  geom_line(aes(x=x, y=y.y)) + 
  ylab("") + xlab("x")

Output:

I edited the sample data to better illustrate the effects, because the y-axis scaling of the original data would not have matched well:

a <-data.frame(x=c(1,1,1,2,2,2,3,3,3),
               y=c(0.3,0.4,0.3,0.2,0.5,0.3,0.4,0.4,0.2), 
               z=c("do","re","mi","do","re","mi","do","re","mi"))

b <- data.frame(x=c(1,2,3),y=c(.4,1,.4))


来源:https://stackoverflow.com/questions/46147739/how-to-overlay-geom-bar-and-geom-line-plots-with-different-number-of-elements-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!