0

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

如何显示数据格式(Sep-12)?

我想以 Sep-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)
4

1 回答 1

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

在此处输入图像描述

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

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

于 2012-11-28T00:04:59.397 回答