ggplot中的y-scale问题 [英] Y-scale issue in ggplot

查看:73
本文介绍了ggplot中的y-scale问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to plot the following data frame called part2 with ggplot 2. This is a sample of my data:

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.我认为当月份以字母顺序显示时,它遵循的是旧的因子顺序,但我不知道如何解决.

However my y-scale is extremely jumbled up for some reason. The y-scale plot goes from 0 -> 1211 fine, and then jumps to 626. I think it was following an old factor order when the months were presented in Alphabetical order, but I don't know how to fix it.

图片如下:

推荐答案

无法重现您的问题,因此我猜测在您的过程中的某个时候您更改了变量的类型并且没有数字 freq 变量.

Can't reproduce your problem, so I'm guessing at some point through out your process you change your variables' type and you don't have a numeric freq variable.

这是我尝试过的:

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")

我得到:

这篇关于ggplot中的y-scale问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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