【问题标题】:Wrapping geom_text / geom_label on ggplot (R) [duplicate]在ggplot(R)上包装geom_text / geom_label [重复]
【发布时间】:2021-06-19 22:36:28
【问题描述】:

我正在尝试找到一种将 geom_text/geom_label 包装在散点图 (ggplot) 上的方法。我已经看到可以手动输入坐标的方法 - 但我将有 10-20 个变量,所以这是不可能的。

我有一个看起来像这样的数据框...

df <- data.frame(x =c(2,4,6,8),
                 y =c(7,3,5,4),
                 label =c("this variable has a long name which needs to be shortened", 
                          "this variable has an even longer name that really needs to be shortened", 
                          "this variables has a name that is much longer than two of the other name, so really needs to be shortened", 
                          "this is pretty long too"))

我想制作以下情节(但带有包装标签)

ggplot(df, aes(x=x, y=y, label=label))+
  geom_point()+
  geom_text(nudge_y=0.05)+
  xlim(0,10)+
  theme_minimal()+
  ggtitle("title")

剧情如下:

【问题讨论】:

    标签: r ggplot2 geom-text


    【解决方案1】:

    你试过str_wrap 吗?根据您的实际数据,您可能需要使用width 参数和nudge_y

    library(ggplot2)
    
    df$label <- stringr::str_wrap(df$label, 25)
    
    ggplot(df, aes(x=x, y=y, label=label))+
      geom_point()+
      geom_text(nudge_y=0.5)+
      xlim(0,10)+
      theme_minimal()+
      ggtitle("title")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多