标记每个数据点

时间:2019-12-06 19:38:34

标签: r ggplot2

我有这样的数据

df<-structure(list(X = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 10L, 
9L, 11L, 12L, 8L), .Label = c("A", "B", "C", "D", "E", "F", "GG", 
"IR", "MM", "TT", "TTA", "UD"), class = "factor"), X_t = c(3.7066, 
3.6373, 3.2693, 2.5626, 2.4144, 2.2868, 2.1238, 1.8671, 1.7627, 
1.4636, 1.4195, 1.0159), NEW = structure(c(8L, 7L, 9L, 1L, 2L, 
3L, 4L, 5L, 6L, 10L, 11L, 12L), .Label = c("12-Jan", "14-Jan", 
"16-Jan", "19-Jan", "25-Jan", "28-Jan", "4-Jan", "Feb-38", "Feb-48", 
"Jan-39", "Jan-41", "Jan-66"), class = "factor")), class = "data.frame", row.names = c(NA, 
-12L))

我正在尝试为每个点贴标签,但收到警告

这是我的绘制方式

ggplot(data=df)+
  geom_point(aes(X_t, X,size =X_t,colour =X_t,label = NEW))

我也想将两个图例合并为一个,因为它是多余的,如果您有任何提示请告诉我

1 个答案:

答案 0 :(得分:1)

geom_text用于文本(例如标签):

ggplot(data=df, aes(X_t, X)) +
  geom_point(aes(size = X_t, colour = X_t)) +
  geom_text(aes(label = NEW), nudge_y = 0.5) +
  guides(color = guide_legend(), size = guide_legend())

enter image description here

您在ggplot()调用中指定的美学将被后续的图层(geom)继承。因此,通过将xy的美学元素放在ggplot()中,我们不必再次指定它们。

关于图例问题,请see this answer以获取详细信息。为了结合颜色和尺寸图例,我们使用guide_legend