R ggplot面中缺少2D密度层

R ggplot面中缺少2D密度层,r,ggplot2,R,Ggplot2,我试图生成一个分面的ggplot,其中包括一个点层和一个2D密度层,使用两个度量:价和能量。我可以创建一个没有我想要的方面的绘图,但是当我按艺术家的方面绘制时,其中一帧中缺少2D密度 以下是我的数据: head(df) #> artist valence energy song #> <chr> <dbl> <dbl> <int> #> 1 A 0.465 0.765 1 #> 2

我试图生成一个分面的ggplot,其中包括一个点层和一个2D密度层,使用两个度量:价和能量。我可以创建一个没有我想要的方面的绘图,但是当我按艺术家的方面绘制时,其中一帧中缺少2D密度

以下是我的数据:

head(df)
#>   artist valence energy  song
#>   <chr>    <dbl>  <dbl> <int>
#> 1 A        0.465  0.765     1
#> 2 A        0.407  0.841     2
#> 3 A        0.279  0.711     3
#> 4 A        0.398  0.302     4
#> 5 A        0.471  0.862     5
#> 6 A        0.387  0.843     6

我不明白为什么会收到这个错误消息,因为我的两个度量值由0和1之间的值组成,并且当我在没有刻面的情况下绘制这两个度量值时没有错误。我使用
stat\u density\u 2d()
stat\u contour()
代替
geom\u density\u 2d()
的其他变体也不起作用。我也尝试过扩展x和y的限制,但这也不能解决错误

数据:
df这对我有用

ggplot(df, aes(x = valence, y = energy)) + 
  geom_point() + 
  scale_x_continuous(limits = c(0, 1)) + 
  scale_y_continuous(limits = c(0, 1)) + 
  geom_hline(aes(yintercept = 0.5)) + 
  geom_vline(aes(xintercept = 0.5)) + 
  geom_density_2d() + facet_wrap(~artist, scales = 'free')

问题很简单,在ggplot v3.3.2中,2d密度在所有方面都以相同的方式进行组合。在第三个面,密度不够高,无法进入第二个面,因此没有等高线。通过将存储箱增加到50,可以看到这一点:

库(ggplot2)
ggplot(df,aes(x=价,y=能量))+
几何点()
标度x连续(极限=c(0,1))+
比例y连续(极限=c(0,1))+
geom_hline(aes(yintercept=0.5))+
geom_vline(aes(xintercept=0.5))+
统计密度2d(箱子=50)+
平面网格(~艺术家)

现在的问题是,第三个面板看起来不错,但前两个不行。您可以通过制作两个
stat\u density\u 2d
来修复此问题,其中一个具有条件alpha值:

ggplot(df,aes(x=化合价,y=能量))+
几何点()
标度x连续(极限=c(0,1))+
比例y连续(极限=c(0,1))+
geom_hline(aes(yintercept=0.5))+
几何线(aes(xintercept=0.5))+
统计密度2d()+
统计密度2d(aes(α=as.数字(as.因子(艺术家))%/%3),箱柜=50+
比例α单位()+
平面网格(~艺术家)


当然,垃圾箱不再在所有面板上显示相等的密度,但这些实际上是您的选择。

您使用的ggplot是什么版本的Reza?packageVersion(“ggplot2”)->“3.3.0”我使用的是ggplot2 3.3.2,这对我不起作用。同样,在这里,它不起作用。ggplot2 3.3.2。天平=‘自由’不应该改变我相信的任何事情。
p + facet_wrap(vars(artist))
#> Warning: stat_contour(): Zero contours were generated
#> Warning in min(x): no non-missing arguments to min; returning Inf
#> Warning in max(x): no non-missing arguments to max; returning -Inf
df <- structure(list(artist = c("A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B"), valence = c(0.341, 
0.387, 0.586, 0.598, 0.385, 0.465, 0.33, 0.646, 0.284, 0.381, 
0.487, 0.55, 0.173, 0.385, 0.35, 0.0424, 0.84, 0.447, 0.129, 
0.0391, 0.209, 0.35, 0.65, 0.505, 0.745, 0.434, 0.357, 0.258, 
0.836, 0.382), energy = c(0.851, 0.843, 0.6, 0.892, 0.857, 0.765, 
0.699, 0.755, 0.841, 0.914, 0.124, 0.857, 0.424, 0.379, 0.694, 
0.446, 0.954, 0.85, 0.229, 0.79, 0.893, 0.838, 0.855, 0.829, 
0.932, 0.907, 0.932, 0.764, 0.949, 0.906), song = c(1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L)), row.names = c(NA, 
-30L), class = c("tbl_df", "tbl", "data.frame"))
ggplot(df, aes(x = valence, y = energy)) + 
  geom_point() + 
  scale_x_continuous(limits = c(0, 1)) + 
  scale_y_continuous(limits = c(0, 1)) + 
  geom_hline(aes(yintercept = 0.5)) + 
  geom_vline(aes(xintercept = 0.5)) + 
  geom_density_2d() + facet_wrap(~artist, scales = 'free')