【问题标题】:Ignoring unknown aesthetics: text in ggplot忽略未知的美学:ggplot中的文本
【发布时间】:2021-04-30 13:33:55
【问题描述】:

我有这个数据集:

library(tidyverse)
dataset <- data.frame(CustomerId = c(1,2),
                      umap_01 = c(0.0198, -0.319),
                      umap_02 = c(0.336, -0.321))

我想用这段代码创建一个ggplot:

p <- dataset %>%
  ggplot(aes(umap_01,umap_02)) +
  geom_point(aes(text = CustomerId), alpha = 0.5)

但我收到了这条消息:

Warning message:
Ignoring unknown aesthetics: text 

我不明白为什么文本是一种有效的审美。拜托,你能帮我这个代码吗?我做错了什么?

【问题讨论】:

  • 您要绘制的是什么? text 不是美学,但label 是!您还需要定义xy 美学
  • 对于我想显示 CustomerId 的每个点,在这种情况下,x 审美是umap_01,y 审美是umap_02

标签: r ggplot2


【解决方案1】:

另一种选择是使用形状。不过,您需要使用scale_shape_identityI()

library(tidyverse)
dataset <- data.frame(CustomerId = c(1,2),
                      umap_01 = c(0.0198, -0.319),
                      umap_02 = c(0.336, -0.321))
dataset %>%
  ggplot(aes(umap_01,umap_02)) +
  geom_point(aes(shape = I(as.character(CustomerId))), size = 10, alpha = 0.5) 

reprex package (v2.0.0) 于 2021-04-30 创建

【讨论】:

  • Shape 是一个很棒的方法!谢谢@tjebo
【解决方案2】:

您可以使用geom_text。例如:

p <- dataset %>%
        ggplot(aes(umap_01,umap_02, label = CustomerId)) +
        geom_text(alpha = 0.5)
p

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多