在 ggplot (R) 上包装 geom_text / geom_label
Wrapping geom_text / geom_label on ggplot (R)
我正在尝试找到一种将 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")
剧情是这样的:
你试过了吗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")
我正在尝试找到一种将 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")
剧情是这样的:
你试过了吗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")