简体   繁体   English

R ggplot省略NA

[英]R ggplot omit NA

I'm trying to do ggplots in R. My dataset has a few rows with NA for one or more variables. 我正在尝试在R中进行ggplots。我的数据集有几行带有NA的一个或多个变量。 How do I get the NA not to show up on the graph? 如何获得不显示在图表上的NA? Here's my code: 这是我的代码:

met$marker_Degree2 = factor(met$marker_Degree, levels=c("none", "weak"))
p4 <- ggplot(met, aes(factor(marker_Degree2), avgtsh))
p4 + ggtitle('Serum marker and Tumor marker') +
geom_point(shape=21, size=4, aes(color=factor(marker_Degree2))) +
scale_color_manual(values = c("orange", "green")) +
theme_bw() +
xlab("Marker Tissue Staining Degree") +
ylab("Mean marker Level in Serum")

Here's an example 这是一个例子

mydata <- data.frame(income=c(50000,NA,10000,30000), y=c("male", "female", NA, "female"))
p <- ggplot(mydata, aes(x,z))
p + geom_point()

Note that NA for numeric() data are hidden automatically, but not for character() data, so the NA shows up. 请注意,numerical()数据的NA会自动隐藏,而character()数据则不会自动隐藏,因此会显示NA。 So you need to do something like this. 所以你需要做这样的事情。

plot_row <- apply(mydata, 1, function(x) sum(!is.na(x))) == ncol(mydata)
p <- ggplot(mydata[plot_row,], aes(x,z))
p + geom_point()

One way to handle the example you provided is as follows: 处理您提供的示例的一种方法如下:

mydata <- data.frame(income=c(50000,NA,10000,30000), y=c("male", "female", NA, "female"))
p <- ggplot(mydata, aes(income,y))
p + geom_point(na.rm = TRUE) + ylim(labels=c("male", "female"))

Alternatively, 或者,

use: 采用:

ggplot(na.omit(mydata), aes(income,y))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM