如何删除频率图的零频率并确定时间?

时间:2019-08-07 17:46:26

标签: r ggplot2

当我绘制频率图时:

Data <- structure(list(Venue = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("Conference", "Journal"), class = "factor"), Year = c(2008L, 
2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 
2018L, 2019L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 
2015L, 2016L, 2017L, 2018L), Frequency = c(0L, 0L, 0L, 0L, 1L, 
1L, 2L, 1L, 4L, 4L, 11L, 3L, 2L, 1L, 0L, 0L, 3L, 5L, 3L, 7L, 
8L, 19L, 10L)), class = "data.frame", row.names = c(NA, -23L))
library(ggplot2)
ggplot(Data, aes(x = Year, y = Frequency, fill = Venue, label = Frequency)) +
  geom_bar(stat = "identity") +
  geom_text(size = 3, position = position_stack(vjust = 0.5))

我在图值中收到零,并且x轴上的年份似乎不是数据框

怎么可能从图中删除零频率(但要保留从2012年开始的年份,即图中的记录),并在每一年的每一年在x轴上显示?

1 个答案:

答案 0 :(得分:2)

这是您想要的吗?

ggplt

获得它的代码是:

ggplot(Data, aes(x = as.character(Year), y = Frequency, fill = Venue, 
                 label = ifelse(Frequency > 0, Frequency, numeric(0)))) +
  geom_bar(stat = "identity") +
  geom_text(size = 3, position = position_stack(vjust = 0.5)) +
 scale_x_discrete(name ="Year")