散点图无法从数据中识别列名称 [英] Scatterpie doesn't recognize column name from data

查看:53
本文介绍了散点图无法从数据中识别列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 scatterpie 绘制8个数据点.但是,该函数抛出一个错误,即找不到具有y坐标值的列所对应的对象.

我的数据

 库(tidyverse)图书馆(散点图)my_df<-结构(list(day_in_july = 13:20,yes_and_yes = c(0.611814345991561,0.574750830564784、0.593323216995448、0.610539845758355、0.650602409638554,0.57429718875502、0.575971731448763、0.545454545454545),yes_but_no = c(0.388185654008439,0.425249169435216、0.406676783004552、0.389460154241645、0.349397590361446,0.42570281124498、0.424028268551237、0.454545454545455)),行名称= c(NA,-8L),类= c("tbl_df","tbl","data.frame"))>my_df###小标题:8 x 3## day_in_july yes_and_yes yes_but_no##< int>< dbl>< dbl>## 1 13 0.612 0.388## 2 14 0.575 0.425## 3 15 0.593 0.407## 4 16 0.611 0.389## 5 17 0.651 0.349## 6 18 0.574 0.426## 7 19 0.576 0.424## 8 20 0.545 0.455 

使用散点图绘制数据失败

我遵循了

但这不是我想要的.相反,我希望每个饼图的中心处的y值对应于该特定饼图的'yes_but_no'比例

解决方案

之所以出现此问题,是因为您为 y 映射&馅饼列.

geom_scatterpie 的基础代码进行了一些数据操作,以将数据帧旋转/收集(取决于您的tidyr软件包的术语),以使其为长格式,因此 yes_but_no 列ggplot尝试绘制最终数据时,它不再单独存在.以下将起作用:

  my_df $ y<-my_df $ yes_but_no#创建重复列ggplot()+geom_scatterpie(aes(x = day_in_july,y = y),数据= my_df,cols = colnames(my_df)[2:3]) 

为获得更好的外观,您可以在隔开y位置的同时应用相等的坐标:

  ggplot()+geom_scatterpie(aes(x = day_in_july,y = y * 100),#空间y位置向外数据= my_df,cols = colnames(my_df)[2:3])+scale_y_continuous(name =是但没有",标签=函数(x)x/100)+#保持原始y标签coord_fixed() 

I want to plot 8 data points using scatterpie. However, the function is throwing an error that it can't find the object corresponding to the column with values for y-coordinates.

My Data

library(tidyverse)
library(scatterpie)

my_df <- structure(list(day_in_july = 13:20, yes_and_yes = c(0.611814345991561, 
0.574750830564784, 0.593323216995448, 0.610539845758355, 0.650602409638554, 
0.57429718875502, 0.575971731448763, 0.545454545454545), yes_but_no = c(0.388185654008439, 
0.425249169435216, 0.406676783004552, 0.389460154241645, 0.349397590361446, 
0.42570281124498, 0.424028268551237, 0.454545454545455)), row.names = c(NA, 
-8L), class = c("tbl_df", "tbl", "data.frame"))  

> my_df

## # A tibble: 8 x 3
## day_in_july yes_and_yes yes_but_no
##         <int>       <dbl>      <dbl>
## 1          13       0.612      0.388
## 2          14       0.575      0.425
## 3          15       0.593      0.407
## 4          16       0.611      0.389
## 5          17       0.651      0.349
## 6          18       0.574      0.426
## 7          19       0.576      0.424
## 8          20       0.545      0.455

Plotting the data using scatterpie fails

I followed the code from the documentation, but it still isn't working for me.

ggplot() + 
  geom_scatterpie(aes(x = day_in_july, y = yes_but_no), 
                           data = my_df, 
                           cols = colnames(my_df)[2:3])  


## Error in FUN(X[[i]], ...) : object 'yes_but_no' not found

I've tried converting from tibble to a data.frame beforehand, but didn't solve the problem.


By the way, setting y as a constant (e.g., 2) works:

ggplot() + 
  geom_scatterpie(aes(x = day_in_july, y = 2), 
                           data = my_df, 
                           cols = colnames(my_df)[2:3])  + 
  coord_fixed()

But this is not what I want. Rather, I want the y-value each pie chart is centered at to correspond to the 'yes_but_no' proportion for that specific pie chart

解决方案

The problem arises because you recycled the same column for the y mapping & pie columns.

The underlying code for geom_scatterpie does some data manipulations to pivot/gather (depending on the terminology of your tidyr package) the dataframe to long form, so the yes_but_no column no longer exists on its own by the time ggplot tries to plot the finalised data. The following would work:

my_df$y <- my_df$yes_but_no # create a duplicate column

ggplot() + 
  geom_scatterpie(aes(x = day_in_july, y = y), 
                  data = my_df, 
                  cols = colnames(my_df)[2:3])

For better appearance, you can apply equal coordinates while spacing out the y positions:

ggplot() + 
  geom_scatterpie(aes(x = day_in_july, y = y*100), # space y positions out
                  data = my_df, 
                  cols = colnames(my_df)[2:3]) +
  scale_y_continuous(name = "yes but no",
                     labels = function(x) x/100) + # maintain original y labels
  coord_fixed()

这篇关于散点图无法从数据中识别列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆