geom_col呈现不可见的图

时间:2019-08-01 13:28:44

标签: r ggplot2

在尝试生成错误图时,我发现了以下不良行为:

# sample data (please excuse the length, you'll see it's important!)

a <- structure(list(valor = c(22.35, 23.9, 32, 36.2, 35.2, 24.3, 42, 
36.4, 16.65, 40.95, 21, 33.2, 32, 33, 28.9, 28, 40.9, 28.4, 32.5, 
24.9, 28.35, 36.4, 31.05, 28.4, 37.9, 35.9, 24, 27.6, 28.6, 37.4, 
31.6, 31.9, 28.6, 33.9, 31.2, 27, 25.6, 31.2, 32.5, 26.4, 40, 
32.9, 32.9, 31.5, 24.9, 21.9, 33.4, 31.8, 31.1, 29.6, 31.5, 29.8, 
32.9, 26.6, 24.6, 35.9), error = c(-18.7573531872138, 1.31066637545209, 
NA, 0.277829536700779, -2.64925385673394, -11.8996081065239, 
-2.60692704590275, -1.33715023334453, NA, -7.61175343400322, 
2.55982080155896, 4.4863429357563, 4.16085789426681, -3.90087313834282, 
-1.8721045665811, 0.369086865146173, 12.2927002385953, -0.848796857979458, 
4.13045179906004, 4.28348313246773, 3.05347592474616, -5.33715023334453, 
-1.68395695575215, 5.15120314202054, -3.45360182568537, 0.700053120316895, 
4.50817359293553, 1.58628172614129, 7.54200618644399, 7.58601073994592, 
-6.61548902751109, -1.03317248113754, 3.54200618644399, 1.84047336001635, 
3.60755820405548, 1.41075911687027, 0.661540377840424, 6.60755820405548, 
-15.86954820094, 4.2336254711588, -15.4283737200925, 1.90546464068269, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, 
-56L), class = "data.frame")

此图有效:

p32 <- ggplot(a[1:32,], aes(x = valor, y = error))+geom_col(position = "jitter")
p32

p32

这不起作用:

p33 <- ggplot(a[1:33,], aes(x = valor, y = error))+geom_col(position = "jitter")
p33

enter image description here

我的研究

我现在了解到,这是由于条的宽度为零(请参见this closed issue),如此处所示:

# Notice xmin == xmax:
head(ggplot_build(p33)$data[[1]], 3)

      x          y PANEL group          ymin        ymax  xmin  xmax colour
1 22.35 -18.752212     1    -1 -1.875920e+01 0.002793391 22.35 22.35     NA
2 23.90   1.315615     1    -1 -3.452852e-05 1.304455085 23.90 23.90     NA
3 32.00         NA     1    -1            NA          NA 32.00 32.00     NA
    fill size linetype alpha
1 grey35  0.5        1    NA
2 grey35  0.5        1    NA
3 grey35  0.5        1    NA

我尝试过的事情:

我知道我可以手动设置width

ggplot(a[1:33,], aes(x = valor, y = error))+geom_col(position = "jitter", width = 0.1)

但是问题是我正在函数中使用ggplot调用 ,该函数以data.frame(在我的示例中为a)作为参数。显然,它可以具有任意长度,并且数据可能本质上是不同的,因此,手动修复width会创建一些带有非常细条的图,而另一些带有非常粗的图:

ggplot(a, aes(x = valor, y = error))+geom_col(position = "jitter", width = .1)

enter image description here

ggplot(b, aes(x = valor, y = error))+geom_col(position = "jitter", width = .1)

enter image description here

    # with b=
b <- structure(list(valor = c(1.03, 0.43, 1.25, 1.2, 0.74, 2.33, 1.49, 
1.5, 0.3, 0.96, 0.81, 1.13, 0.83, 0.68, 2.22, 0.68, 0.9, 1.03, 
0.39, 0.84, 1.4, 0.85, 0.7, 1.55, 1.08, 0.37, 0.66, 0.67, 1.36, 
0.97, 1.03, 0.64, 1, 0.78, 0.62, 0.5, 0.94, 0.56, 2.09, 1.01), 
    error = c(2.23998224289866, 0.224579421022632, -0.637159523178084, 
    -2.74850423807165, -2.69675570480791, 4.59843342442166, 2.34260767883423, 
    -12.4611961378406, 1.02484359455743, 2.46750883802447, 0.376157081501411, 
    -1.354215218894, 0.947671489701406, 0.426378012316505, 19.9389705823977, 
    -1.5736219876835, -22.1173385165668, 5.23998224289866, -0.540189922794198, 
    7.23019854807831, -3.46146029781903, -2.48937236945532, 5.06312219297025, 
    -1.49229963183367, -3.53967661036512, 0.534698732147042, 
    1.77779803536164, 6.10360322576836, 6.71339758402689, -5.27443362843563, 
    1.23998224289866, 1.11679330753741, -0.510113509024535, 0.502074779997471, 
    1.44272604499763, -0.91952618750328, -17.0537006712522, 3.33491106257746, 
    -8.09000221353266, 1.7414648468139)), row.names = c(NA, -40L
), class = "data.frame")

this post我尝试添加+scale_x_continuous(oob = scales::rescale_none) AND / OR +scale_y_continuous(oob = scales::rescale_none),但是它们都不起作用(既不能使细条变粗,也不能使细条变细)个)。

我该如何解决这个问题,并且有一个可以处理ab的呼叫,并且产生看起来相似的输出? (关于条形的外观)

1 个答案:

答案 0 :(得分:2)

从概念上讲,小节不能在连续的x刻度上工作。但是,您可以使用其他geom:

aws s3 cp s3://bucketname/file.csv - | sed 's/foo/bar/g' | aws s3 cp -  s3://bucketname/new-file.csv