簡體   English   中英

在箱線圖上使用 geom_jitter 疊加時標記數據點

[英]label datapoints when using geom_jitter overlaying on a box plot

我在箱線圖上疊加數據點。 我遇到的問題是在嘗試標記數據點時,標簽未顯示在數據點上。 任何幫助在這里表示贊賞。 謝謝

df <- data.frame(sno= c(1:10), A =sample(1:1000, 10), B=sample(1:100, 10), C=sample(1:300, 10))
df <- melt(df, id.vars = "sno")

library(ggplot2)
ggplot(df, aes(x="", y=value, label=sno)) + 
    facet_wrap(~variable, scales = "free") +
    geom_boxplot()+
    geom_jitter(position=position_jitter(0.2), col="blue") +
    geom_text(aes(label=sno))

您可以使用來自base jitter來制作您自己的數據集,然后在geom_point使用它。 使用這種方法,您可以為geom_label分配x ,以便點和標簽對齊。

df <- data.frame(sno= c(1:10), A =sample(1:1000, 10), 
                               B=sample(1:100, 10), C=sample(1:300, 10))
df <- reshape2::melt(df, id.vars = "sno")

dfj <- df
dfj$xj <- jitter(as.numeric(factor(df$variable)))

library(ggplot2)

ggplot(df, aes(x=as.numeric(variable), y=value, group=NULL)) + 
  facet_wrap(~variable, scales = "free") +
  geom_boxplot()+
  geom_point(data = dfj, aes(x=xj), col="blue") +
  geom_text(data = dfj, aes(x=xj,label=sno, hjust=2)) + 
  scale_x_continuous(breaks = as.numeric(df$variable))+
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())
#> Warning: Continuous x aesthetic -- did you forget aes(group=...)?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM