【问题标题】:ggplot2: Label not showing with the geom_text() functionggplot2:使用 geom_text() 函数未显示标签
【发布时间】:2021-03-15 06:01:45
【问题描述】:

我有一个数据框,并基于它制作了一个散点图(使用 ggplot2)。像这样的:

df = data.frame(x = letters, y = c(1:26))
p = ggplot(df, aes(x = x, y = y)) + geom_point()

但我想在某个坐标中添加一个特定的标签(使用 geom_text)。问题是标签没有显示在打印的图中。

p + geom_text(x = 3, y = 30, label = "Scatter plot")

Plot without printed label

仅当我想使用坐标时才会出现此特定错误,因为如果我删除坐标,则打印带有标签的绘图(尽管它出现在绘图的每个点中)。

我写错了吗?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    ylim 刚好低于该范围,即它基于数据中提供的值,即 1 到 26。可以通过指定自定义 ylim 层来修改它

    library(ggplot2)
    p <- ggplot(df, aes(x = x, y = y)) + 
               geom_point() + 
               ylim(c(1, 35))
    p + 
         geom_text(x = 3, y = 30, label = "Scatter plot")
    

    -输出

    【讨论】:

    • 好的,非常感谢!这是一个非常有用的答案
    猜你喜欢
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多