【问题标题】:How to remove zero frequency for frequency plot and fix time?如何去除频率图的零频率并固定时间?
【发布时间】:2019-08-07 17:46:26
【问题描述】:

当我制作频率图时:

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 轴上显示每个条形的所有年份?

【问题讨论】:

  • 使用x = as.character(Year) 而不是x = Year

标签: r ggplot2


【解决方案1】:

这是你想要的吗?

获取它的代码是:

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

【讨论】:

    猜你喜欢
    • 2017-06-17
    • 1970-01-01
    • 2017-02-09
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多