R 根据ggplot中的条件打印行名

R 根据ggplot中的条件打印行名,r,ggplot2,R,Ggplot2,我有这样一个数据集: df <- data.frame(v1 = rnorm(10), col = rbinom(10, size=1,prob= 0.5)) rownames(df) <- letters[1:10] > head(df) v1 col a -0.1806868 1 b 0.6934783 0 c -0.4658297 1 d 1.6760829 0 e -0.8475840 0 f -1.3499387 1

我有这样一个数据集:

df <- data.frame(v1 = rnorm(10), col = rbinom(10, size=1,prob= 0.5))
rownames(df) <- letters[1:10]

> head(df)
          v1 col
a -0.1806868   1
b  0.6934783   0
c -0.4658297   1
d  1.6760829   0
e -0.8475840   0
f -1.3499387   1
ggplot(df, aes(x = v1, y=rownames(df), group = col, color= col)) + geom_point()
现在我只想显示y轴上col==1的行名。 不应显示其他名称,但应显示点

为了添加一些上下文,我在y轴上绘制了一个有许多重叠变量名称的绘图,但我只想显示虚线之外的变量名称

您可以使用scale_y_离散:

您可以使用scale_y_离散:


对于@MauritsEvers给出的答案,没有什么可以补充的,我只是有一个想法,对于你的情节来说,可能需要更少的水平线来引导你的眼睛

我们可以使用scale_y_离散中的breaks参数

set.seed(1); df <- data.frame(v1 = rnorm(10), col = rbinom(10, size=1,prob= 0.5))
rownames(df) <- letters[1:10]

axis_labels <- which(df$col == 1)
ggplot(df, aes(x = v1, y=rownames(df), group = col, color= col)) +
 geom_point() +
 scale_y_discrete(breaks = rownames(df)[axis_labels])

对于@MauritsEvers给出的答案,没有什么可以补充的,我只是有一个想法,对于你的情节来说,可能需要更少的水平线来引导你的眼睛

我们可以使用scale_y_离散中的breaks参数

set.seed(1); df <- data.frame(v1 = rnorm(10), col = rbinom(10, size=1,prob= 0.5))
rownames(df) <- letters[1:10]

axis_labels <- which(df$col == 1)
ggplot(df, aes(x = v1, y=rownames(df), group = col, color= col)) +
 geom_point() +
 scale_y_discrete(breaks = rownames(df)[axis_labels])