用geom_bar()覆盖两个条形图 [英] Overlay two bar plots with geom_bar()

查看:232
本文介绍了用geom_bar()覆盖两个条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将两个条形图叠加在彼此之上,而不是在彼此之上. 数据来自同一数据集.我希望在x轴上使用阻止",并在重叠条形图中使用开始"和结束".

I'm trying to overlay two bar plots on top of each other, not beside. The data is from the same dataset. I want 'Block' on the x-axis and 'Start' and 'End' as overlaying bar plots.

    Block   Start       End
1    P1L     76.80       0.0
2    P1S     68.87       4.4
3    P2L     74.00       0.0
4    P2S     74.28       3.9
5    P3L     82.22       7.7
6    P3S     80.82      17.9

我的脚本是

 ggplot(data=NULL,aes(x=Block))+
    geom_bar(data=my_data$Start,stat="identity",position ="identity",alpha=.3,fill='lightblue',color='lightblue4')+
    geom_bar(data=my_data$End,stat="identity",position ="identity",alpha=.8,fill='pink',color='red')

我得到错误:ggplot2不知道如何处理数字数值的数据

I get Error: ggplot2 doesn't know how to deal with data of class numeric

我也尝试过

    ggplot(my_data,aes(x=Block,y=Start))+
      geom_bar(data=my_data$End, stat="identity",position="identity",...)

有人知道我该如何实现吗?谢谢.

Anyone know how I can make it happen? Thank you.

如何获得道奇覆盖条?

我编辑此帖子,因为我的下一个问题是相关的,因为这与我的原始帖子相反.

I edit this post, because my next question is relevant as it's the opposite problem of my original post.

@ P.merkle

@P.merkle

我必须将绘图更改为四个条形,以显示所有标记为L和S的图块的平均值.L代表沿海地区,S代表亚沿海地区.他们接受了两种治疗:正常和减量.

I had to change my plot into four bars showing the mean values of all Blocks labeled L and S. The L stand for littoral, and S for Sublittoral. They were exposed for two treatments: Normal and reduced.

我已经计算出均值及其标准偏差. 我需要四个条带各自的误差线: 正常/沿海,减少/沿海,正常/近海滨,减少/近海滨.

I've calculated the means, and their standard deviation. I need four bars with their respective error bars: Normal/Littoral , Reduced/Littoral , Normal/Sublittoral , Reduced/Sublittoral.

问题是当我绘制它时,沿岸条和沿岸下条都重叠!因此,现在我希望它们重叠! 我该如何实现?我已经尝试过各种position = 'dodge'position = position_dodge(newdata$Force),但是没有运气...

Problem is when I plot it, both the littoral bars and both the sublittoral bars overlay each other! So now I want them not to overlap! How can i make it happen? I've tried all sorts of position = 'dodge' andposition = position_dodge(newdata$Force), without luck...

我的newdata包含以下信息:

Zonation Force N mean sd se 1 Litoral Normal 6 0.000000 0.000000 0.000000 2 Litoral Redusert 6 5.873333 3.562868 1.454535 3 Sublitoral Normal 6 7.280000 2.898903 1.183472 4 Sublitoral Redusert 6 21.461667 4.153535 1.695674

Zonation Force N mean sd se 1 Litoral Normal 6 0.000000 0.000000 0.000000 2 Litoral Redusert 6 5.873333 3.562868 1.454535 3 Sublitoral Normal 6 7.280000 2.898903 1.183472 4 Sublitoral Redusert 6 21.461667 4.153535 1.695674

我的脚本是这样的:

ggplot(data=cdata,aes(x=newdata$Force,y=newdata$mean))+
        geom_bar(stat="identity",position ="dodge",
                 alpha=.4,fill='red', color='lightblue4',width = .6)+
        geom_errorbar(aes(ymin=newdata$mean-sd,ymax=newdata$mean+sd),
                      width=.2, position=position_dodge(.9))

很遗憾,结果是

至于误差线,显然那里有四个线,但它们重叠.拜托,我该怎么解决?

As of the error bars, it's clearly four bars there, but they overlap. Please, how can I solve this?

推荐答案

对于您的问题,有一个简单的,更复杂的答案.

There is a simple, and a more complex answer to your question.

如果您不需要图例,则简单的解决方案可能对您有用.

If you don't need a legend, the simple solution might work for you.

如果您想在条形图旁边显示图例,请考虑使用更复杂的解决方案,该解决方案要求将数据从宽格式转换为长格式.

If you would like to display a legend next to the bar plot, consider using the more complex solution, which requires your data to be converted from wide format to long format.

您可以在单个几何体(此处为geom_bar)的级别上完善美学规范:

You can refine your aesthetics specification on the level of individual geoms (here, geom_bar):

ggplot(data=my_data,aes(x=Block))+
  geom_bar(aes(y=Start),stat="identity",position ="identity",alpha=.3,fill='lightblue',color='lightblue4') +
  geom_bar(aes(y=End),stat="identity",position ="identity",alpha=.8,fill='pink',color='red')

要添加图例,请使用reshape2::melt将数据框从宽格式转换为长格式,从而为您提供variable列(开始"对结束")和value.然后使用variable列定义图例:

To add a legend, use reshape2::melt to convert your data frame from wide format into long format, which gives you the columns variable ("Start" vs. "End"), and value. Then use the variable column to define your legend:

library(reshape2)
my_data_long <- melt(my_data, id.vars = c("Block"))
ggplot(data=my_data_long,aes(x=Block, y=value, fill=variable, color=variable, alpha=variable)) +
  geom_bar(stat="identity",position ="identity") +
  scale_colour_manual(values=c("lightblue4","red")) +
  scale_fill_manual(values=c("lightblue","pink")) +
  scale_alpha_manual(values=c(.3, .8))

这篇关于用geom_bar()覆盖两个条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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