r - 不要删除 ggplot 中的 na 值

标签 r ggplot2 na

我有一个数据框df

df<-structure(list(y = c(2268.14043972082, 2147.62290922552, 2269.1387550775, 
2247.31983098201, 1903.39138268307, 2174.78291538358, 2359.51909126411, 
2488.39004804939, 212.851575751527, 461.398994384333, 567.150629704352, 
781.775113821961, 918.303706148872, 1107.37695799186, 1160.80594193377, 
1412.61328924168, 1689.48879626486, 260.737164468854, 306.72700499362, 
283.410379620422, 366.813913489692, 387.570173754128, 388.602676983443, 
477.858510450125, 128.198042456082, 535.519377609133, 1028.8780498564, 
1098.54431357711, 1265.26965941035, 1129.58344809909, 820.922447928053, 
749.343583476846, 779.678206156474, 646.575242339517, 733.953282899613, 
461.156280127354, 906.813018662913, 798.186995701282, 831.365377249207, 
764.519073183124, 672.076289062505, 669.879217186302, 1341.47673353751, 
1401.44881976186, 1640.27575962036), P = c(1750.51986303926, 
1614.11541634798, 951.847023338079, 1119.3682884872, 1112.38984390156, 
1270.65773075982, 1234.72262170166, 1338.46096616983, 1198.95775346458, 
1136.69287367165, 1265.46480803983, 1364.70149818063, 1112.37006707489, 
1346.49240261316, 1740.56677791104, 1410.99217295647, 1693.18871380948, 
901.760176040232, 763.971046562772, 994.8699095021, 972.755147593882, 
1011.41669411398, 643.705302958842, 537.54384616157, 591.212003320456, 
464.405641604215, NA, NA, NA, NA, NA, 246.197052353527, 265.237238116562, 
1260.68540103734, 398.080919081345, NA, 374.611004248261, 527.686996757984, 
765.678002953529, 661.830007851124, 484.864011824131, 612.936006393284, 
672.088483441621, 625.397994920611, 785.390003710985), x = c(49, 
50, 51, 52, 53, 54, 55, 56, 1, 2, 3, 4, 5, 14, 15, 16, 17, 2, 
3, 4, 5, 6, 10, 11, 3, 30, 64, 66, 67, 68, 69, 34, 35, 37, 39, 
2, 17, 18, 99, 100, 102, 103, 67, 70, 72), Te = c(9.10006221322572, 
7.65505467142961, 8.21480062559674, 8.09251754304318, 8.466220758789, 
8.48094407814006, 8.77304120569444, 8.31727518543397, 8.14410265791868, 
8.80921738865237, 9.04091478341757, 9.66233618146246, 8.77015716015164, 
9.46037931956657, 9.59702379240667, 10.1739258740118, 9.39524442215692, 
0.616491147620629, 0.631476354027448, 0.129135682201776, 1.87297579763483, 
2.53941370394744, -0.518422834982312, 1.34466852591922, 1.05118063842584, 
1.79515935418336, 6.65555189859301, 6.38647333946436, 6.48427760143803, 
6.33587322941382, 7.3501870975794, 3.04366850755114, 1.94656549866681, 
3.61970174782833, 4.41939287295897, 9.47923019665153, 8.87480698915229, 
8.06993784515111, 2.31976094801132, 2.65270969716837, 2.21586561444892, 
2.66012522311856, 5.72634186318572, 4.63445162539409, 6.18505171198551
)), .Names = c("y", "P", "x", "Te"), row.names = c(NA, -45L), class = "data.frame")

我使用 ggplot 绘制这个 y 与 x 的关系图,其中点的大小和颜色分别基于 T 和 P。

ggplot(df) +
  geom_point(aes(x = x, y = y, size=P, colour=Te), alpha=0.7) +
  scale_colour_gradient(low="#00FF33", high ="#FF0000", guide = "colourbar")+
  labs(colour="T", size="P")+
  xlab("x") + ylab("y")+ 
  scale_size(range = c(3, 8)) +
  theme_bw(base_size = 12, base_family = "Helvetica") + 
  theme(panel.grid.minor = element_line(colour="grey", size=0.5),
        axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position="bottom",
        legend.box="horizontal") +
  guides(colour = guide_colourbar(title.position="top", title.hjust = 0.5),
         size = guide_legend(title.position="top", title.hjust = 0.5))

一些点被删除了,因为我在 P 中有一些 NA 值。但是,我想保留这些点并给它们一个灰色。

有人知道怎么做吗?

最佳答案

一个解决方案是使用两个数据子集——第一个调用 geom_point仅绘制 df$P 所在的数据点不是 NA .第二次调用 geom_point绘制点 df$PNA因此删除了 size = P审美。

ggplot(df) +
  geom_point(data = subset(df, !is.na(P)), aes(x = x, y = y, size=P, colour=Te), alpha=0.7) +
  geom_point(data = subset(df, is.na(P)), aes(x = x, y = y, color = Te), alpha = 0.7, color = "black") +
  scale_colour_gradient(low="#00FF33", high ="#FF0000", guide = "colourbar")+
  labs(colour="T", size="P")+
  xlab("x") + ylab("y")+ 
  scale_size(range = c(3, 8)) +
  theme_bw(base_size = 12, base_family = "Helvetica") + 
  theme(panel.grid.minor = element_line(colour="grey", size=0.5),
        axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position="bottom",
        legend.box="horizontal") +
  guides(colour = guide_colourbar(title.position="top", title.hjust = 0.5),
         size = guide_legend(title.position="top", title.hjust = 0.5))

注意:您可以通过在第一个 x 中传递“共享”美学(即 yalpha )和参数(即 ggplot )来删除一些重复代码。称呼。您还可以使用 labs 减少一些重复, xlabs , 和 ylabs通过将所有内容放入 labs() .最后,gradient = "colourbar"scale_colour_gradient 的默认值的 guide参数,所以它也可以被删除:

ggplot(df, aes(x = x, y = y), alpha = 0.7) +
  geom_point(data = subset(df, !is.na(P)), aes(size = P, color = Te)) +
  geom_point(data = subset(df, is.na(P)), color = "black") +
  scale_colour_gradient(low = "#00FF33", high = "#FF0000") +
  labs(x = "x", y = "y", colour = "T", size = "P") +
  scale_size(range = c(3, 8)) +
  theme_bw(base_size = 12, base_family = "Helvetica") + 
  theme(panel.grid.minor = element_line(colour = "grey", size = 0.5),
             axis.text.x = element_text(angle = 45, hjust = 1),
         legend.position = "bottom",
              legend.box = "horizontal") +
  guides(colour = guide_colourbar(title.position = "top", title.hjust = 0.5),
           size = guide_legend(title.position = "top", title.hjust = 0.5))

关于r - 不要删除 ggplot 中的 na 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33501519/

相关文章:

r - `testthat::expect_silent()` 似乎没有注意到 ggplot2 错误

r - 如何在简单的 ggplot2 散点图中干净地标记点?

r - geom_sf 映射点/形状

r - is.na() 应用于 'NULL' 类型的非(列表或向量)是什么意思?

r - 如何在 R 中找到具有值(每行)的最后一列?

r - 在 R 中生成概率分布函数 (PDF) 的问题

r - 如何在 R 中自动制作列表列表

r - 在 R 中使用 str_split() 后获取第二项

如果日期低于 R 中特定行的日期,则返回 NA

r - 第一页和其他页的标题