ggplot条形图问题 - 仅包含条形图的标签

时间:2018-06-14 09:39:38

标签: r ggplot2

我按年生成条形图。每年都有股票市场IPO的数据,这些数据分为3类。我使用下面的代码生成图表

graded_graph <-
  ggplot(graded_count, aes(fy, value, fill = parameter, width = 0.75)) + 
  geom_bar(position = 'dodge', stat = 'identity') + 
  geom_text(aes(fy, value, label=value), position=position_dodge(width=0.75), vjust=-0.5) +
  scale_fill_manual("IPO Market", values = c("blue", "grey", "red"), labels = c("Grade 1 & 2", "Grade 3", "Grade 4 & 5")) +
  ylab("IPOs") + xlab("Fiscal Year") +
  ggtitle("Underpriced IPOs (Based on Issue Grade)")

graded_graph <- graded_graph + theme_tufte() + theme_classic() + theme_phd_min + theme(legend.position = "bottom") +
  theme(axis.text.x = element_text(angle = -90, vjust = 0.5, hjust = 0),
        axis.line = element_line(colour = "black"), panel.grid.major = element_line(colour = "grey"))

显示所有列

Showing All Columns

我现在使用此代码

排除值为0的条形图
graded_graph <-
  ggplot(graded_count, aes(fy, value, fill = parameter, width = 0.75)) + 
  geom_bar(position = 'dodge', stat = 'identity') + geom_text(data=subset(graded_count, value>0),aes(label=value), position=position_dodge(width=0.9), vjust=-0.25) +
  scale_fill_manual("IPO Market", values = c("blue", "grey", "red"), labels = c("Grade 1 & 2", "Grade 3", "Grade 4 & 5")) +
  ylab("IPOs") + xlab("Fiscal Year") +
  ggtitle("Underpriced IPOs (Based on Issue Grade)")

graded_graph <- graded_graph + theme_tufte() + theme_classic() + theme_phd_min + theme(legend.position = "bottom") +
  theme(axis.text.x = element_text(angle = -90, vjust = 0.5, hjust = 0),
        axis.line = element_line(colour = "black"), panel.grid.major = element_line(colour = "grey"))

现在看起来像这样:

隐藏零值列

Hiding zero value columns

如何让值标签位于相应列的顶部而不会有点偏离?这可以在第一年和去年看到。我没有遇到任何解决这个问题的例子。

我从Excel导入的数据快照 Data

0 个答案:

没有答案