调整geom_bar中的x轴刻度和条形颜色(分组条形图)

时间:2016-11-29 03:08:28

标签: r ggplot2 geom-bar

我已经搜索了很多教程,但似乎无法破解 R 中基本的几个命令。

我尝试使用11个类别(barplot 0 的平均百分比数据在ggplot2中创建分组variable 10 ,代表鲑鱼流被枚举的次数)两次periods 1 & 2 之前& After )。

我的基本数据如下:

period 1 = (0)7.5, (1)2.9, (2)6.8, (3)3.9, (4)4.1, (5)7.7, (6)4.4, (7)8.7, (8)10.8, (9)14.5, (10)28.8;
period 2 = (0)8.7, (1)2.5, (2)2.7, (3)6.5, (4)8.3, (5)7.9, (6)6.5, (7)8.8, (8)13.1, (9)13.7, (10)21.3;

到目前为止,我用来生成配对barplot的代码是:

ggplot(data=data, aes(x=variable, y=value, fill=factor(period))) +
 geom_bar(stat="identity", position="dodge") +
 scale_fill_discrete(name="Period", breaks=c(1,2), labels=c("Before WSP", "After WSP")) +
 xlab("Monitoring effort") +
 ylab("Mean percentage")

产生

  

/ Users / price / Desktop / paired barplot.pdf

我的问题是,如何更改x轴刻度以便它们全部显示(0到10 ......不仅仅是" 0"," 2.5",并且不是小数而是整数,如何在不打扰"填充"的情况下将我的条形颜色更改为灰度。命令我的传奇?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我不太清楚你想要什么。

我调整了你的ggplot代码:

ggplot(data=data, aes(x=measure, y=value, fill=factor(timestamp))) +
  geom_bar(stat="identity", position="dodge") +
  xlab("Monitoring effort") +
  ylab("Mean percentage") + 
  scale_fill_manual(values = c("#aeaeae", "#0a0a0a"), labels=c("Before WSP", "After WSP")) + 
  scale_y_continuous(breaks = seq(0,30,by=5))

产生这个情节: enter image description here

注意scale_y_continuous指定了中断,scale_fill_manual带有颜色。

这是数据:

     measure timestamp value
1   measure1     time1   7.5
2   measure2     time1   2.9
3   measure3     time1   6.8
4   measure4     time1   3.9
5   measure5     time1   4.1
6   measure6     time1   7.7
7   measure7     time1   4.4
8   measure8     time1   8.7
9   measure9     time1  10.8
10 measure10     time1  14.5
11 measure11     time1  28.8
12  measure1     time2   8.7
13  measure2     time2   2.5
14  measure3     time2   2.7
15  measure4     time2   6.5
16  measure5     time2   8.3
17  measure6     time2   7.9
18  measure7     time2   6.5
19  measure8     time2   8.8
20  measure9     time2  13.1
21 measure10     time2  13.7
22 measure11     time2  21.3