geom_rect - 不同的数据帧 - >不同的表现?

时间:2017-05-19 09:50:47

标签: r ggplot2 boxplot

我的问题:我试图让部分坐标系统被遮挡,但主要的网格线仍然可见(如img 1)。为此我使用data.frame (因为没有更好的选择)。我有与测试数据集一起使用的代码,可以在第一个代码片段中看到。

当我尝试与另一个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() 相同的代码时,我无法分享,因为我使用长程序和一些API构建它(请参阅下面的说明it),我没有得到相同的(预期的)结果:阴影更暗,更重要的是,它覆盖了网格线。

代码段1:

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)

Img 1 - 好:

enter image description here

代码段2:

> 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

Img2 - 糟糕:

enter image description here

这怎么可能?我该如何解决这个问题?

数据集的比较:

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

在@ beetroot的回答后编辑#1 我的数据集有更多行的事实似乎有所不同:行越多,阴影越暗。但问题仍然存在:如何在处理我的数据集时确保第一张图像中的阴影?

在@ beetroot的回答后编辑#2 甜菜根找到了一个解决方案来解决与行数相关的问题部分。不幸的是,试图插入&#34;我的数据设置为甜菜根的代码会产生以下错误:

technicalsHt

test在{{1}}的某些方面有所不同......

enter image description here

1 个答案:

答案 0 :(得分:2)

原因是为每行数据绘制了一个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()

enter image description here

如果您将数据参数从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): enter image description here

test2的情节(可见度将alpha增加到0.5):

enter image description here