英文:
Don't show na values in R with ggplot package
问题
我用ggplot在R中绘制了那个图表。在Excel文件的列中有很多NA。对于这个图,如何不显示NA值(灰色的)?
ggplot(dolu, aes(x=month, y=year, color=value)) +
geom_point() +
scale_x_continuous(breaks=1:12, labels=c("Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık")) +
theme_classic() +
labs(x="Ay", y="Yıl")
英文:
I plotted that graph in R with ggplot. In the excel file column there is so much NA. For this plot, how can I do not show NA values (Gray ones)?
ggplot(dolu, aes(x=month, y=year, color=value)) +
geom_point() +
scale_x_continuous(breaks=1:12, labels=c("Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık")) +
theme_classic() +
labs(x="Ay", y="Yıl")
答案1
得分: 1
以下是您要翻译的部分:
一种选择是将scale_color_continuous
中用于NA
值的颜色设置为"transparent"
。
使用一些假的随机示例数据:
library(ggplot2)
set.seed(123)
dolu <- data.frame(
month = rep(1:12, each = 21),
year = 2000:2020,
value = sample(c(1:6, NA), 21 * 12, replace = TRUE)
)
ggplot(dolu, aes(x = month, y = year, color = value)) +
geom_point() +
scale_x_continuous(
breaks = 1:12,
labels = c("Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık")
) +
scale_color_continuous(na.value = "transparent") +
theme_classic() +
labs(x = "Ay", y = "Yıl")
第二个选择当然是简单地删除带有缺失value
的观测值:
ggplot(subset(dolu, !is.na(value)), aes(x = month, y = year, color = value)) +
geom_point() +
scale_x_continuous(
breaks = 1:12,
labels = c("Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık")
) +
theme_classic() +
labs(x = "Ay", y = "Yıl")
英文:
One option would be to set the color used for the NA
values to "transparent"
in scale_color_continuous
.
Using some fake random example data:
library(ggplot2)
set.seed(123)
dolu <- data.frame(
month = rep(1:12, each = 21),
year = 2000:2020,
value = sample(c(1:6, NA), 21 * 12, replace = TRUE)
)
ggplot(dolu, aes(x = month, y = year, color = value)) +
geom_point() +
scale_x_continuous(
breaks = 1:12,
labels = c("Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık")
) +
scale_color_continuous(na.value = "transparent") +
theme_classic() +
labs(x = "Ay", y = "Yıl")
<!-- -->
And a second option would of course be to simply to drop the observations with missing value
s:
ggplot(subset(dolu, !is.na(value)), aes(x = month, y = year, color = value)) +
geom_point() +
scale_x_continuous(
breaks = 1:12,
labels = c("Ocak", "Şubat", "Mart", "Nisan", "Mayıs", "Haziran", "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık")
) +
theme_classic() +
labs(x = "Ay", y = "Yıl")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论