- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个数据框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$P
是NA
因此删除了 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
中传递“共享”美学(即 y
和 alpha
)和参数(即 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 Language Definition 中,对NA值进行了简要描述,其中一部分说 ... In particular, FALSE & NA is FALSE, TRUE | NA is TR
我对 R 还很陌生,目前遇到一个问题,数据如下所示: ID h1 h2 h3 h4 h5 h6 h7 h8
我有一个 csv包含国家名称及其 ISO 代码的文件。这是它的样子: "Name","Code" "Afghanistan","AF" "Albania","AL" "Algeria","DZ" "N
我想用 dplyr 解决以下问题。最好与窗口功能之一一起使用。我有一个包含房屋和购买价格的数据框。下面是一个例子: houseID year price 1 19
在data.frame(或data.table)中,我想用最接近的先前非NA值“向前填充”NA。一个使用向量(而不是 data.frame)的简单示例如下: > y yy [1] NA NA NA
这是一个示例数据框: > df = data.frame(rep(seq(0, 120, length.out=6), times = 2), c(sample(1:50, 4), + NA, NA,
我有一个包含条目的数据框;似乎这些值不被视为 NA,因为 is.na 返回 FALSE。我想将这些值转换为 NA 但找不到方法。 最佳答案 使用 dfr[dfr==""]=NA哪里dfr是你的数据框。
我有一个示例表,其中包含一些但不是全部 NA需要替换的值。 > dat id message index 1 1 1 2 1 foo 2 3 1
在 R 中,如果从 NA 中减去一个数字,它将返回 NA: > x NA - x [1] NA 但是如果你尝试从 NA 中减去一个日期,它会返回一个错误: > x NA - x Error in
这个问题在这里已经有了答案: Logical operators (AND, OR) with NA, TRUE and FALSE (2 个答案) 关闭 4 年前。 为什么在 R 中会这样? >
我有一个看起来像这样的数据框: SampleNo Lab1 Lab2 Lab3 lab4 lab5 lab6 lab7 lab8 lab9 lab10 1 59
我有一个按“id”分组的数据框和一个包含缺失值的变量“age”,NA。 在每个“id”中,我想替换“age”的缺失值,但只“填充”之前 第一个 非NA 值。 data % group_by(id) %
我有如下所示的数据框: df df id value v1 v2 v3 1 1 351 NA 1 0 2 2 585 0 1 1 3 3 321 NA 0 1 4
所以我有一个数据集,只需查看它,数据集中就有明显的 NA。 > dput(bmi.cig) structure(list(MSI.subset.BMI = structure(c(4L, 4L, 4
我有两个 30m x 30m 的光栅文件,我想从中采样点。在采样之前,我想从图像中移除模糊区域。我求助于 R 和 Hijman 的 Raster 包来完成这项任务。 使用 drawPoly(sp=TR
我有以下时间序列 > y y[c(1,2,5,9,10)] y [,1] 2011-09-04 NA 2011-09-05 NA 2011-09-06 3 201
这个问题在这里已经有了答案: Replace missing values (NA) with most recent non-NA by group (7 个回答) 5年前关闭。 我有一个 DF 个
我想向我的数据框中添加一个新变量 (N_notNAs),它定义了其他任何变量是否为 NA。 x y z N_notNAs 2 3 NA NA NA 1 3 NA 2
我有一个名为 SMOKE 的因子,级别为“Y”和“N”。缺失值被替换为 NA(从初始级别“NULL”开始)。然而,当我查看这个因素时,我得到这样的结果: head(SMOKE) # N N Y Y
假设我有以下 data.frame: t<-c(1,1,2,4,5,4) u<-c(1,3,4,5,4,2) v<-c(2,3,4,5,NA,2) w<-c(NA,3,4,5,2,3) x<-c(2,
我是一名优秀的程序员,十分优秀!