如何在ggplot2中每个组的底部添加geom_text标签

时间:2020-05-01 16:47:20

标签: r ggplot2 geom-text

我正在尝试在每个分组的条形下方添加日期,即y轴的0和x轴的产品标签之间的日期。列出的是代表。

df1 <- data.frame(product = c("A","A","A","A","A","A","A","B","B","B","B","B","B","B","C","C","C","C","C","C","C","D","D","D","D","D","D","D"), 
                  start_date =as.Date(c('2020-02-01', '2020-02-02', '2020-02-03', '2020-02-04', '2020-02-05', '2020-02-06', '2020-02-07')),
                  value = c(15.71,17.37,19.93,14.28,15.85,10.5,8.58,5.62,5.19,5.44,4.6,7.04,6.29,3.3,20.35,27.92,23.07,12.83,22.28,21.32,31.46,34.82,23.68,29.11,14.48,25.2,16.91,27.79))

graph <- ggplot(df1, aes(y = value, x = product, fill = product, group = factor(start_date))) +
  geom_col(data = df1, stat = "identity",position = position_dodge(width = 0.8), width = 0.7, inherit.aes = TRUE, size = 0) + 
  geom_text(aes(label= format(as.Date(start_date,format="%Y-%m-%d"), format = "%d")), vjust = "bottom", position = position_dodge(width = 0.8), inherit.aes = TRUE) +
  xlab("Product") + ylab("Values") 

即使我调整调整值或调整值,我也会得到以下图表 enter image description here

我想要与下图相似的日期 enter image description here

1 个答案:

答案 0 :(得分:0)

您几乎拥有了它,但是关键是要认识到所有文本都具有相同的y值:在这种情况下,它的值比0略低。

    @app.route('/predict', methods=["GET"])
    def predict():
        message = "some message" # hardcode a value
        prediction = model.predict([message]) # feeds to model
        classification = encoder.inverse_transform(prediction) # decodes prediction

        return render_template('index.html', message=message, classification=classification)

enter image description here

还要注意,我在这里和那里删除了不需要的位,因为它们是继承的:(1)graph <- ggplot(df1, aes(y = value, x = product, fill = product, group = factor(start_date))) + geom_col(position = position_dodge(width = 0.8), width = 0.7, inherit.aes = TRUE, size = 0) + geom_text(aes( label= format(as.Date(start_date,format="%Y-%m-%d"), format = "%d"), y=-0.5 ), position = position_dodge(width = 0.8), size=2) + xlab("Product") + ylab("Values") 调用data=,(2)geom_col调用{{ 1}}。