不要在R中使用ggplot包显示”na”值。

huangapple go评论90阅读模式
英文:

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")

不要在R中使用ggplot包显示”na”值。

英文:

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")

不要在R中使用ggplot包显示”na”值。

答案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")

不要在R中使用ggplot包显示”na”值。<!-- -->

And a second option would of course be to simply to drop the observations with missing values:

ggplot(subset(dolu, !is.na(value)), aes(x = month, y = year, color = value)) +
  geom_point() +
  scale_x_continuous(
    breaks = 1:12,
    labels = c(&quot;Ocak&quot;, &quot;Şubat&quot;, &quot;Mart&quot;, &quot;Nisan&quot;, &quot;Mayıs&quot;, &quot;Haziran&quot;, &quot;Temmuz&quot;, &quot;Ağustos&quot;, &quot;Eyl&#252;l&quot;, &quot;Ekim&quot;, &quot;Kasım&quot;, &quot;Aralık&quot;)
  ) +
  theme_classic() +
  labs(x = &quot;Ay&quot;, y = &quot;Yıl&quot;)

huangapple
  • 本文由 发表于 2023年3月21日 01:07:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定