ggplot 中的 Y 尺度问题

Y-scale issue in ggplot

我正在尝试使用 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 -> 1211 很好,然后跳到 626。当月份按字母顺序显示时,我认为它遵循旧的因子顺序,但我不知道如何修复它。

图片如下:

无法重现您的问题,所以我猜测您在整个过程中的某个时刻更改了变量的类型并且没有数字 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")

我得到: