首页 > 解决方案 > geom_col 呈现不可见的图

问题描述

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

# 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

在此处输入图像描述

我的研究

我现在明白这是由条形宽度为零引起的(请参阅此已关闭问题),如下所示:

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

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

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

在此处输入图像描述

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

在此处输入图像描述

    # 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")

根据这篇文章,我尝试添加+scale_x_continuous(oob = scales::rescale_none) AND / OR +scale_y_continuous(oob = scales::rescale_none),但它们都不起作用(既不能加厚细条,也不能减薄粗条)。

我怎样才能解决这个问题并有一个可以处理a产生看起来相似的输出的调用?(关于酒吧的外观)b

标签: rggplot2

解决方案


条形图在概念上不能在连续的 x 尺度上工作。但是,您可以使用不同的几何图形:

+ geom_segment(aes(xend = valor, yend = 0))

推荐阅读