geom_rect - 具有不同外观的不同数据框
geom_rect - different data frames with different appearances
我的问题:我正在尝试对坐标系的一部分进行着色,但主要的 网格线 仍然可见(如 img 1 中所示)。为此 我使用 geom_rect
(因为没有更好的选择)。我有适用于测试数据集的代码,如第一个代码片段所示。
当我用另一个 data.frame
尝试相同的代码时,我不能分享它,因为我使用一个长程序和一些 API 构建它(见下文它的描述),我没有得到相同的(预期的)结果:阴影更暗,更重要的是,它覆盖了网格线。
片段 1:
test<-data.frame(a=c('1','1','2','2'),b=c(2,-2,4,-3),d=c('m','n','m','n'))
ggplot(data=test,aes(x=a,y=b)) +
geom_rect(fill = 'grey', xmin = -Inf, xmax = Inf, ymin =-Inf, ymax = 0, alpha =0.05) +
geom_boxplot()
图片 1 - 好:
片段 2:
ggplot(data = technicalsHt, aes(x = name, y = px_last)) +
geom_rect(fill = 'grey', xmin = -Inf, xmax = Inf, ymin =-Inf, ymax = 0, alpha =0.05) +
geom_boxplot(outlier.shape=NA)
Img2 - 错误:
如何解决?
数据集对比:
> str(test)
'data.frame': 4 obs. of 3 variables:
$ a: Factor w/ 2 levels "1","2": 1 1 2 2
$ b: num 2 -2 4 -3
$ d: Factor w/ 2 levels "m","n": 1 2 1 2
> str(technicalsHt)
'data.frame': 36 obs. of 3 variables:
$ date : Date, format: "2017-05-08" "2017-05-09" ...
$ px_last: num 0.827 0.943 0.652 -0.242 -0.475 ...
$ name : Factor w/ 4 levels "Stock Price Strength",..: 1 1 1 1 1 1 1 1 1 2 ...
> technicalsHt
date px_last name
1 2017-05-08 0.82662887 Stock Price Strength
2 2017-05-09 0.94317706 Stock Price Strength
3 2017-05-10 0.65180657 Stock Price Strength
4 2017-05-11 -0.24172959 Stock Price Strength
5 2017-05-12 -0.47482598 Stock Price Strength
6 2017-05-15 0.67123127 Stock Price Strength
7 2017-05-16 0.71008067 Stock Price Strength
8 2017-05-17 -1.56260914 Stock Price Strength
9 2017-05-18 -1.52375974 Stock Price Strength
10 2017-05-08 0.45763568 Junk Bond Demand*
11 2017-05-09 -0.22417964 Junk Bond Demand*
12 2017-05-10 -0.86425117 Junk Bond Demand*
13 2017-05-11 -0.87816577 Junk Bond Demand*
14 2017-05-12 -0.14069205 Junk Bond Demand*
15 2017-05-15 -0.89208036 Junk Bond Demand*
16 2017-05-16 -0.61378840 Junk Bond Demand*
17 2017-05-17 1.41774297 Junk Bond Demand*
18 2017-05-18 1.73777873 Junk Bond Demand*
19 2017-05-08 1.25714740 Stock Price Breadth
20 2017-05-09 0.86192921 Stock Price Breadth
21 2017-05-10 0.81957857 Stock Price Breadth
22 2017-05-11 0.42779421 Stock Price Breadth
23 2017-05-12 -0.12824197 Stock Price Breadth
24 2017-05-15 -0.06365315 Stock Price Breadth
25 2017-05-16 -0.19438420 Stock Price Breadth
26 2017-05-17 -1.08824445 Stock Price Breadth
27 2017-05-18 -1.89192563 Stock Price Breadth
28 2017-05-08 0.85639356 120D Momentum
29 2017-05-09 0.63138711 120D Momentum
30 2017-05-10 0.67208965 120D Momentum
31 2017-05-11 0.31738619 120D Momentum
32 2017-05-12 0.05165838 120D Momentum
33 2017-05-15 0.52908486 120D Momentum
34 2017-05-16 0.35874200 120D Momentum
35 2017-05-17 -1.89159826 120D Momentum
36 2017-05-18 -1.52514351 120D Momentum
> head(technicalsHt)
date px_last name
1 2016-11-14 -2.278607 120D Momentum
2 2016-11-15 -1.754333 120D Momentum
3 2016-11-16 -1.893738 120D Momentum
4 2016-11-17 -1.574128 120D Momentum
5 2016-11-18 -1.774994 120D Momentum
6 2016-11-21 -1.249234 120D Momentum
> head(test)
a b d
1 1 2 m
2 1 -2 n
3 2 4 m
4 2 -3 n
编辑#1 遵循@beetroot 的回答
我的数据集有更多行这一事实似乎有所不同:行越多,阴影越深。但问题仍然存在:如何在处理我的数据集时确保第一张图像中的阴影?
编辑#2 遵循@beetroot 的回答
Beetroot 找到了解决与行数相关的部分问题的解决方案。不幸的是,试图 "plug in" 我的数据集到甜菜根的代码中会产生以下错误:
ggplot() +
geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 0), alpha = 0.5, fill = "grey") +
geom_boxplot(data = technicalsHt, aes(x = as.numeric(as.character(name)), y = px_last, group = name)) +
scale_x_continuous(breaks = c(1,2,3,4))
Warning messages:
1: In eval(expr, envir, enclos) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
4: In min(diff(sort(x))) : no non-missing arguments to min; returning Inf
5: Removed 36 rows containing non-finite values (stat_boxplot).
一定是 technicalsHt
在某些方面与 test
不同?
原因是为每一行数据绘制了一个 geom_rect
(我相信),并且由于您的第二个数据框有更多行,因此区域变得更暗,因为更多 geom_rect
s 被绘制在彼此之上。
例如,看这个情节:
test2 <- rbind(test, test, test)
ggplot(data = test2, aes(x = a, y = b)) +
geom_rect(fill = 'grey', xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 0, alpha = 0.05) +
geom_boxplot()
如果将数据参数从 ggplot()
移动到 geom_boxplot()
并将值放在 aes()
中,则可以避免这种情况(但是,因为 geom_rect()
具有连续刻度我不得不转换 a
,这可能不太理想):
ggplot() +
geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 0), alpha = 0.5, fill = "grey") +
geom_boxplot(data = test, aes(x = as.numeric(as.character(a)), y = b, group = a)) +
scale_x_continuous(breaks = c(1,2))
test
的绘图(将 alpha 增加到 0.5 以提高可见性):
test2
的绘图(为了可见性将 alpha 增加到 0.5):
我的问题:我正在尝试对坐标系的一部分进行着色,但主要的 网格线 仍然可见(如 img 1 中所示)。为此 我使用 geom_rect
(因为没有更好的选择)。我有适用于测试数据集的代码,如第一个代码片段所示。
当我用另一个 data.frame
尝试相同的代码时,我不能分享它,因为我使用一个长程序和一些 API 构建它(见下文它的描述),我没有得到相同的(预期的)结果:阴影更暗,更重要的是,它覆盖了网格线。
片段 1:
test<-data.frame(a=c('1','1','2','2'),b=c(2,-2,4,-3),d=c('m','n','m','n'))
ggplot(data=test,aes(x=a,y=b)) +
geom_rect(fill = 'grey', xmin = -Inf, xmax = Inf, ymin =-Inf, ymax = 0, alpha =0.05) +
geom_boxplot()
图片 1 - 好:
片段 2:
ggplot(data = technicalsHt, aes(x = name, y = px_last)) +
geom_rect(fill = 'grey', xmin = -Inf, xmax = Inf, ymin =-Inf, ymax = 0, alpha =0.05) +
geom_boxplot(outlier.shape=NA)
Img2 - 错误:
如何解决?
数据集对比:
> str(test)
'data.frame': 4 obs. of 3 variables:
$ a: Factor w/ 2 levels "1","2": 1 1 2 2
$ b: num 2 -2 4 -3
$ d: Factor w/ 2 levels "m","n": 1 2 1 2
> str(technicalsHt)
'data.frame': 36 obs. of 3 variables:
$ date : Date, format: "2017-05-08" "2017-05-09" ...
$ px_last: num 0.827 0.943 0.652 -0.242 -0.475 ...
$ name : Factor w/ 4 levels "Stock Price Strength",..: 1 1 1 1 1 1 1 1 1 2 ...
> technicalsHt
date px_last name
1 2017-05-08 0.82662887 Stock Price Strength
2 2017-05-09 0.94317706 Stock Price Strength
3 2017-05-10 0.65180657 Stock Price Strength
4 2017-05-11 -0.24172959 Stock Price Strength
5 2017-05-12 -0.47482598 Stock Price Strength
6 2017-05-15 0.67123127 Stock Price Strength
7 2017-05-16 0.71008067 Stock Price Strength
8 2017-05-17 -1.56260914 Stock Price Strength
9 2017-05-18 -1.52375974 Stock Price Strength
10 2017-05-08 0.45763568 Junk Bond Demand*
11 2017-05-09 -0.22417964 Junk Bond Demand*
12 2017-05-10 -0.86425117 Junk Bond Demand*
13 2017-05-11 -0.87816577 Junk Bond Demand*
14 2017-05-12 -0.14069205 Junk Bond Demand*
15 2017-05-15 -0.89208036 Junk Bond Demand*
16 2017-05-16 -0.61378840 Junk Bond Demand*
17 2017-05-17 1.41774297 Junk Bond Demand*
18 2017-05-18 1.73777873 Junk Bond Demand*
19 2017-05-08 1.25714740 Stock Price Breadth
20 2017-05-09 0.86192921 Stock Price Breadth
21 2017-05-10 0.81957857 Stock Price Breadth
22 2017-05-11 0.42779421 Stock Price Breadth
23 2017-05-12 -0.12824197 Stock Price Breadth
24 2017-05-15 -0.06365315 Stock Price Breadth
25 2017-05-16 -0.19438420 Stock Price Breadth
26 2017-05-17 -1.08824445 Stock Price Breadth
27 2017-05-18 -1.89192563 Stock Price Breadth
28 2017-05-08 0.85639356 120D Momentum
29 2017-05-09 0.63138711 120D Momentum
30 2017-05-10 0.67208965 120D Momentum
31 2017-05-11 0.31738619 120D Momentum
32 2017-05-12 0.05165838 120D Momentum
33 2017-05-15 0.52908486 120D Momentum
34 2017-05-16 0.35874200 120D Momentum
35 2017-05-17 -1.89159826 120D Momentum
36 2017-05-18 -1.52514351 120D Momentum
> head(technicalsHt)
date px_last name
1 2016-11-14 -2.278607 120D Momentum
2 2016-11-15 -1.754333 120D Momentum
3 2016-11-16 -1.893738 120D Momentum
4 2016-11-17 -1.574128 120D Momentum
5 2016-11-18 -1.774994 120D Momentum
6 2016-11-21 -1.249234 120D Momentum
> head(test)
a b d
1 1 2 m
2 1 -2 n
3 2 4 m
4 2 -3 n
编辑#1 遵循@beetroot 的回答 我的数据集有更多行这一事实似乎有所不同:行越多,阴影越深。但问题仍然存在:如何在处理我的数据集时确保第一张图像中的阴影?
编辑#2 遵循@beetroot 的回答 Beetroot 找到了解决与行数相关的部分问题的解决方案。不幸的是,试图 "plug in" 我的数据集到甜菜根的代码中会产生以下错误:
ggplot() +
geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 0), alpha = 0.5, fill = "grey") +
geom_boxplot(data = technicalsHt, aes(x = as.numeric(as.character(name)), y = px_last, group = name)) +
scale_x_continuous(breaks = c(1,2,3,4))
Warning messages:
1: In eval(expr, envir, enclos) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
4: In min(diff(sort(x))) : no non-missing arguments to min; returning Inf
5: Removed 36 rows containing non-finite values (stat_boxplot).
一定是 technicalsHt
在某些方面与 test
不同?
原因是为每一行数据绘制了一个 geom_rect
(我相信),并且由于您的第二个数据框有更多行,因此区域变得更暗,因为更多 geom_rect
s 被绘制在彼此之上。
例如,看这个情节:
test2 <- rbind(test, test, test)
ggplot(data = test2, aes(x = a, y = b)) +
geom_rect(fill = 'grey', xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 0, alpha = 0.05) +
geom_boxplot()
如果将数据参数从 ggplot()
移动到 geom_boxplot()
并将值放在 aes()
中,则可以避免这种情况(但是,因为 geom_rect()
具有连续刻度我不得不转换 a
,这可能不太理想):
ggplot() +
geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 0), alpha = 0.5, fill = "grey") +
geom_boxplot(data = test, aes(x = as.numeric(as.character(a)), y = b, group = a)) +
scale_x_continuous(breaks = c(1,2))
test
的绘图(将 alpha 增加到 0.5 以提高可见性):
test2
的绘图(为了可见性将 alpha 增加到 0.5):