条形图标签ggplot2 R

时间:2015-11-02 22:49:27

标签: r ggplot2

我有如下数据。我正在尝试创建一个单独的条形图来显示针对目标的收入。

黑条表示2015年第一天的当前收入。它几乎是实时数据,每天或每6小时更新一次。所以黑条每天都在增加。红色条表示达到第一个目标的收入金额。白色条表示达到第二个目标的收入金额。

我的问题是如何创建标签来显示每个条的值,如下图所示。并且每个栏上的三角形是必需的。提前致谢!
df = data.frame(revenue = runif(306, min = 10, max = 20),
               day = seq(1,306,1),
               year = rep(2015,306))
df = aggregate(revenue~year, data = df, sum)
b1 = 5200
b2 = 6000
df = cbind(df,b1,b2)
df$year = as.factor(df$year)

ggplot(df, aes(year,b2)) +
  geom_bar(fill = "white", width=.1, stat="identity")+
  geom_bar(aes(year, b1), fill  = "red", width=.1, stat="identity")+
  geom_bar(aes(year, revenue), fill = "black",
           width=.1, stat="identity") +
  coord_flip()

picture

编辑11/3

我对原始代码进行了一些调整,并尝试使用geom_point()在图中添加三角形。但我无法移动栏下的三角形。有什么想法吗?

df = data.frame(year = factor(2015),revenue = 4300, goal1 = 5200, goal2 = 6000)
df2 = data.frame(year=factor(2015),label=c("revenue","goal1","goal2"),value=c(4300,5200,6000))

ggplot(df, aes(year,goal2)) +
  geom_bar(fill = "white", width=.1, stat="identity")+
  geom_bar(aes(year, goal1), fill  = "blue", width=.1, stat="identity")+
  geom_bar(aes(year, revenue), fill = "red",
           width=.1, stat="identity") +
  geom_text(data = df2, aes(year,value, label=paste(label,"\n",value), 
                vjust=ifelse(label=="revenue",2,-1)), size = 5) +
  geom_errorbar(aes(yintercept = goal1, ymax=goal1, ymin=goal1),
                 color = "black", size = 1, width=.1)+
  geom_point(data = df, aes(year, revenue), shape = 17, size = 5,  col = "firebrick4", bg = "firebrick4") + coord_flip()

enter image description here

1 个答案:

答案 0 :(得分:2)

首先,您需要转置数据,将“目标”和“收入”(在图上共享类型(金钱)和维度(x))分成一列,并标注(注释)进入另一栏:

df2 = data.frame(year=factor(2015),
                 label=c("revenue","goal 1","goal 2"),
                 value=c(4533,5200,6000))

为了使条形按正确的顺序重叠,您最初以该特定顺序添加了geom_bars(如果您的值的顺序不正确,则会失败)。现在,他们将通过一次调用geom_bar进行绘制,并且可以通过重新排序标签因子并根据值排列数据来解决正确的分层问题:

ggplot(arrange(mutate(df2,label=reorder(label,value,sum)),-value)) + 
  scale_fill_manual(values=c(revenue="red",`goal 1`="blue",`goal 2`="white"),guide="none") +
  geom_bar(aes(year, value, fill=label),stat="identity", position="identity",alpha=1,width=0.1) +
  geom_text(aes(year,value, label=paste(label,"\n",value), vjust=ifelse(label=="revenue",2,-1))) +
  coord_flip()

并添加geom_text来标记它们。我只是选择了随机颜色,抱歉。

enter image description here

注意:

vjust使用起来相当烦人,它是根据文本显示框高度的倍数来衡量的。只需调整它直到它是正确的。可能使用两个geom_texts来输出标签和值。

可以根据需要使用geom_text或geom_point添加三角形。

或者,丢弃自动x轴标签并使用x轴显示实际值。这符合tuftes的建议。如果收入接近其中一个目标,您可能需要处理重叠标签问题(基本情节处理此问题但ggplot没有)。

更新

OP请求帮助定位指针三角形。这是使用自定义位置调整的可能解决方案。我也设置了它,所以收入始终在最高位置(使用新的zorder列)。

df2 = data.frame(year=factor(2015),label=c("revenue","goal 1","goal 2"),value=c(4533,5200,6000),zorder=c(3,2,1))
df2.ordered=arrange(mutate(df2,label=reorder(label,zorder,sum)),zorder)

ggplot(df2.ordered) + aes(x=year, y=value, fill=label) +
  scale_fill_manual(values=c(revenue="red",`goal 1`="blue",`goal 2`="white"),guide="none") +
  geom_bar(stat="identity", position="identity",alpha=1,width=0.1) +
  geom_text(aes(label=paste(label,"\n",value), vjust=ifelse(label=="revenue",2,-1))) +
  geom_point(aes(x=year, y=value, fill=2), hjust=100, fill= 1, shape = 17, size = 5, hjust=-300, col = "firebrick4", 
             bg = "firebrick4", position=c(adjust=function(data)mutate(data,x=x-0.05))) +
  coord_flip()

此刻度与本示例中的文本不在同一侧 - 请自行修复。

enter image description here