【问题标题】:geom_rect - different data frames with different appearancesgeom_rect - 具有不同外观的不同数据框
【发布时间】:2017-05-19 09:50:47
【问题描述】:

我的问题:我试图对坐标系的一部分进行着色,但主要的 网格线 仍然可见(如 img 1 所示)。为此我使用geom_rect(因为没有更好的选择)。我的代码适用于测试数据集,如第一个代码 sn-p 所示。

当我用另一个 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

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

根据@beetroot 的回答编辑#2 Beetroot 找到了解决与行数相关的部分问题的解决方案。不幸的是,试图将我的数据集“插入”到甜菜根的代码中会产生以下错误:

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

一定是technicalsHttest在某些方面有所不同?

【问题讨论】:

  • 另一个选项(除了不全局设置数据集)是使用annotate。见答案here
  • 就警告消息而言,我认为这与矩形无关,如果您尝试将非基于数字的字符向量转换为数字,您将得到所有 @ 987654338@。这就是 as.numeric(as.character(name)) 在您的代码中所做的。您可以使用as.numeric(as.factor(name)) 取回整数。
  • @aosmith as.numeric(as.factor()) 做到了!

标签: r ggplot2 boxplot


【解决方案1】:

原因是每行数据都绘制了一个geom_rect(我相信),并且由于您的第二个数据框有更多行,因此由于在顶部绘制了更多geom_rects,因此该区域变得更暗彼此。

例如,看看这个情节:

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 以提高可见性):

【讨论】:

  • 非常感谢您的回答。我认为您说数据集中的行数不同是有道理的。谢谢。像您一样将数据放入 geom_boxplot 会导致根本没有阴影(请参阅您上传的第二张图片)!!
  • 再次非常感谢您。您的解决方案适用于测试数据集......但不适用于我最初使用的那个。我相应地编辑了我的问题,并上传了我正在使用的数据的简化版本。当然,我们正在取得进展,但部分故事似乎仍然缺失。谢谢
猜你喜欢
  • 2021-07-13
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多