R:从Tibble可视化数据

时间:2019-12-13 15:29:20

标签: r plot tibble

我有这段代码在下面构建了标题。

data.3<-data.1 %>% group_by(ICD10Code,PatientId) %>% 
summarise(ReferralSource=first(ReferralSource),NextAppt=first(NextAppt),Age=max(Age),
InsuranceName=toString(unique(InsuranceName))) %>% ungroup() %>% group_by(ICD10Code) %>% 
summarise(mAge = mean(Age, na.rm = T),Frequency=n())

然后我排序,这样我就可以看到“ ICD10Code”列中最常用的值是什么

data.3[order(-data.3$Frequency),]

结果是这样的:

ICD10Code  mAge Frequency frequency
   <fct>     <dbl>     <dbl>     <dbl>
 1 I10        51.0        86        86
 2 E119       47.8        35        35
 3 E782       51.1        27        27
 4 Z0000      51.1        21        21
 5 K219       42.6        18        18
 6 F418       41.8        17        17
 7 J449       57.2        17        17
 8 M545       51.8        17        17
 9 R030       49          16        16
10 J302       50.1        14        14
# … with 485 more rows

我要做的只是绘制前10个最常用的代码。在这一点上,我将尽一切努力来帮助我形象化此小标题。这个小标题正是我想要绘制的图形,但似乎我所拥有的所有内容都存在一些错误。

我已经尝试过了:

top_n(data.3, n=10, data.3$Frequency) %>%
ggplot(., aes(x=data.3$ICD10Code, y=data.3$Frequency))+
geom_bar(stat='bin')

结果是:

Error: Aesthetics must be either length 1 or the same as the data (23): x, y

也尝试过:

top_n(data.3, n=10, data.3$Frequency) %>%
plot(x=data.3$ICD10Code,data.3$Frequency)

但是得到这个:

Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ

我从SO那里搜索并尝试了不同的方法,似乎什么也无法工作。 我要完成的全部工作是按频率显示前10个ICD10Code和按mAge显示ICD10Code。

0 个答案:

没有答案