geom_text制作标签出现在图中

发布于 2025-02-12 19:07:56 字数 166 浏览 2 评论 0 原文

我正在尝试使我的dataFrame的列“Activitéen valeur”的值出现在我的geom_bar中的栏上方,但由于我收到以下错误消息,它不起作用。数据(11):标签”。我该如何修复?

library(ggplot2)

ggplot() +
geom_bar

I am trying to make the values of the column "activité en valeur" of my dataframe appear above my bars in geom_bar, yet it doesn't work as i get the following message of error "Aesthetics must be either length 1 or the same as the data (11): label". How can i fix it ?

library(ggplot2)

ggplot() +
geom_bar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小红帽 2025-02-19 19:07:56

标签上添加标签映射审美<代码> aes(),而不是将数据帧列作为参数。另外,由于您有一个躲闪的栏,因此还必须将标签的位置设置为 position_dodge(),在这里您必须设置道奇的宽度,默认情况下,该宽度为.9 for geom_bar/col 。我还将您的值四舍五入为一个数字,并通过设置 vjust = - 。1稍微移动它们。最后,作为一般提醒,请勿使用 fra_acti_valeur_par_type $ ... 在ggplot2中。只需在Aesthetics上映射列名:

library(ggplot2)

ggplot(fra_acti_valeur_par_type, aes(
  x = `type de séjour`, y = `activité en valeur`,
  fill = as.factor(annee)
)) +
  geom_col(position = "dodge") +
  ggtitle("Evolution du mix d'activité en valeur (CA/nombre de séjours)") +
  labs(
    y = "CA/nombre de séjours",
    x = "type de séjour",
    fill = "année"
  ) +
  geom_text(aes(label = round(`activité en valeur`, 1)),
    vjust = -.1,
    position = position_dodge(width = .9),
    na.rm = TRUE
  )
#> Warning: Removed 1 rows containing missing values (geom_col).

“”

To add your labels map on the label aesthetic inside aes() instead of passing dataframe column as argument. Also, as you have a dodged bar you have to set the position for the labels to position_dodge() as well, where you have to set the width of the dodge which by defaults to .9 for geom_bar/col. I also rounded your values to one digit and moved them slightly by setting vjust=-.1. Finally, as a general reminder, don't use fra_acti_valeur_par_type$... in ggplot2. Simply map the column name on aesthetics:

library(ggplot2)

ggplot(fra_acti_valeur_par_type, aes(
  x = `type de séjour`, y = `activité en valeur`,
  fill = as.factor(annee)
)) +
  geom_col(position = "dodge") +
  ggtitle("Evolution du mix d'activité en valeur (CA/nombre de séjours)") +
  labs(
    y = "CA/nombre de séjours",
    x = "type de séjour",
    fill = "année"
  ) +
  geom_text(aes(label = round(`activité en valeur`, 1)),
    vjust = -.1,
    position = position_dodge(width = .9),
    na.rm = TRUE
  )
#> Warning: Removed 1 rows containing missing values (geom_col).

熟人话多 2025-02-19 19:07:56

您在 geom_text 美学中使用 group 的另一个选项,因此它知道要躲闪的值:

library(ggplot2)
ggplot(fra_acti_valeur_par_type, aes(x = `type de séjour`, y = 
                                       `activité en valeur`, 
                                     fill = as.factor(annee))) +
  geom_bar(stat = "identity", position = "dodge") +
  ggtitle("Evolution du mix d'activité en valeur (CA/nombre de séjours)") +
  labs(y = "CA/nombre de séjours",
       x = "type de séjour",
       fill = "année") + 
  geom_text(data = fra_acti_valeur_par_type,
            aes(x = `type de séjour`, y = `activité en valeur` + 150, group = annee, label = format(`activité en valeur`, digits = 1)),
            size = 3,
            position = position_dodge(.9),
            inherit.aes = TRUE,
            na.rm = TRUE)

output:

“在此处输入图像说明”

Another option where you use group in your geom_text aesthetics, so it knows which values to dodge:

library(ggplot2)
ggplot(fra_acti_valeur_par_type, aes(x = `type de séjour`, y = 
                                       `activité en valeur`, 
                                     fill = as.factor(annee))) +
  geom_bar(stat = "identity", position = "dodge") +
  ggtitle("Evolution du mix d'activité en valeur (CA/nombre de séjours)") +
  labs(y = "CA/nombre de séjours",
       x = "type de séjour",
       fill = "année") + 
  geom_text(data = fra_acti_valeur_par_type,
            aes(x = `type de séjour`, y = `activité en valeur` + 150, group = annee, label = format(`activité en valeur`, digits = 1)),
            size = 3,
            position = position_dodge(.9),
            inherit.aes = TRUE,
            na.rm = TRUE)

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文