如何在条形图中显示日期格式(Sep-12)并设置y轴限制?

时间:2012-11-27 23:06:07

标签: r

我有两个关于使用ggplot()构建条形图的问题。

如何显示数据格式(9月12日)?

我想以9月12日的格式显示日期。我的数据是季度摘要。我想展示3月,6月,9月和12月的季度。但是,我在ggplot()函数中使用了as.Date(YearQuarter)。它显示了4月,7月,10月,1月的不同顺序。

如何增加y轴限制?

y轴设置为70%,其中一个值标签不在图片中。我添加了ylim(0,1)以将y限制增加到1.但是,由于y轴不再显示百分比,我丢失了百分比格式。

x4.can.t.m <- structure(list(NR_CAT = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
1L, 2L, 3L, 1L, 2L, 3L), .Label = c("0%", "1 to 84%", "85% +"
), class = "factor"), TYPE = structure(c(1L, 1L, 1L, 2L, 2L, 
2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 
2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("PM BUSINESS", "PM CONSUMER", 
"PREPAY"), class = "factor"), YearQuarter = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("2011-09-01", 
"2011-12-01", "2012-03-01", "2012-06-01", "2012-09-01"), class = "factor"), 
    value = c(0.5, 0, 0.5, 0.35, 0, 0.65, 0.28, 0.02, 0.7, 0.4, 
    0, 0.6, 0.38, 0, 0.62, 0.43, 0.01, 0.56, 0.57, 0, 0.43, 0.35, 
    0, 0.65, 0.39, 0.01, 0.6, 0.55, 0, 0.45, 0.4, 0.02, 0.58, 
    0.35, 0.02, 0.63, 0.35, 0, 0.65, 0.55, 0.01, 0.44, 0.47, 
    0, 0.53)), .Names = c("NR_CAT", "TYPE", "YearQuarter", "value"
), row.names = c(NA, -45L), class = "data.frame")

这是我的情节代码:

x4.can.t.m$YearQuarter <- as.Date(x4.can.t.m$YearQuarter)
x4.can.t.d.bar <- ggplot(data=x4.can.t.m, aes(x=YearQuarter, y=value,fill=NR_CAT)) + 
                  geom_bar(stat="identity",position = "dodge",ymax=NR_CAT+0.2) +
              facet_wrap(~TYPE,ncol=1) +
                  geom_text(aes(label =paste(round(value*100,0),"%",sep="")), 
                             position=position_dodge(width=0.9), 
                             vjust=-0.25,size=3)  +
        scale_y_continuous(formatter='percent',ylim=1) +
            labs(y="Percentage",x="Year Quarter") +
            ylim(0,100%)


x4.can.t.d.bar +scale_fill_manual("Canopy Indicators",values=tourism.cols(c(6,9,8)))+
                opts(title="Canopy Indicator: All Customers portout for Network 
                            Issues",size=4)

1 个答案:

答案 0 :(得分:1)

看起来你有一个旧版本的ggplot;以下是ggplot 0.2.9.1。我必须修复几件事才能使你的情节发挥作用。从x4.can.t.m的原始定义开始:

x4.can.t.m$YearQuarter <- format(as.Date(x4.can.t.m$YearQuarter),"%b-%y")

library("scales")
ggplot(data=x4.can.t.m, aes(x=YearQuarter, y=value, fill=NR_CAT)) + 
  geom_bar(stat="identity", position = "dodge") +
  geom_text(aes(label = paste(round(value*100,0),"%",sep=""), group=NR_CAT), 
            position=position_dodge(width=0.9), 
            vjust=-0.25, size=3)  +
  scale_y_continuous("Percentage", labels=percent, limits=c(0,1)) +
  labs(x="Year Quarter") +
  scale_fill_discrete("Canopy Indicators") + 
  facet_wrap(~TYPE,ncol=1) +
  ggtitle("Canopy Indicator: All Customers portout for Network Issues") +
  theme(plot.title = element_text(size=rel(1.2)))

enter image description here

问题的第一部分是通过将YearQuarter格式化为您想要的格式,将其保留为字符串来实现的。

第二部分在scale_y_continuous中指定限制,并使用labels参数指定格式化函数。请注意,此部分需要library("scales")才能正常工作。