在箱形图上使用 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
中使用它。使用这种方法,您可以将 x
分配给 geom_label
,以便点和标签对齐。
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=...)?
我在箱形图上叠加数据点。我遇到的问题是在尝试标记数据点时,标签未显示在数据点上。在这里的任何帮助表示赞赏。谢谢
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
中使用它。使用这种方法,您可以将 x
分配给 geom_label
,以便点和标签对齐。
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=...)?