【问题标题】:R: Ignoring unknown aesthetics: labelR:忽略未知的美学:标签
【发布时间】:2018-02-14 01:49:24
【问题描述】:

我在使用 geom_point 进行绘图时出错。

library(ggplot2)
theme_set(theme_bw())

mtcars$`car name` <- rownames(mtcars) 
mtcars$mpg_z <- round((mtcars$mpg - mean(mtcars$mpg))/sd(mtcars$mpg), 2)  
mtcars$mpg_type <- ifelse(mtcars$mpg_z < 0, "below", "above") 
mtcars <- mtcars[order(mtcars$mpg_z), ]  # sort
mtcars$`car name` <- factor(mtcars$`car name`, levels = mtcars$`car name`) 

ggplot() + 
  geom_point(data=mtcars, aes(x=`car name`, y=mpg_z,label=mpg_z), stat='identity', fill="black", size=6)  +
  geom_text(color="white", size=2) +
  labs(title="Diverging Lollipop Chart", 
       subtitle="Normalized mileage from 'mtcars': Lollipop") + 
  ylim(-2.5, 2.5) +
  coord_flip() 

但我无法在点内输入数字并出现错误:

Warning: Ignoring unknown aesthetics: label

剧情来了。先感谢您!!!

【问题讨论】:

  • labelgeom_text 而不是 geom_point 的美学。
  • @dorayin 如果您找到了问题的解决方案,请接受其中一个答案 (stackoverflow.com/help/someone-answers),以便该线程不再处于活动状态。跨度>

标签: r


【解决方案1】:

这是我个人认为更有吸引力的另一种方法(它还避免了附近点的重叠标签问题):

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.4.3
library(ggrepel)
#> Warning: package 'ggrepel' was built under R version 3.4.2
theme_set(theme_bw())

mtcars$`car name` <- rownames(mtcars)
mtcars$mpg_z <-
  round((mtcars$mpg - mean(mtcars$mpg)) / sd(mtcars$mpg), 2)
mtcars$mpg_type <- ifelse(mtcars$mpg_z < 0, "below", "above")
mtcars <- mtcars[order(mtcars$mpg_z),]  # sort
mtcars$`car name` <-
  factor(mtcars$`car name`, levels = mtcars$`car name`)

ggplot() +
  geom_point(
    data = mtcars,
    mapping = aes(x = `car name`, y = mpg_z),
    stat = 'identity',
    fill = "black",
    size = 6
  )  +
  geom_text(color = "white", size = 2) +
  labs(title = "Diverging Lollipop Chart",
       subtitle = "Normalized mileage from 'mtcars': Lollipop") +
  ylim(-2.5, 2.5) +
  coord_flip() +
  ggrepel::geom_label_repel(data = mtcars,
                            mapping = aes(x = `car name`, y = mpg_z, label = mpg_z))

reprex package (v0.1.1.9000) 于 2018 年 2 月 13 日创建。

【讨论】:

  • 谢谢!有没有办法在点内输入数字?
【解决方案2】:

只需将您的 aes 呼叫转移到 ggplot

ggplot(data=mtcars, aes(x=`car name`, y=mpg_z,label=mpg_z)) + 
  geom_point(stat='identity', fill="black", size=6)  +
  geom_text(color="white", size=2) +
  labs(title="Diverging Lollipop Chart", 
       subtitle="Normalized mileage from 'mtcars': Lollipop") + 
  ylim(-2.5, 2.5) +
  coord_flip() 

geom_pointgeom_text 将继承相关的美学。

【讨论】:

  • 谢谢!我知道这种方式,但我想为 geom_bars 和其他功能(我没有在这里包括)使用不同的数据集。所以我需要保持 ggplot() 为空。
  • 可以将数据移动到相关的geom中,将aes留在ggplot中,也可以将aes也移动,但是label在geom_point中没有位置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2018-03-18
  • 1970-01-01
  • 2018-01-26
  • 2011-01-25
  • 1970-01-01
相关资源
最近更新 更多