ggplot中的Y-scale问题

时间:2015-10-04 15:23:52

标签: r ggplot2 dataframe bar-chart axis

我正在尝试使用ggplot 2绘制名为part2的以下数据框。这是我的数据示例:

Month Denial.Code freq
1    January       Total    0
2   February       Total  626
3      March       Total 1151
4      April       Total  849
5        May       Total 1144
6       June       Total  957
7       July       Total  911
8     August       Total 1145
9  September       Total  902
10   October       Total 1211
11  November       Total  920
12  December       Total  948

ggplot(part2, aes (x = Month,y=freq, fill = 
      factor(Denial.Code)))+geom_bar(stat="identity", position="dodge")+xlab("")
      +ylab("Denial Frequency")+scale_fill_discrete(name="Denial Code")
      +ggtitle("# Denials by Month")

然而,由于某种原因,我的y尺度非常混乱。 y比例图从0 - >到0。 1211很好,然后跳到626.我认为这是按照旧的因子顺序,按月按字母顺序排列,但我不知道如何解决它。

图片如下:

enter image description here

1 个答案:

答案 0 :(得分:0)

无法重现您的问题,所以我在某个过程中猜测您的过程会改变您的变量'类型,你没有数字freq变量。

这是我尝试过的:

part2 = read.table(text=
"Month Denial.Code freq
1    January       Total    0
2   February       Total  626
3      March       Total 1151
4      April       Total  849
5        May       Total 1144
6       June       Total  957
7       July       Total  911
8     August       Total 1145
9  September       Total  902
10   October       Total 1211
11  November       Total  920
12  December       Total  948", header=T)

library(ggplot2)

# re-order the level of variable to be plotted in the right order
part2$Month = factor(part2$Month, c("January","February","March","April","May",
                        "June","July","August","September","October",
                        "November","December"))

ggplot(part2, aes (x = Month,y=freq, fill = factor(Denial.Code)))+
  geom_bar(stat="identity", position="dodge")+
  xlab("") +ylab("Denial Frequency")+
  scale_fill_discrete(name="Denial Code")+
  ggtitle("# Denials by Month")

我得到了:

enter image description here