微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 ggplot2 中的每个条上正确添加标签

如何解决在 ggplot2 中的每个条上正确添加标签

我使用 ggplot2 创建了一个条形图。我试图用它的值在每个条形上添加标签,但由于某种奇怪的原因,当两个值出现在一个条形上时,这是非常错误的。关于如何解决这个问题/我可能做错了什么的任何想法?

library(tidyverse)

data <- structure(list(Date = structure(c(18717,18717,18747,18778,18778),class = "Date"),Category = c("Total PSCE","Businesses","Households","Total PSCE","Households"),Forecast = c(2.3,3.5,2.5,3.4,5.4,3.7,5.1,3.8)),row.names = c(NA,-9L
),class = c("tbl_df","tbl","data.frame"))

ggplot(data,aes(Date,Forecast,fill = Category)) +
  geom_col(position=position_dodge()) +
  geom_text(aes(label = Forecast),colour = "white",size = 3,vjust = 2,position = position_dodge(.9))

enter image description here

解决方法

这里的问题本质上是图在 x 轴上的尺度是连续的,因此通常的位置闪避宽度为 1 是不合适的,因为 3 个条形占据了近 30 天的空间。我尝试用 27 替换宽度参数。这个解决方案取得了一些成功。

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-93-e1b29adbeebb> in <module>
----> 1 classifier = maxent.MaxentClassifier.train(data,bernoulli=False,max_iter=10)

/usr/local/lib/python3.8/dist-packages/nltk/classify/maxent.py in train(cls,train_toks,algorithm,trace,encoding,labels,gaussian_prior_sigma,**cutoffs)
    324         algorithm = algorithm.lower()
    325         if algorithm == "iis":
--> 326             return train_maxent_classifier_with_iis(
    327                 train_toks,**cutoffs
    328             )

/usr/local/lib/python3.8/dist-packages/nltk/classify/maxent.py in train_maxent_classifier_with_iis(train_toks,**cutoffs)
   1175     # Construct an encoding from the training data.
   1176     if encoding is None:
-> 1177         encoding = BinaryMaxentFeatureEncoding.train(train_toks,labels=labels)
   1178 
   1179     # Count how many times each feature occurs in the training data.

/usr/local/lib/python3.8/dist-packages/nltk/classify/maxent.py in train(cls,count_cutoff,**options)
    665 
    666             # Record each of the features.
--> 667             for (fname,fval) in tok.items():
    668 
    669                 # If a count cutoff is given,then only add a joint

AttributeError: 'str' object has no attribute 'items'

reprex package (v2.0.0) 于 2021 年 4 月 7 日创建

另一种解决方案是将月份变量设为分类变量。为此,我喜欢使用 lubridate 包中的函数 library(tidyverse) data <- structure(list(Date = structure(c(18717,18717,18747,18778,18778),class = "Date"),Category = c("Total PSCE","Businesses","Households","Total PSCE","Households"),Forecast = c(2.3,3.5,2.5,3.4,5.4,3.7,5.1,3.8)),row.names = c(NA,-9L ),class = c("tbl_df","tbl","data.frame")) ggplot(data,aes(Date,Forecast,fill = Category)) + geom_col(position=position_dodge(27)) + geom_text(aes(label = Forecast),colour = "white",size = 3,vjust = 2,position = position_dodge(27)) 。我已经演示了这如何使宽度为 1,如下所示。 (month() 函数可用于实现类似的效果)。

as.factor()

reprex package (v2.0.0) 于 2021 年 4 月 7 日创建

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?