我有一个名为mydf
的数据框。我试图绘制如下所示的数据,但我想仅标记那些污染超过1.2的样本(而不是一切)。我还想在1.2污染边缘添加水平线。我如何在R中执行此操作?
mydf <- structure(list(sample.names = structure(c(2L, 3L, 4L, 5L, 6L,
1L, 7L, 8L, 9L, 10L), .Label = c("LPH-001-1", "LPH-001-10", "LPH-001-10_AK1",
"LPH-001-10_AK2", "LPH-001-10_PD", "LPH-001-10_SCC", "LPH-001-13",
"LPH-001-13_AK1", "LPH-001-13_AK2", "LPH-001-13_PD"), class = "factor"),
contamination = structure(c(5L, 1L, 4L, 2L, 2L, 4L, 3L, 8L,
7L, 6L), .Label = c("0.7", "1.0", "1.1", "1.2", "1.3", "1.4",
"1.7", "2.0"), class = "factor")), .Names = c("sample.names",
"contamination"), row.names = c(NA, -10L), class = "data.frame")
cc<- ggplot(mydf, aes(x=sample.names, y=contamination, label= mydf[,"sample.names"])) + geom_point()
cc + geom_text()
答案 0 :(得分:2)
我会将sample.names
和contamination
分别转换为字符和数字向量,然后创建一个新的名称向量,其中包含带contamination <= 1.2
的样本的占位符。 geom_hline
可以添加水平线。
mydf$contamination <- as.numeric(as.character(mydf$contamination))
mydf$sample.names <- as.character(mydf$sample.names)
mydf$sample.names1.2 <- ifelse(mydf$contamination > 1.2, mydf$sample.names, "")
ggplot(mydf, aes(x=sample.names, y=contamination, label = sample.names1.2)) +
geom_point() +
geom_text() +
geom_hline(yintercept = 1.2)