问题描述
我试图获得一个相当常见的barplot,但尽管阅读了大量有关R中绘图的文档以及ggplot及其所有图层的文档,但我无法按照我想要的方式获得这个绘图。
我的数据非常简单。
aov.data
区块RTreg RTrnd
1区块1 0.0000 862.0707
2区块2 667.2081 770.4315
3 Block3 645.4730 696.0200
4 Block4 674.5200 659.4765
5 Block5 651.4295 633.7333
我想要得到的是一个带x轴的 Block
列的barplot,作为分类变量。在y轴上,我希望有值绘制 RTreg
和 RTrnd
列。这是我无法理解的部分。我想在x轴上每个刻度标记都有两个小节。一个代表该区块中的 RTreg
值的栏,以及代表该区块中的 RTrnd
值的栏。就像这样:
。
但是有5组两条而不是两条。
到目前为止,我已经使用了以下代码:
ggplot (aov.data,aes(x = Block))+
geom_bar(aes(y = RTreg),stat =identity,position =dodge,col =blue)+
geom_bar (position =dodge)+
geom_bar(aes(y = RTrnd),stat =identity,position =dodge,col =red)
我以为我在做的是首先创建包含块的分类x轴的基础绘图。然后,用 geom_bar
我以为我是先添加 RTreg
列,再加上第二个 geom_bar
,我添加了 RTrnd
列。选择位置
等于 dodge
我虽然我将并排获取两条线。然而,我得到的情节是:
。
有关如何并排获取两个酒吧的任何想法,并希望有不同颜色和每个传说?我真的很感谢这里的一些指导。
在此先感谢您。 您必须重新整理数据从宽到长的框架,然后你不需要分别为每个条件设置栏。假设数据框被命名为 df
。
<$ $ d
$ ggplot(df.long,aes(Block,value,fill = variable))+
geom_bar(stat =identity,position =dodge)
I am trying to get a barplot which is fairly common, but despite reading tons of documentation on plotting in R, and the documentation of ggplot and all its layers, I cannot get this plot the way I want it to.
My data is fairly simple.
aov.data
Block RTreg RTrnd
1 Block1 0.0000 862.0707
2 Block2 667.2081 770.4315
3 Block3 645.4730 696.0200
4 Block4 674.5200 659.4765
5 Block5 651.4295 633.7333
What I want to get is a barplot with the Block
column in the x axis, working as a categorical variable. In the y axis I want to have the values to plot the RTreg
and RTrnd
columns. This is the part I cannot get right. What I would like is t have two bars per tick mark in the x axis. One bar representing the value of RTreg
in that Block, and one bar representing the value of RTrnd
in that block. Something like this:
.
But with 5 sets of two bars instead of two.
So far, I have used the following code:
ggplot(aov.data,aes(x=Block)) +
geom_bar(aes(y=RTreg),stat="identity",position="dodge",col="blue") +
geom_bar(position="dodge") +
geom_bar(aes(y=RTrnd),stat="identity",position="dodge",col="red")
What I thought I was doing was to first create the base plot with the categorical x axis containing the blocks. Then, with geom_bar
I thought I was adding, first, the RTreg
column, and with the second geom_bar
, that I was adding the RTrnd
column. With the option position
being equal to dodge
I though I was going to get the two bars, side by side. However, the plot I am getting is:
Valid XHTML http://s12.postimage.org/k0przrmtp/failed_geom_bar.png.
Any idea on how to get the two bars side by side, and hopefully with different colors and a legend for each? I would really appreciate some guidance here.
Thanks in advance.
You have to reshape your data frame from wide to long and then you don't need to set bars for each condition separately.
Assuming that data frame is named df
.
library(reshape2)
df.long<-melt(df)
ggplot(df.long,aes(Block,value,fill=variable))+
geom_bar(stat="identity",position="dodge")
这篇关于与两个变量并排的Barplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!